sortapp.java

来自「本程序提供了各种排序算法及演示,由java实现,可以清楚看到各算法的流程演示.」· Java 代码 · 共 533 行 · 第 1/2 页

JAVA
533
字号
   protected synchronized void pause()   {      Thread t = Thread.currentThread();      try      {         if (shouldRun)           runner.sleep(slider.getValue());         while ((!shouldRun) && (runner == t))           runner.sleep(100);      }      catch (InterruptedException ie) {  }   }   private void clearTextFields()   {      for(int i = 0; i < numOfVars; i++)        variables[i].setText("");      compField.setText("");      exchangeField.setText("");   }   private void stopAndClear()   {      stop();      clearTextFields();      program.clear();      ok.setEnabled(true);      contPause.setLabel("  Pause  ");      contPause.setEnabled(false);      clear.setEnabled(false);      if ((typeOfSort == ss.QUICK) || (typeOfSort == ss.MERGE))        stackArea.clear();      if (threadStarted)        arrayCanvas.clear();      arrayLine.requestFocus();   }   public abstract void run();   public abstract void noGraphics();   protected void copyToRandomCopy()   {      randomCopy = new int[copy.length];      for(int i = 0; i < copy.length; i++)        randomCopy[i] = copy[i];   }   protected void AToCopy()   {      copy = new int[A.length];      for(int i = 0; i < A.length; i++)        copy[i] = A[i];   }   private void copyToA()   {      A = new int[copy.length];      for(int i = 0; i < copy.length; i++)        A[i] = copy[i];   }   private void randomCopyToA()   {      A = new int[randomCopy.length];      for(int i = 0; i < randomCopy.length; i++)        A[i] = randomCopy[i];   }   private void processCommand()   {      stop();      arrayLine.requestFocus();      String gottenText = arrayLine.getText().trim();      arrayLine.setText(gottenText);      arrayLine.selectAll();      if (gottenText.equals(""))      {         MessageBox mb = new MessageBox(ss, this, "Error",                         "Cannot sort array: no array entered.", "exclaim.gif");      }      else      {         CharArray dataLine = new CharArray(gottenText);         boolean ok = true;         if ((isSpecial) && ((gottenText.indexOf("rand") == 0) || (gottenText.indexOf("sorted") == 0) ||             (gottenText.indexOf("reverse") == 0)) && (!gottenText.equals(arrayString)))         {            ok = false;            YesNoBox ynb = new YesNoBox(ss, this, "Question",                           "Special-case array illegally altered.  Revert to original?",                           "questionMark.gif");            if (ynb.getAnswer() == YesNoBox.YES)            {               arrayLine.setText(arrayString);               arrayLine.selectAll();               copy = new int[randomCopy.length];               for(int i = 0; i < randomCopy.length; i++)                 copy[i] = randomCopy[i];               ok = true;            }         }         else if (((gottenText.indexOf("rand") == 0) || (gottenText.indexOf("sorted") == 0) ||                   (gottenText.indexOf("reverse") == 0)) && (gottenText.equals(arrayString)))            isSpecial = true;         else            isSpecial = false;         if (ok)         {            if (!isSpecial)            {               try               {                  A = dataLine.getBoundlessArray();                  String s = "";                  for(int i = 0; i < A.length; i++)                    s += Integer.toString(A[i]) + " ";                  arrayLine.setText(s.trim());                  arrayLine.selectAll();                  if (A.length > SortStarter.MAXLENGTH)                  {                     ok = false;                     YesNoBox ynb = new YesNoBox(ss, this, "Question",                                    "Array exceeds max length of " + ss.MAXLENGTH + " integers. Proceed without graphics?",                                    "questionMark.gif");                     if (ynb.getAnswer() == YesNoBox.YES)                       prepareNoGraphics();                  }               }               catch (ArrayException ae)               {                  ok = false;                  MessageBox mb = new MessageBox(ss, this, "Error", ae.getMsg(), "exclaim.gif");               }            }            else            {               copyToA();               if (A.length > SortStarter.MAXLENGTH)               {                  ok = false;                  YesNoBox ynb = new YesNoBox(ss, this, "Question",                                 "Array exceeds max length of " + ss.MAXLENGTH + " integers. Proceed without graphics?",                                 "questionMark.gif");                  if (ynb.getAnswer() == YesNoBox.YES)                    prepareNoGraphics();               }            }         }         if (ok)         {            AToCopy();            if (typeOfSort == ss.MERGE)            {               scratch = new int[A.length];               for(int i = 0; i < scratch.length; i++)                 scratch[i] = Integer.MAX_VALUE;               arrayCanvas.setExtendedData(scratchPointers, scratchPointersPos, scratchStatusArray);            }            else               scratch = null;            arrayCanvas.setData(A, scratch, arrayPointers, arrayPointersPos, colorArray, arrayPane);            clearTextFields();            this.ok.setEnabled(false);            contPause.setEnabled(true);            contPause.setLabel("  Pause  ");            clear.setEnabled(true);            if (typeOfSort == ss.QUICK)              stackArea.clear();            shouldRun = true;            start();         }      }   }   private void prepareNoGraphics()   {      if (threadStarted)         stopAndClear();      else         clearTextFields();      arrayLine.requestFocus();      String gottenText = arrayLine.getText().trim();      arrayLine.setText(gottenText);      arrayLine.selectAll();      if (gottenText.equals(""))      {         arrayLine.setText("");         MessageBox mb = new MessageBox(ss, this, "Error",                         "Cannot sort array: no array entered.", "exclaim.gif");      }      else      {         CharArray dataLine = new CharArray(gottenText);         boolean ok = true;         if ((isSpecial) && ((gottenText.indexOf("rand") == 0) || (gottenText.indexOf("sorted") == 0) ||             (gottenText.indexOf("reverse") == 0)) && (!gottenText.equals(arrayString)))         {            ok = false;            YesNoBox ynb = new YesNoBox(ss, this, "Question",                           "Special-case array illegally altered.  Revert to original?",                           "questionMark.gif");            if (ynb.getAnswer() == YesNoBox.YES)            {               arrayLine.setText(arrayString);               arrayLine.selectAll();               randomCopyToA();               ok = true;            }         }         else if (((gottenText.indexOf("rand") == 0) || (gottenText.indexOf("sorted") == 0) ||                   (gottenText.indexOf("reverse") == 0)) && (gottenText.equals(arrayString)))            isSpecial = true;         else            isSpecial = false;         if (ok)         {            if (isSpecial)            {               copyToA();               noGraphics();            }            else            {               try               {                  A = dataLine.getBoundlessArray();                  AToCopy();                  noGraphics();               }               catch (ArrayException are)               {                  MessageBox mb = new MessageBox(ss, this, "Error", are.getMsg(), "exclaim.gif");               }            }         }      }   }   public void actionPerformed(ActionEvent ae)   {         if (ae.getSource() == ok)         processCommand();      else if (ae.getSource() == help)         if ((helpWindow == null) || (!helpWindow.isVisible()))         {            String docTitles[] =              {"About the Algorithms",               "How to Operate " + '"' + "Sorting Demo" + '"'};            String fileNames[] =               {"Algorithms.dat",               "Operation.dat"};            helpWindow = new HelpWindow(ss, this, product, docTitles, fileNames);         }         else         {            helpWindow.toFront();            helpWindow.requestFocus();         }      else if (ae.getSource() == about)      {         AboutBox aboutBox = new AboutBox(ss, this, product,                                 "Written by Brian S. Borowski",                                 "Seton Hall University",                                 "Dept. of Math and Computer Science",                                 "

⌨️ 快捷键说明

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