I can't seem to get this program to sort. Not sure why. Can anyone see a problem with my bubble sort?
thanks.
Code:import java.awt.*; import java.applet.*; import java.awt.event.*; public class Assignment8b extends Applet implements ActionListener { private Button add, display, sort; private TextField input; private int j; private String [] stringArray = new String [10]; private boolean clickedYet = false; private boolean displayNow = false; public void init() { //add button add = new Button ("Add"); add(add); add.addActionListener(this); //display button display = new Button ("Display"); add(display); display.addActionListener(this); //search button sort = new Button ("Sort"); add(sort); sort.addActionListener(this); //input textField input = new TextField (20); add (input); input.addActionListener (this); } public void paint(Graphics g){ g.drawString ("Array output below",50,75); g.drawLine (50,80,150,80); if (displayNow) display (stringArray, g); } public void actionPerformed (ActionEvent event) { clickedYet = true; if (clickedYet){ if (event.getSource() == add) addData (stringArray); if (event.getSource() == display) displayNow = true; if (event.getSource() == sort) sort (stringArray); } input.setText(null); repaint (); } // This section below contains the methods to carry out the tasks 1,2 and 3 public void addData (String [] array) { array[j++] = input.getText(); } public void display(String [] array, Graphics g) { int yValue=110; for (int w=0; w < array.length; w++) { g.drawString("Index "+w+" is "+array[w], 70, yValue); yValue +=20; } } //bubble sort public void sort(String [] array) { String temp; for (int pass=0; pass < (array.length - 1); pass++) for (int i=1; i < (array.length - pass); i++) if (array[i].compareTo(array[i-1]) > 0) { temp = array [i]; array [i] = array [i - 1]; array [i-1] = temp; } } } // end of program


Reply With Quote


Bookmarks