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