Rev 51 | Rev 54 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
// Written by Ira Snyderimport java.io.*;public class Test {private static int compCount, swapCount;private class Entry implements Comparable {public int x;public Entry( int x ) { this.x = x; }public int compareTo( Object o ) {compCount++;if( !(o instanceof Entry) ) throw new IllegalArgumentException();Entry y = (Entry)o;return this.x - y.x;}public Entry setEntry( int x ) {return new Entry(x);}public String toString() { return Integer.toString(x); }} //end Entry classpublic int getCompCount() { return compCount; }public int getSwapCount() { return swapCount; }public void resetCompCount() { compCount = 0; }public void resetSwapCount() { swapCount = 0; }//this is the only way a swap method can be made in java.//the usual C++ (and others) way of using pointers is not//possible in java. (I've come up with a very solid//explanation of this. See me if you want to know.)public static void swap( Object[] array, int posA, int posB ) {Object temp = array[posB]; // temp = Barray[posB] = array[posA]; // B = Aarray[posA] = temp; // A = tempswapCount++;}public void printArray( Object[] array, PrintStream ps ) {int i=0;for( i=0; i<array.length-1; i++ ) { System.out.print(array[i] + ", "); }System.out.println(array[i]); //print the last element}public void testBubbleSort() {Entry[] array = { new Entry(3), new Entry(2), new Entry(1), new Entry(12), new Entry(812), new Entry(10), new Entry(4) };printArray(array,System.out);SortMethods.QuickSort(array,0,array.length);printArray(array,System.out);System.out.println("swapcount: " + getSwapCount());System.out.println("compcount: " + getCompCount());}}