//www.configure-all.com //Author: Sergey Skudaev //Two Directional Selection Sort Algorithm Demo //Copyright by Sergey Skudaev. All right reserved import java.applet.*; import java.awt.*; import java.awt.event.*; public class TwoSelArr extends Applet implements ActionListener, Runnable { Button btnsm, btnlg, btnw; Label lblsm, lbllg, lbltemp, lblw; 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; int count; public void init() { f = new Font("SanSerif", Font.BOLD, 20); setLayout(new BorderLayout()); btnw = new Button(" "); btnw.setBackground(Color.red); lblw = new Label("Walker"); lblw.setForeground(Color.red); temp = new Button(" "); lbltemp = new Label("Temp"); lbltemp.setForeground(Color.red); btnsm = new Button(" "); btnsm.setBackground(Color.yellow); lblsm = new Label("Smallest"); lblsm.setForeground(Color.red); btnlg = new Button(" "); btnlg.setBackground(Color.magenta); lbllg = new Label("Largest"); lbllg.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]); last = btnCh.length; btnInput = new Button("Sort"); btnInput.addActionListener(this); pbtn.add(btnInput); 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(temp); ptemp.add(lbltemp); ptemp.add(btnsm); ptemp.add(lblsm); ptemp.add(btnlg); ptemp.add(lbllg); ptemp.add(btnw); ptemp.add(lblw); 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 = 0; boolean sorted = false; last = 10; while((current < last-1) && (sorted == false)) { int smalest = current; int largest = last-1; int walker = current +1; String btnlbl, btnlbl2, btnlbl3; sorted = true; btnlbl = btnCh[smalest].getLabel(); if(btnlbl.compareTo(btnCh[largest].getLabel()) > 0 ) { temp.setLabel(btnCh[largest].getLabel()); btnCh[largest].setLabel(btnCh[smalest].getLabel()); btnCh[smalest].setLabel(temp.getLabel()); sorted = false; } try { Thread.sleep(2000); } catch(InterruptedException e) { } while( walker < last) { btnlbl2 = btnCh[walker].getLabel(); if(btnlbl2.compareTo(btnCh[smalest].getLabel())< 0) { temp.setLabel(btnCh[smalest].getLabel()); btnCh[smalest].setLabel(btnCh[walker].getLabel()); btnCh[walker].setLabel(temp.getLabel()); sorted = false; } btnlbl3 = btnCh[walker].getLabel(); if(btnlbl3.compareTo(btnCh[largest].getLabel()) > 0) { temp.setLabel(btnCh[walker].getLabel()); btnCh[walker].setLabel(btnCh[largest].getLabel()); btnCh[largest].setLabel(temp.getLabel()); sorted = false; } for(int k = 0; k< 10; k++) btnCh[k].setBackground(Color.lightGray); btnCh[largest].setBackground(Color.magenta); btnCh[smalest].setBackground(Color.yellow); btnCh[walker].setBackground(Color.red); btnsm.setLabel(btnCh[smalest].getLabel()); btnlg.setLabel(btnCh[largest].getLabel()); btnw.setLabel(btnCh[walker].getLabel()); try { Thread.sleep(2000); } catch(InterruptedException e) { } walker = walker + 1; } current = current + 1; last = last -1; } } public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString("Two Direction Selective Sort Illustration", 100, 150); } }