⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortstarter.java

📁 本程序提供了各种排序算法及演示,由java实现,可以清楚看到各算法的流程演示.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         sortChoice.requestFocus();         start.setEnabled(false);         sortChoice.setEnabled(false);         importBox.setEnabled(false);         arrayField.setEnabled(false);         specialBox.setEnabled(false);         arrayChoice.setEnabled(false);         lengthChoice.setEnabled(false);         generate.setEnabled(false);         compare.setEnabled(false);         if (importBox.getState())         {            CharArray dataLine = new CharArray(arrayField.getText().trim());            if (dataLine.isExistence())              try              {                 A = dataLine.getBoundlessArray();                 importStr = "";                 for(int i = 0; i < A.length; i++)                   importStr += Integer.toString(A[i]) + " ";                 importStr = importStr.trim();                 arrayField.setText(importStr);                 s = importStr;                 A = null;              }              catch(ArrayException arrayExc){}         }         else if (specialBox.getState())         {            s = specialStr;                    }         switch(typeOfSort)         {            case BUBBLE:              bsa = new BubbleSortApp(this, true, s, table, A);              break;            case SELECT:              ssa = new SelectionSortApp(this, true, s, table, A);              break;            case INSERT:              isa = new InsertionSortApp(this, true, s, table, A);              break;            case SHELL:              shsa = new ShellSortApp(this, true, s, table, A);              break;            case QUICK:              qsa = new QuickSortApp(this, true, s, table, A);              break;            case MERGE:              msa = new MergeSortApp(this, true, s, table, A);              break;            case HEAP:              hsa = new HeapSortApp(this, true, s, table, A);              break;            case OETRANS:              oetsa = new OETransSortApp(this, true, s, table, A);              break;            case SHEAR:              shrsa = new ShearSortApp(this, true, s, table, A);              break;          }      }      else if (ae.getSource() == compare)      {         if (table == null)           table = new CompareTable(this, strChoices, sortInfo);         else           table.toFront();      }      else if (ae.getSource() == generate)         makeAnArray();   }   private void makeRandomArray(int _highIndex)   {      int highIndex = _highIndex;      A = new int[highIndex];      copySpecialArray = new int[highIndex];      specialStr = "rand(" + highIndex + "): ";      for(int i = 0; i < highIndex; i++)      {         A[i] = (int)(Math.random() * 999);         copySpecialArray[i] = A[i];         if (i < MAXLENGTH)           specialStr += Integer.toString(A[i]) + " ";         else if (i == MAXLENGTH)           specialStr += "...";      }      specialStr = specialStr.trim();      arrayField.setText(specialStr);   }   private void makeSortedArray(int _highIndex)   {      int highIndex = _highIndex;      A = new int[highIndex];      copySpecialArray = new int[highIndex];      specialStr = "sorted(" + highIndex + "): ";      for(int i = 0; i < highIndex; i++)      {         A[i] = i + 1;         copySpecialArray[i] = A[i];         if (i < MAXLENGTH)           specialStr += Integer.toString(A[i]) + " ";         else if (i == MAXLENGTH)           specialStr += "...";      }      specialStr = specialStr.trim();      arrayField.setText(specialStr);   }   private void makeReverseArray(int _highIndex)   {      int highIndex = _highIndex;      A = new int[highIndex];      copySpecialArray = new int[highIndex];      specialStr = "reverse(" + highIndex + "): ";      for(int i = 0; i < highIndex; i++)      {         A[i] = highIndex - i;         copySpecialArray[i] = A[i];         if (i < MAXLENGTH)           specialStr += Integer.toString(A[i]) + " ";         else if (i == MAXLENGTH)           specialStr += "...";      }      specialStr = specialStr.trim();      arrayField.setText(specialStr);   }   private void makeAnArray()   {      if (arrayChoice.getSelectedIndex() == RANDOM)        makeRandomArray(Integer.parseInt(lengthChoice.getSelectedItem().toString()));      else if (arrayChoice.getSelectedIndex() == SORTED)        makeSortedArray(Integer.parseInt(lengthChoice.getSelectedItem().toString()));      else        makeReverseArray(Integer.parseInt(lengthChoice.getSelectedItem().toString()));   }   public void focusGained(FocusEvent fe){}   public void focusLost(FocusEvent fe)   {      if (fe.getSource() == importBox)      {         if (importBox.getState())         {            arrayField.selectAll();            arrayField.requestFocus();         }      }      else if (fe.getSource() == specialBox)      {         if (!specialBox.getState())         {            arrayChoice.setEnabled(false);            lengthChoice.setEnabled(false);         }      }      else         arrayField.select(0, 0);   }   private void removeRand()   {      String a = arrayField.getText();      int pos = a.indexOf(":");      if (pos != -1)         a = a.substring(pos + 2);      arrayField.setText(a);   }   public void itemStateChanged(ItemEvent ie)   {      if (ie.getSource() == sortChoice)        typeOfSort = sortChoice.getSelectedIndex();      else if (ie.getSource() == importBox)        if (importBox.getState())        {           removeRand();           arrayChoice.setEnabled(false);           lengthChoice.setEnabled(false);           generate.setEnabled(false);           arrayField.setEditable(true);           arrayField.setBackground(Color.white);           arrayField.requestFocus();           arrayField.selectAll();           A = null;        }        else        {           arrayField.select(0, 0);           arrayField.setEditable(false);           arrayField.setBackground(SystemColor.activeCaptionBorder);        }      else if (ie.getSource() == specialBox)      {         if (specialBox.getState())         {            arrayField.setEditable(false);            arrayField.setBackground(SystemColor.activeCaptionBorder);            arrayChoice.setEnabled(true);            lengthChoice.setEnabled(true);            if (arrayChoice.getSelectedIndex() == RANDOM)               generate.setEnabled(true);            arrayChoice.requestFocus();            if (copySpecialArray == null)               makeAnArray();            else            {               A = copySpecialArray;               String specialStr;               if (arrayChoice.getSelectedIndex() == RANDOM)                  specialStr = "rand(";               else if (arrayChoice.getSelectedIndex() == SORTED)                  specialStr = "sorted(";               else                  specialStr = "reverse(";               specialStr += lengthChoice.getSelectedItem() + "): ";               for(int i = 0; i < A.length; i++)               {                  if (i < MAXLENGTH)                    specialStr += Integer.toString(A[i]) + " ";                  else if (i == MAXLENGTH)                    specialStr += "...";               }               specialStr = specialStr.trim();               arrayField.setText(specialStr);            }         }         else         {            arrayChoice.setEnabled(false);            lengthChoice.setEnabled(false);            generate.setEnabled(false);            arrayField.setEditable(true);            arrayField.setBackground(Color.white);            A = null;         }      }      else if (ie.getSource() == arrayChoice)      {         if (arrayChoice.getSelectedIndex() == RANDOM)            generate.setEnabled(true);         else            generate.setEnabled(false);         makeAnArray();      }      else if (ie.getSource() == lengthChoice)         makeAnArray();   }   private void setBGColor(String background)   {      if ((background == null) || (background.length() != 7) ||          (background.charAt(0) != '#'))         setBackground(Color.white);      else      {         String hexcolor, red, green, blue;         hexcolor = background.substring(1, background.length());         red = hexcolor.substring(0, 2);         green = hexcolor.substring(2, 4);         blue = hexcolor.substring(4, 6);         setBackground(new Color(Integer.parseInt(red, 16),                                 Integer.parseInt(green, 16),                                 Integer.parseInt(blue, 16)));      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -