Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
51 irasnyd 1
// Written by Ira Snyder
56 irasnyd 2
// Project #5
3
// Due Date: 12-03-2004
4
// People Helped: Allen Oliver
51 irasnyd 5
 
6
import java.io.*;
54 irasnyd 7
import java.util.Random;
51 irasnyd 8
 
9
public class Test {
56 irasnyd 10
    //some variables to keep track of compares and swaps
53 irasnyd 11
    private static int compCount, swapCount;
51 irasnyd 12
 
56 irasnyd 13
    //the class Entry, which stores int's
53 irasnyd 14
    private class Entry implements Comparable {
56 irasnyd 15
        public int x; //the data
51 irasnyd 16
 
56 irasnyd 17
        //constructor
51 irasnyd 18
        public Entry( int x ) { this.x = x; }
19
 
56 irasnyd 20
        //method to compare this to another object
21
        //returns 0 if they are equal, >0 if this is bigger,
22
        //and <0 if o is bigger
53 irasnyd 23
        public int compareTo( Object o ) {
51 irasnyd 24
            compCount++;
53 irasnyd 25
            if( !(o instanceof Entry) ) throw new IllegalArgumentException();
26
 
27
            Entry y = (Entry)o;
28
            return this.x - y.x;
51 irasnyd 29
        }
56 irasnyd 30
 
31
        //change the value of the current Entry
51 irasnyd 32
        public Entry setEntry( int x ) {
33
            return new Entry(x);
34
        }
35
 
56 irasnyd 36
        //print the Entry
51 irasnyd 37
        public String toString() { return Integer.toString(x); }
38
 
39
    } //end Entry class
40
 
56 irasnyd 41
    //get the private variables
51 irasnyd 42
    public int getCompCount() { return compCount; }
43
    public int getSwapCount() { return swapCount; }
56 irasnyd 44
 
45
    //reset compares and swaps
51 irasnyd 46
    public void resetCompCount() { compCount = 0; }
47
    public void resetSwapCount() { swapCount = 0; }
57 irasnyd 48
 
49
    //increment the swapcount variable
50
    public static void incSwapCount() { swapCount++; }
51
    public static void incSwapCount( int increaseBy ) { swapCount += increaseBy; }
51 irasnyd 52
 
53 irasnyd 53
    //this is the only way a swap method can be made in java.
54
    //the usual C++ (and others) way of using pointers is not
55
    //possible in java. (I've come up with a very solid
56
    //explanation of this. See me if you want to know.)
56 irasnyd 57
    //
58
    //this method adds to swapcount, and should be called for swaps
59
    //during a test run
53 irasnyd 60
    public static void swap( Object[] array, int posA, int posB ) {
61
        Object temp = array[posB];  // temp = B
56 irasnyd 62
        array[posB] = array[posA];  // B = A
63
        array[posA] = temp;         // A = temp
53 irasnyd 64
        swapCount++;
65
    }
56 irasnyd 66
 
67
    //method to print the entire array of type Object
53 irasnyd 68
    public void printArray( Object[] array, PrintStream ps ) {
69
        int i=0;
70
        for( i=0; i<array.length-1; i++ ) { System.out.print(array[i] + ", "); }
71
        System.out.println(array[i]); //print the last element
72
    }
54 irasnyd 73
 
56 irasnyd 74
    //method to generate an array of Entry's. It can generate arrays of
75
    //any size, and will generate then with different ordering of numbers
76
    //inside the array
77
    //
54 irasnyd 78
    // howMany should be how many numbers you want to generate
79
    // order: 0 = random
80
    //        1 = ascending
81
    //        2 = descending
82
    public Entry[] generateNumbers( int howMany, int order ) {
83
        Random generator = new Random();
84
        Entry[] answer = new Entry[howMany];
53 irasnyd 85
 
56 irasnyd 86
        //if order is a bad value, throw an Exception
55 irasnyd 87
        if( order < 0 || order > 3 ) throw new IllegalArgumentException();
54 irasnyd 88
 
89
        for( int i=0; i<howMany; i++ ) {
90
            //I deemed random int's from 0 to 10239 to be a good test
56 irasnyd 91
            //so I used these instead of the default, which is from
92
            // -MAX_INT to +MAX_INT
54 irasnyd 93
            if( order == 0 ) { answer[i] = new Entry(generator.nextInt(10240)); }
94
            else if( order == 1) { answer[i] = new Entry(i); } //ascending
55 irasnyd 95
            else if( order == 2 ){ answer[i] = new Entry(howMany-i-1); } //descending
96
            else { answer[i] = new Entry(1); } //set all to 1.0
54 irasnyd 97
        }
98
 
99
        return answer;
100
    }
101
 
56 irasnyd 102
    //method to generate a nice header for the program's output
103
    //I did this to eliminate duplicate code
55 irasnyd 104
    private String generateHeader() {
105
        StringBuffer output = new StringBuffer("Method");
106
        StringBuffer spaces = new StringBuffer();
107
 
108
        for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }
109
        output.append(spaces);
110
        output.append("Number Compares");
111
 
112
        spaces = new StringBuffer();
113
        for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }
114
        output.append(spaces);
57 irasnyd 115
        output.append("Data Movements");
55 irasnyd 116
 
117
        return output.toString();
54 irasnyd 118
    }
119
 
56 irasnyd 120
    //method to generate a properly spaced output line, which is used
121
    //to output the type of sort method, the number of swaps, and the
122
    //number of compares
123
    //
124
    //this prints out the number of swaps and compares that are stored
125
    //at the time that this is called
54 irasnyd 126
    private String generateOutputLine( String name ) {
127
        StringBuffer output = new StringBuffer(name); //put the name
128
        StringBuffer spaces = new StringBuffer();
51 irasnyd 129
 
54 irasnyd 130
        for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }
131
        output.append(spaces);
132
        output.append(getCompCount()); //appends the current number of comps
51 irasnyd 133
 
54 irasnyd 134
        spaces = new StringBuffer();
135
        for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }
136
        output.append(spaces);
137
        output.append(getSwapCount()); //appends the current number of swaps
138
 
139
        return output.toString();
51 irasnyd 140
    }
54 irasnyd 141
 
56 irasnyd 142
    //method to run BubbleSort and print the output
54 irasnyd 143
    public void testBubbleSort( Entry[] array ) {
56 irasnyd 144
        resetSwapCount(); //resets the counting variables
54 irasnyd 145
        resetCompCount();
146
 
56 irasnyd 147
        SortMethods.BubbleSort(array); //sort the array
54 irasnyd 148
 
56 irasnyd 149
        //print the output line
54 irasnyd 150
        System.out.println( generateOutputLine("BubbleSort"));
151
    }
56 irasnyd 152
 
153
    //method to run ImprovedBubbleSort and print the output
54 irasnyd 154
    public void testImprovedBubbleSort( Entry[] array ) {
56 irasnyd 155
        resetSwapCount(); //resets the counting variables
54 irasnyd 156
        resetCompCount();
157
 
56 irasnyd 158
        SortMethods.ImprovedBubbleSort(array); //sort the array
54 irasnyd 159
 
56 irasnyd 160
        //print the output line
54 irasnyd 161
        System.out.println( generateOutputLine("Impr. BubbleSort"));
162
    }
163
 
56 irasnyd 164
    //method to run SelectionSort and print the output
54 irasnyd 165
    public void testSelectionSort( Entry[] array ) {
56 irasnyd 166
        resetSwapCount(); //reset counting variables
54 irasnyd 167
        resetCompCount();
168
 
56 irasnyd 169
        SortMethods.SelectionSort(array); //sort the array
170
 
171
        //print the output line
54 irasnyd 172
        System.out.println( generateOutputLine("SelectionSort"));
173
    }
174
 
56 irasnyd 175
    //method to run InsertionSort and print the output
54 irasnyd 176
    public void testInsertionSort( Entry[] array ) {
56 irasnyd 177
        resetSwapCount(); //reset counting variables
54 irasnyd 178
        resetCompCount();
179
 
56 irasnyd 180
        SortMethods.InsertionSort(array); //sort the array
54 irasnyd 181
 
56 irasnyd 182
        //print the output line
54 irasnyd 183
        System.out.println( generateOutputLine("InsertionSort"));
184
    }
185
 
56 irasnyd 186
    //method to run ShellSort and print the output line
54 irasnyd 187
    public void testShellSort( Entry[] array ) {
56 irasnyd 188
        resetSwapCount(); //reset counting variables
54 irasnyd 189
        resetCompCount();
190
 
56 irasnyd 191
        SortMethods.ShellSort(array); //sort the array
54 irasnyd 192
 
56 irasnyd 193
        //print the output line
54 irasnyd 194
        System.out.println( generateOutputLine("ShellSort"));
195
    }
196
 
56 irasnyd 197
    //method to run the MergeSort and print the output line
54 irasnyd 198
    public void testMergeSort( Entry[] array ) {
56 irasnyd 199
        resetSwapCount(); //reset counting variables
54 irasnyd 200
        resetCompCount();
201
 
56 irasnyd 202
        SortMethods.MergeSort(array); //sort the array
54 irasnyd 203
 
56 irasnyd 204
        //print the output line for MergeSort
54 irasnyd 205
        System.out.println( generateOutputLine("MergeSort"));
206
    }
207
 
56 irasnyd 208
    //method to run the HeapSort and print the output line
54 irasnyd 209
    public void testHeapSort( Entry[] array ) {
56 irasnyd 210
        resetSwapCount(); //reset counting variables
54 irasnyd 211
        resetCompCount();
212
 
56 irasnyd 213
        SortMethods.HeapSort(array); //sort the array
54 irasnyd 214
 
56 irasnyd 215
        //print the output line for HeapSort
54 irasnyd 216
        System.out.println( generateOutputLine("HeapSort"));
217
    }
218
 
56 irasnyd 219
    //method to run QuickSort on the array, and print the output line
54 irasnyd 220
    public void testQuickSort( Entry[] array ) { 
56 irasnyd 221
        resetSwapCount(); //reset counting variables
54 irasnyd 222
        resetCompCount();
223
 
56 irasnyd 224
        SortMethods.QuickSort(array); //sort the array
54 irasnyd 225
 
56 irasnyd 226
        //print the output line for QuickSort
54 irasnyd 227
        System.out.println( generateOutputLine("QuickSort"));
228
    }
229
 
56 irasnyd 230
    //a generic method that will print out the header given to it
231
    // as a parameter, then run the suite of tests on the given array
55 irasnyd 232
    public void runTest( Entry[] array, String testHeader ) {
56 irasnyd 233
        //make 6 arrays (copies)
54 irasnyd 234
        Entry[] arrayc1 = new Entry[array.length];
235
        Entry[] arrayc2 = new Entry[array.length];
236
        Entry[] arrayc3 = new Entry[array.length];
237
        Entry[] arrayc4 = new Entry[array.length];
238
        Entry[] arrayc5 = new Entry[array.length];
239
        Entry[] arrayc6 = new Entry[array.length];
55 irasnyd 240
        Entry[] arrayc7 = new Entry[array.length];
54 irasnyd 241
 
242
        //fill the copies from the original
243
        System.arraycopy(array,0,arrayc1,0,array.length);
244
        System.arraycopy(array,0,arrayc2,0,array.length);
245
        System.arraycopy(array,0,arrayc3,0,array.length);
246
        System.arraycopy(array,0,arrayc4,0,array.length);
247
        System.arraycopy(array,0,arrayc5,0,array.length);
248
        System.arraycopy(array,0,arrayc6,0,array.length);
55 irasnyd 249
        System.arraycopy(array,0,arrayc7,0,array.length);
54 irasnyd 250
 
56 irasnyd 251
        //print both headers
55 irasnyd 252
        System.out.println(testHeader);
253
        System.out.println( generateHeader() );
54 irasnyd 254
 
56 irasnyd 255
        //run each test on one copy of the original array
256
        testBubbleSort(array);              //BubbleSort
257
        testImprovedBubbleSort(arrayc1);    //ImprovedBubbleSort
258
        testSelectionSort(arrayc2);         //SelectionSort
259
        testInsertionSort(arrayc3);         //InsertionSort
260
        testShellSort(arrayc4);             //ShellSort (AKA Comb Sort)
261
        testMergeSort(arrayc5);             //MergeSort
262
        testHeapSort(arrayc6);              //HeapSort
263
        testQuickSort(arrayc7);             //QuickSort
54 irasnyd 264
    }
55 irasnyd 265
 
56 irasnyd 266
    //run the test for 512 random elements
55 irasnyd 267
    public void test1() {
268
        Entry[] array = generateNumbers(512,0);
269
        String header = "Test 1: Array Length 512 Composed of Random Elements";
270
        runTest(array,header);
271
    }
56 irasnyd 272
 
273
    //run the test for 1024 random elements
55 irasnyd 274
    public void test2() {
275
        Entry[] array = generateNumbers(1024,0);
276
        String header = "Test 2: Array Length 1024 Composed of Random Elements";
277
        runTest(array,header);
278
    }
279
 
56 irasnyd 280
    //run the test for 2048 random elements
55 irasnyd 281
    public void test3() {
282
        Entry[] array = generateNumbers(2048,0);
283
        String header = "Test 3: Array Length 2048 Composed of Random Elements";
284
        runTest(array,header);
285
    }
56 irasnyd 286
 
287
    //run the test for 4096 random elements
55 irasnyd 288
    public void test4() {
289
        Entry[] array = generateNumbers(4096,0);
290
        String header = "Test 4: Array Length 4096 Composed of Random Elements";
291
        runTest(array,header);
292
    }
293
 
56 irasnyd 294
    //run the test for 1024 Ascending elements (0,1,2 ... 1023,1024)
55 irasnyd 295
    public void test5() {
296
        Entry[] array = generateNumbers(1024,1);
297
        String header = "Test 5: Array Length 1024 Composed of Ascending Elements";
298
        runTest(array,header);
299
    }
300
 
56 irasnyd 301
    //run the test for 1024 Descending elements (1024,1023 ... 2,1,0)
55 irasnyd 302
    public void test6() {
303
        Entry[] array = generateNumbers(1024,2);
304
        String header = "Test 6: Array Length 1024 Composed of Descending Elements";
305
        runTest(array,header);
306
    }
56 irasnyd 307
 
308
    //run the test for 1024 elements that all have the value 1.0
55 irasnyd 309
    public void test7() {
310
        Entry[] array = generateNumbers(1024,3);
311
        String header = "Test 7: Array Length 1024 Composed of Elements of value 1.0";
312
        runTest(array,header);
313
    }
51 irasnyd 314
}
315