//www.configure-all.com //Author: Sergey Skudaev //Insert Sort Algorithm Demo //Copyright by Sergey Skudaev. All right reserved import java.applet.*; import java.awt.*; import java.awt.event.*; public class InSortArr extends Applet implements ActionListener, Runnable { char charArr[]; String input; int last; Button temp; Thread thread; boolean equalFlag = false; String [] arr = {"6", "2", "7", "3", "9", "1", "5", "4", "0", "8"}; Font f; private Button[] btnCh = new Button[10]; Button btnInput; Panel pbtn; Panel ptCh; Panel ptemp; Panel pcom; Button btncur; Label lcur; int count; Label ltemp; public void init() { f = new Font("SanSerif", Font.BOLD, 20); setLayout(new BorderLayout()); temp = new Button(" "); ltemp = new Label("Temp "); ltemp.setForeground(Color.red); btncur = new Button(" "); btncur.setBackground(Color.yellow); lcur = new Label("Current"); lcur.setForeground(Color.red); pbtn = new Panel(); pbtn.setLayout(new FlowLayout()); ptCh = new Panel(); ptCh.setLayout(new FlowLayout()); ptemp = new Panel(); ptemp.setLayout(new FlowLayout()); pcom = new Panel(); pcom.setLayout(new BorderLayout()); for(int i = 0; i <10; i++) btnCh[i] = new Button(arr[i]); btnInput = new Button("Sort"); btnInput.addActionListener(this); pbtn.add(btnInput); last = btnCh.length; ptCh.add(btnCh[0]); ptCh.add(btnCh[1]); ptCh.add(btnCh[2]); ptCh.add(btnCh[3]); ptCh.add(btnCh[4]); ptCh.add(btnCh[5]); ptCh.add(btnCh[6]); ptCh.add(btnCh[7]); ptCh.add(btnCh[8]); ptCh.add(btnCh[9]); ptemp.add(btncur); ptemp.add(lcur); ptemp.add(temp); ptemp.add(ltemp); pcom.add(ptCh, BorderLayout.NORTH); pcom.add(ptemp, BorderLayout.SOUTH); add(pcom, BorderLayout.NORTH); add(pbtn, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { if(e.getSource() == btnInput) run(); } public void run() { int current = 1; String lbltemp; int walker; while(current < last) { temp.setLabel(btnCh[current].getLabel()); //temp.setBackground(Color.red); walker = current -1; lbltemp = temp.getLabel(); //< while((walker >= 0)&&(lbltemp.compareTo(btnCh[walker].getLabel()) < 0)) { btnCh[walker+1].setLabel(btnCh[walker].getLabel()); for(int l = 0; l< last;l++) btnCh[l].setBackground(Color.lightGray); btnCh[walker+1].setBackground(Color.red); btnCh[walker].setBackground(Color.red); btnCh[current].setBackground(Color.yellow); walker = walker -1; try { Thread.sleep(3000); } catch(InterruptedException e) { break; } } btnCh[walker+1].setLabel(temp.getLabel()); current = current + 1; } } public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString("Insertion Sort Illustration", 180, 150); } }