/*******************************/ /* */ /*  並べ替え(ソーティング) */ /* */ /* 1997/11/21 宍戸 輝光 */ /* */ /*******************************/ import java.awt.*; import java.util.Random; public class sorting extends java.applet.Applet implements Runnable { Image sc1,sc2; Graphics g_sc1,g_sc2; int i,j,k,w,n,cpn,exn; int data[]=new int[32]; int sorted[]=new int[32]; boolean go; Thread runner=null; Random rnd=new Random(); Button go_b,clear16_b,clear32_b; Choice sortm; Label cpL,exL; public void init(){ resize(340,300); setBackground(Color.green); setFont(new Font("TimesRoman",Font.PLAIN,12)); sc1=createImage(194,97); /* データ表示用イメージ */ g_sc1=sc1.getGraphics(); sc2=createImage(194,97); /* ソート表示用イメージ */ g_sc2=sc2.getGraphics(); this.setLayout((LayoutManager)null); go_b=new Button("Start"); /* ソート実行ボタン */ add(go_b); go_b.reshape(8,232,80,28); clear16_b=new Button("Clear(n=16)"); /* 16個のデータでクリア */ add(clear16_b); clear16_b.reshape(108,210,128,28); clear32_b=new Button("Clear(n=32)"); /* 32個のデータでクリア */ add(clear32_b); clear32_b.reshape(108,256,128,28); sortm=new Choice(); sortm.addItem("Selection Sort"); sortm.addItem("Bubble Sort"); add(sortm); sortm.reshape(204,64,128,24); cpL=new Label("Compare "); /* 比較回数表示用のラベル */ add(cpL); cpL.reshape(204,128,96,24); cpL.setBackground(Color.green); exL=new Label("Exchange "); /* 交換回数表示用のラベル */ add(exL); exL.reshape(204,160,96,24); exL.setBackground(Color.green); screenClear(sc1); screenClear(sc2); n=16; /* 最初に16個のデータを作り表示する */ dataMake(); } public void stop() { if (runner!=null) { runner.stop(); runner=null; } } public void screenClear(Image sc) { /* イメージをクリア */ Graphics g_sc=sc.getGraphics(); g_sc.setColor(Color.white); g_sc.fillRect(0,0,193,96); g_sc.setColor(Color.black); g_sc.drawRect(0,0,193,96); } public void dataMake() { /* 乱数でデータ作成 */ for (i=0;idata[j+1]) { w=data[j]; data[j]=data[j+1]; data[j+1]=w; exn++; /* 交換回数カウンタ+1 */ } cpn++; /* 比較回数カウンタ+1 */ drawSorted(j,j+1); /* ソートの様子を描画 */ if (!go) /* クリアされていたら終了 */ break; try {Thread.sleep(150);} /* 150msウエイト */ catch (InterruptedException e) {} } } public void drawSorted(int a,int b) { /* ソートの様子を描画 */ screenClear(sc2); /* ソート描画用イメージをクリア */ for (k=0;k