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