Java Code for a Bubble Sort

Java Code for a Bubble Sort

In the bubbleSort.java program, shown in Listing 3.1, a class called ArrayBub encapsu- lates an array a[] , which holds variables of type long .

In a more serious program, the data would probably consist of objects, but we use a primitive type for simplicity. (We’ll see how objects are sorted in the objectSort.java program in Listing 3.4.) Also, to reduce the size of the listing, we don’t show find() and delete() methods with the ArrayBub class, although they would normally be part of a such a class.

LISTING 3.1 The bubbleSort.java Program // bubbleSort.java

// demonstrates bubble sort // to run this program: C>java BubbleSortApp //////////////////////////////////////////////////////////////// class ArrayBub

{ private long[] a; // ref to array a private int nElems; // number of data items

//-------------------------------------------------------------- public ArrayBub(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet }

//-------------------------------------------------------------- public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size }

//-------------------------------------------------------------- public void display() // displays array contents { for(int j=0; j<nElems; j++) // for each element,

System.out.print(a[j] + “ “); // display it System.out.println(“”); }

//-------------------------------------------------------------- public void bubbleSort()

86 CHAPTER 3 Simple Sorting

LISTING 3.1 Continued {

int out, in; for(out=nElems-1; out>1; out--) // outer loop (backward)

for(in=0; in<out; in++) // inner loop (forward) if( a[in] > a[in+1] ) // out of order? swap(in, in+1); // swap them } // end bubbleSort() //-------------------------------------------------------------- private void swap(int one, int two) { long temp = a[one]; a[one] = a[two]; a[two] = temp; }

//-------------------------------------------------------------- } // end class ArrayBub //////////////////////////////////////////////////////////////// class BubbleSortApp

{ public static void main(String[] args)

{ int maxSize = 100; // array size ArrayBub arr; // reference to array arr = new ArrayBub(maxSize); // create the array

arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);

arr.display(); // display items arr.bubbleSort(); // bubble sort them

Bubble Sort 87

LISTING 3.1 Continued arr.display(); // display them again

} // end main() } // end class BubbleSortApp ////////////////////////////////////////////////////////////////

The constructor and the insert() and display() methods of this class are similar to those we’ve seen before. However, there’s a new method: bubbleSort() . When this method is invoked from main() , the contents of the array are rearranged into sorted order.

The main() routine inserts 10 items into the array in random order, displays the array, calls bubbleSort() to sort it, and then displays it again. Here’s the output:

The bubbleSort() method is only four lines long. Here it is, extracted from the listing: public void bubbleSort()

{ int out, in;

for(out=nElems-1; out>1; out--) // outer loop (backward) for(in=0; in<out; in++) // inner loop (forward) if( a[in] > a[in+1] ) // out of order? swap(in, in+1); // swap them } // end bubbleSort()

The idea is to put the smallest item at the beginning of the array (index 0) and the largest item at the end (index nElems-1 ). The loop counter out in the outer for loop starts at the end of the array, at nElems-1 , and decrements itself each time through the loop. The items at indices greater than out are always completely sorted. The out variable moves left after each pass by in so that items that are already sorted are no longer involved in the algorithm.

The inner loop counter in starts at the beginning of the array and increments itself each cycle of the inner loop, exiting when it reaches out . Within the inner loop, the two array cells pointed to by in and in+1 are compared, and swapped if the one in in is larger than the one in in+1 .

For clarity, we use a separate swap() method to carry out the swap. It simply exchanges the two values in the two array cells, using a temporary variable to hold the value of the first cell while the first cell takes on the value in the second and

88 CHAPTER 3 Simple Sorting

then setting the second cell to the temporary value. Actually, using a separate swap() method may not be a good idea in practice because the function call adds a small amount of overhead. If you’re writing your own sorting routine, you may prefer to put the swap instructions in line to gain a slight increase in speed.