大虾们,帮忙做一个算法同步演示的可视化例子。
比如执行如下一个排序算法
 public void Bubble(int a[]){
    for (int i = a.length; --i>=0;){
      for (int j = 0; j <i; j++){
          if (a[j] > a[j+1]) {
              int T = a[j];
              a[j] = a[j+1];
              a[j+1] = T;
           }
       }
    }
}
当算法执行到if的代码块时,用蓝色显示“4.            交换a[j] 与 a[j+1]”这一句表示正在执行的动作,用绿色显示其他句子。当然为了看到演示过程,请在每次操作后设置适当演示。
算法:Bubble
输入:数组data[]
输出:按非降序排列的数组data[]
1.for i<—data.length-1 to 0
2.    for j<—  0 to i
3.        if a[j] > a[j+1] then
4.            交换a[j] 与 a[j+1]
5.        end if
6.    end for
7.end for

解决方案 »

  1.   

    这个applet程序应该满足你的要求吧
    import java.applet.Applet;  
    import java.awt.*;  
    import java.awt.event.*;  
    public class Sort extends Applet implements Runnable{  
         public  static int[] data={18,4,17,5,13,8,14,7,10,16,11,3,19,2,20,15,6,9,1,12};  
         Button bt=new Button("Start"); 
         Button bt2=new Button("Parse"); 
         Button bt3=new Button("Go on");
         boolean isParse=true;
               int x=50,y=100;  
               Graphics g=getGraphics();  
         public void init(){  
              add(bt);
              add(bt2);
              add(bt3);  
              this.setSize(300,300); 
              bt.addActionListener(new ActionListener(){  
                      public void actionPerformed(ActionEvent e){
                                isParse=false; 
                                 if(e.getSource()==bt)threadStart(); 
                                 bt.setEnabled(false);
                     }  
              });
              bt2.addActionListener(new ActionListener(){
                     public void actionPerformed(ActionEvent e){
                                if(e.getSource()==bt2)
                                 isParse=true;
                                             
                     }
              }); 
              bt3.addActionListener(new ActionListener(){
                     public void actionPerformed(ActionEvent e){
                                if(e.getSource()==bt3)
                                 isParse=false;
                                             
                     }
              }); 
         }  
         public void threadStart(){ 
             new Thread(this).start(); 
         } 
         public void paint(Graphics g){ 
               g.setColor(Color.green);  
               g.setFont(new Font("TimesRoman",Font.PLAIN,12));  
              for(int k=-1;++k   <data.length;){ 
                    g.drawRect(x,y+10*k,10*data[k],5);  
                    g.fillRect(x,y+10*k,10*data[k],5);  
                    g.drawString(String.valueOf(data[k]),x+10*data[k]+15,y+10*(k+1));  
                  } 
              } 
         public void run() { 
                sort(data); 
                } 
       public void sort(int a[]){  
        for (int i = a.length; --i>=0;)  
          for (int j = 0; j   <i; j++) { 
             while(isParse); 
              if (a[j] > a[j+1]) { 
                  int T = a[j]; 
                  a[j] = a[j+1]; 
                  a[j+1] = T; 
                  repaint();         
                  try { 
                    Thread.sleep(1000); 
                      } catch (InterruptedException e) { 
                          e.printStackTrace(); 
                      } 
                   
                  
                  } 
              } 
        } 
    }  
      

  2.   

    有点IDE调试功能的感觉
    等待高人。