Rev 100 | Blame | Compare with Previous | Last modification | View Log | RSS feed
// Written by Ira Snyder// Project #5// Due Date: 12-03-2004// People Helped: Allen Oliver// License: Public Domain (added 07-11-2005)import java.io.*;import java.util.Random;public class Test {//some variables to keep track of compares and swapsprivate static int compCount, swapCount;//the class Entry, which stores int'sprivate class Entry implements Comparable {public int x; //the data//constructorpublic Entry( int x ) { this.x = x; }//method to compare this to another object//returns 0 if they are equal, >0 if this is bigger,//and <0 if o is biggerpublic int compareTo( Object o ) {compCount++;if( !(o instanceof Entry) ) throw new IllegalArgumentException();Entry y = (Entry)o;return this.x - y.x;}//change the value of the current Entrypublic Entry setEntry( int x ) {return new Entry(x);}//print the Entrypublic String toString() { return Integer.toString(x); }} //end Entry class//get the private variablespublic int getCompCount() { return compCount; }public int getSwapCount() { return swapCount; }//reset compares and swapspublic void resetCompCount() { compCount = 0; }public void resetSwapCount() { swapCount = 0; }//increment the swapcount variablepublic static void incSwapCount() { swapCount++; }public static void incSwapCount( int increaseBy ) { swapCount += increaseBy; }//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.)////this method adds to swapcount, and should be called for swaps//during a test runpublic static void swap( Object[] array, int posA, int posB ) {Object temp = array[posB]; // temp = Barray[posB] = array[posA]; // B = Aarray[posA] = temp; // A = tempswapCount++;}//method to print the entire array of type Objectpublic 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}//method to generate an array of Entry's. It can generate arrays of//any size, and will generate then with different ordering of numbers//inside the array//// howMany should be how many numbers you want to generate// order: 0 = random// 1 = ascending// 2 = descendingpublic Entry[] generateNumbers( int howMany, int order ) {Random generator = new Random();Entry[] answer = new Entry[howMany];//if order is a bad value, throw an Exceptionif( order < 0 || order > 3 ) throw new IllegalArgumentException();for( int i=0; i<howMany; i++ ) {//I deemed random int's from 0 to 10239 to be a good test//so I used these instead of the default, which is from// -MAX_INT to +MAX_INTif( order == 0 ) { answer[i] = new Entry(generator.nextInt(10240)); }else if( order == 1) { answer[i] = new Entry(i); } //ascendingelse if( order == 2 ){ answer[i] = new Entry(howMany-i-1); } //descendingelse { answer[i] = new Entry(1); } //set all to 1.0}return answer;}//method to generate a nice header for the program's output//I did this to eliminate duplicate codeprivate String generateHeader() {StringBuffer output = new StringBuffer("Method");StringBuffer spaces = new StringBuffer();for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }output.append(spaces);output.append("Number Compares");spaces = new StringBuffer();for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }output.append(spaces);output.append("Data Movements");return output.toString();}//method to generate a properly spaced output line, which is used//to output the type of sort method, the number of swaps, and the//number of compares////this prints out the number of swaps and compares that are stored//at the time that this is calledprivate String generateOutputLine( String name ) {StringBuffer output = new StringBuffer(name); //put the nameStringBuffer spaces = new StringBuffer();for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }output.append(spaces);output.append(getCompCount()); //appends the current number of compsspaces = new StringBuffer();for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }output.append(spaces);output.append(getSwapCount()); //appends the current number of swapsreturn output.toString();}//method to run BubbleSort and print the outputpublic void testBubbleSort( Entry[] array ) {resetSwapCount(); //resets the counting variablesresetCompCount();SortMethods.BubbleSort(array); //sort the array//print the output lineSystem.out.println( generateOutputLine("BubbleSort"));}//method to run ImprovedBubbleSort and print the outputpublic void testImprovedBubbleSort( Entry[] array ) {resetSwapCount(); //resets the counting variablesresetCompCount();SortMethods.ImprovedBubbleSort(array); //sort the array//print the output lineSystem.out.println( generateOutputLine("Impr. BubbleSort"));}//method to run SelectionSort and print the outputpublic void testSelectionSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.SelectionSort(array); //sort the array//print the output lineSystem.out.println( generateOutputLine("SelectionSort"));}//method to run InsertionSort and print the outputpublic void testInsertionSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.InsertionSort(array); //sort the array//print the output lineSystem.out.println( generateOutputLine("InsertionSort"));}//method to run ShellSort and print the output linepublic void testShellSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.ShellSort(array); //sort the array//print the output lineSystem.out.println( generateOutputLine("ShellSort"));}//method to run the MergeSort and print the output linepublic void testMergeSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.MergeSort(array); //sort the array//print the output line for MergeSortSystem.out.println( generateOutputLine("MergeSort"));}//method to run the HeapSort and print the output linepublic void testHeapSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.HeapSort(array); //sort the array//print the output line for HeapSortSystem.out.println( generateOutputLine("HeapSort"));}//method to run QuickSort on the array, and print the output linepublic void testQuickSort( Entry[] array ) {resetSwapCount(); //reset counting variablesresetCompCount();SortMethods.QuickSort(array); //sort the array//print the output line for QuickSortSystem.out.println( generateOutputLine("QuickSort"));}//a generic method that will print out the header given to it// as a parameter, then run the suite of tests on the given arraypublic void runTest( Entry[] array, String testHeader ) {//make 6 arrays (copies)Entry[] arrayc1 = new Entry[array.length];Entry[] arrayc2 = new Entry[array.length];Entry[] arrayc3 = new Entry[array.length];Entry[] arrayc4 = new Entry[array.length];Entry[] arrayc5 = new Entry[array.length];Entry[] arrayc6 = new Entry[array.length];Entry[] arrayc7 = new Entry[array.length];//fill the copies from the originalSystem.arraycopy(array,0,arrayc1,0,array.length);System.arraycopy(array,0,arrayc2,0,array.length);System.arraycopy(array,0,arrayc3,0,array.length);System.arraycopy(array,0,arrayc4,0,array.length);System.arraycopy(array,0,arrayc5,0,array.length);System.arraycopy(array,0,arrayc6,0,array.length);System.arraycopy(array,0,arrayc7,0,array.length);//print both headersSystem.out.println(testHeader);System.out.println( generateHeader() );//run each test on one copy of the original arraytestBubbleSort(array); //BubbleSorttestImprovedBubbleSort(arrayc1); //ImprovedBubbleSorttestSelectionSort(arrayc2); //SelectionSorttestInsertionSort(arrayc3); //InsertionSorttestShellSort(arrayc4); //ShellSort (AKA Comb Sort)testMergeSort(arrayc5); //MergeSorttestHeapSort(arrayc6); //HeapSorttestQuickSort(arrayc7); //QuickSort}//run the test for 512 random elementspublic void test1() {Entry[] array = generateNumbers(512,0);String header = "Test 1: Array Length 512 Composed of Random Elements";runTest(array,header);}//run the test for 1024 random elementspublic void test2() {Entry[] array = generateNumbers(1024,0);String header = "Test 2: Array Length 1024 Composed of Random Elements";runTest(array,header);}//run the test for 2048 random elementspublic void test3() {Entry[] array = generateNumbers(2048,0);String header = "Test 3: Array Length 2048 Composed of Random Elements";runTest(array,header);}//run the test for 4096 random elementspublic void test4() {Entry[] array = generateNumbers(4096,0);String header = "Test 4: Array Length 4096 Composed of Random Elements";runTest(array,header);}//run the test for 1024 Ascending elements (0,1,2 ... 1023,1024)public void test5() {Entry[] array = generateNumbers(1024,1);String header = "Test 5: Array Length 1024 Composed of Ascending Elements";runTest(array,header);}//run the test for 1024 Descending elements (1024,1023 ... 2,1,0)public void test6() {Entry[] array = generateNumbers(1024,2);String header = "Test 6: Array Length 1024 Composed of Descending Elements";runTest(array,header);}//run the test for 1024 elements that all have the value 1.0public void test7() {Entry[] array = generateNumbers(1024,3);String header = "Test 7: Array Length 1024 Composed of Elements of value 1.0";runTest(array,header);}}