2016-09-04 43 views
0

在这里,我尝试使用插入排序对整数数组进行排序,并根据排序机制逐步打印数组。我希望在每个排序步骤之间有一点延迟。我尝试了Thread.sleep和其他一些方法,但找不到解决方案。请帮助我,我是编程初学者。如何在for循环中添加延迟?

public void insertionSort(int array[]){ 

     int j, k, key; 

     for(j=1; j<array.length; j++){ 

      key = array[j]; 
      k = j-1; 

      while(k>=0 && array[k]>key){ 
       array[k+1] = array[k]; 
       k = k-1; 
      } 
      array[k+1] = key; 
      for(int i=0; i<array.length; i++){ 
       txtArOutput.append(String.valueOf(arr[i])+"\t"); 
      } 
      txtArOutput.append("\n"); 
      //delay 
     } 
    } 
+0

你为什么要提高方法的执行时间? –

+0

你想要1)使用[Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html),2)[提高你的搜索技巧](https:// www.google.com/webhp?rct=j#q=site:stackoverflow.com+java+swing+loop+delay) –

+0

@NikolasCharalambidis我只是想在每个打印语句之间有一点延迟,就像模拟器 – Isuru

回答

0

希望这样有助于

public class InsertionSortDemo extends JFrame { 

    private JTextArea textArea; 
    private JButton button; 

    public InsertionSortDemo() { 

     textArea = new JTextArea(); 

     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(textArea, BorderLayout.CENTER); 

     button = new JButton("Run Simulation"); 
     button.addActionListener(e -> { 

      button.setEnabled(false); 
      textArea.setText(""); 

      new Thread(() -> { 

       Random r = new Random(); 

       int [] array = { 

         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 
         r.nextInt(100), 

       }; 
       insertionSort(array); 

      }).start(); 

     }); 

     panel.add(button, BorderLayout.SOUTH); 

     setContentPane(panel); 
     setSize(300, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> new InsertionSortDemo()); 
    } 

    public void insertionSort(int array[]){ 

     int j, k, key; 

     for(j=1; j<array.length; j++){ 

      key = array[j]; 
      k = j-1; 

      while(k>=0 && array[k]>key){ 
       array[k+1] = array[k]; 
       k = k-1; 
//    print(array); 
      } 
      array[k+1] = key; 

      print(array); 

      sleep(100); 

     } 

     SwingUtilities.invokeLater(() -> button.setEnabled(true)); 

    } 

    private void print(int array[]) { 

     SwingUtilities.invokeLater(() -> { 

      for (int i : array) { 
       textArea.append(i + ","); 
      } 
      textArea.append(System.lineSeparator()); 

     }); 

    } 

    private void sleep(long n) { 
     try { 
      Thread.sleep(500); 
     } catch (InterruptedException e) { 
     } 
    } 

} 
+0

不能我使用这种方法在文本区域打印输出,因为我正在使用秋千 – Isuru

+0

它一次打印整个东西,没有延迟:( – Isuru

+0

希望这种方式可以帮助 – guleryuz