//www.configure-all.com //Author: Sergey Skudaev //Two Directional Bubble Sort Algorithm Demo //Copyright by Sergey Skudaev. All right reserved import java.applet.*; import java.awt.*; import java.awt.event.*; public class TwoBubSort extends Applet implements ActionListener, Runnable { char charArr[]; String input; int last; boolean exchange; Button temp; Button temp2; Label ltemp; 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; Label ltmp2; int count; public void init() { f = new Font("SanSerif", Font.BOLD, 20); setLayout(new BorderLayout()); temp = new Button(" "); temp.setBackground(Color.red); temp2 = new Button(" "); temp2.setBackground(Color.yellow); ltmp2 = new Label("Temp"); ltmp2.setForeground(Color.red); pbtn = new Panel(); ltemp = new Label("Temp"); ltemp.setForeground(Color.red); 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(temp2); ptemp.add(ltmp2); 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() { String lblBtnCh; String lblBtn; int curr = 0; boolean sorted = false; while((curr < last)&&(sorted == false)) { sorted = true; int walker1 = last-1; int walker2 = curr; while(walker1 > curr) { for(int l = 0; l< 10; l++) btnCh[l].setBackground(Color.lightGray); btnCh[walker1].setBackground(Color.red); btnCh[walker1].setBackground(Color.red); btnCh[walker1-1].setBackground(Color.red); btnCh[walker2].setBackground(Color.yellow); btnCh[walker2+1].setBackground(Color.yellow); try { Thread.sleep(3000); } catch(InterruptedException e) { break; } lblBtnCh = btnCh[walker1].getLabel(); if(lblBtnCh.compareTo(btnCh[walker1-1].getLabel()) < 0) { sorted = false; temp.setLabel(btnCh[walker1].getLabel()); btnCh[walker1].setLabel(btnCh[walker1-1].getLabel()); btnCh[walker1-1].setLabel(temp.getLabel()); } walker1--; lblBtn = btnCh[walker2].getLabel(); if(lblBtn.compareTo(btnCh[walker2+1].getLabel()) > 0) { sorted = false; temp2.setLabel(btnCh[walker2].getLabel()); btnCh[walker2].setLabel(btnCh[walker2+1].getLabel()); btnCh[walker2+1].setLabel(temp2.getLabel()); } walker2++; } //inner while end curr++; last--; } //outer while end } public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString("Two Direction Bubble Sort Illustration", 100, 150); } }