Subversion Repositories programming

Rev

Rev 55 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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