51 |
irasnyd |
1 |
// Written by Ira Snyder
|
|
|
2 |
|
|
|
3 |
import java.io.*;
|
54 |
irasnyd |
4 |
import java.util.Random;
|
51 |
irasnyd |
5 |
|
|
|
6 |
public class Test {
|
53 |
irasnyd |
7 |
private static int compCount, swapCount;
|
51 |
irasnyd |
8 |
|
53 |
irasnyd |
9 |
private class Entry implements Comparable {
|
51 |
irasnyd |
10 |
public int x;
|
|
|
11 |
|
|
|
12 |
public Entry( int x ) { this.x = x; }
|
|
|
13 |
|
53 |
irasnyd |
14 |
public int compareTo( Object o ) {
|
51 |
irasnyd |
15 |
compCount++;
|
53 |
irasnyd |
16 |
if( !(o instanceof Entry) ) throw new IllegalArgumentException();
|
|
|
17 |
|
|
|
18 |
Entry y = (Entry)o;
|
|
|
19 |
return this.x - y.x;
|
51 |
irasnyd |
20 |
}
|
|
|
21 |
|
|
|
22 |
public Entry setEntry( int x ) {
|
|
|
23 |
return new Entry(x);
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
public String toString() { return Integer.toString(x); }
|
|
|
27 |
|
|
|
28 |
} //end Entry class
|
|
|
29 |
|
|
|
30 |
public int getCompCount() { return compCount; }
|
|
|
31 |
public int getSwapCount() { return swapCount; }
|
|
|
32 |
|
|
|
33 |
public void resetCompCount() { compCount = 0; }
|
|
|
34 |
public void resetSwapCount() { swapCount = 0; }
|
|
|
35 |
|
53 |
irasnyd |
36 |
//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
|
|
|
38 |
//possible in java. (I've come up with a very solid
|
|
|
39 |
//explanation of this. See me if you want to know.)
|
|
|
40 |
public static void swap( Object[] array, int posA, int posB ) {
|
|
|
41 |
Object temp = array[posB]; // temp = B
|
|
|
42 |
array[posB] = array[posA]; // B = A
|
|
|
43 |
array[posA] = temp; // A = temp
|
|
|
44 |
swapCount++;
|
|
|
45 |
}
|
51 |
irasnyd |
46 |
|
53 |
irasnyd |
47 |
public void printArray( Object[] array, PrintStream ps ) {
|
|
|
48 |
int i=0;
|
|
|
49 |
for( i=0; i<array.length-1; i++ ) { System.out.print(array[i] + ", "); }
|
|
|
50 |
System.out.println(array[i]); //print the last element
|
|
|
51 |
}
|
54 |
irasnyd |
52 |
|
|
|
53 |
// howMany should be how many numbers you want to generate
|
|
|
54 |
// order: 0 = random
|
|
|
55 |
// 1 = ascending
|
|
|
56 |
// 2 = descending
|
|
|
57 |
public Entry[] generateNumbers( int howMany, int order ) {
|
|
|
58 |
Random generator = new Random();
|
|
|
59 |
Entry[] answer = new Entry[howMany];
|
53 |
irasnyd |
60 |
|
54 |
irasnyd |
61 |
if( order < 0 || order > 2 ) throw new IllegalArgumentException();
|
|
|
62 |
|
|
|
63 |
for( int i=0; i<howMany; i++ ) {
|
|
|
64 |
//I deemed random int's from 0 to 10239 to be a good test
|
|
|
65 |
if( order == 0 ) { answer[i] = new Entry(generator.nextInt(10240)); }
|
|
|
66 |
else if( order == 1) { answer[i] = new Entry(i); } //ascending
|
|
|
67 |
else { answer[i] = new Entry(howMany-i-1); } //descending
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
return answer;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private StringBuffer generateBlankSB() {
|
|
|
74 |
return new StringBuffer(" ");
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
private String generateOutputLine( String name ) {
|
|
|
78 |
StringBuffer output = new StringBuffer(name); //put the name
|
|
|
79 |
StringBuffer spaces = new StringBuffer();
|
51 |
irasnyd |
80 |
|
54 |
irasnyd |
81 |
for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }
|
|
|
82 |
output.append(spaces);
|
|
|
83 |
output.append(getCompCount()); //appends the current number of comps
|
51 |
irasnyd |
84 |
|
54 |
irasnyd |
85 |
spaces = new StringBuffer();
|
|
|
86 |
for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }
|
|
|
87 |
output.append(spaces);
|
|
|
88 |
output.append(getSwapCount()); //appends the current number of swaps
|
|
|
89 |
|
|
|
90 |
return output.toString();
|
51 |
irasnyd |
91 |
}
|
54 |
irasnyd |
92 |
|
|
|
93 |
|
|
|
94 |
public void testBubbleSort( Entry[] array ) {
|
|
|
95 |
resetSwapCount();
|
|
|
96 |
resetCompCount();
|
|
|
97 |
|
|
|
98 |
SortMethods.BubbleSort(array);
|
|
|
99 |
|
|
|
100 |
System.out.println( generateOutputLine("BubbleSort"));
|
|
|
101 |
}
|
51 |
irasnyd |
102 |
|
54 |
irasnyd |
103 |
public void testImprovedBubbleSort( Entry[] array ) {
|
|
|
104 |
resetSwapCount();
|
|
|
105 |
resetCompCount();
|
|
|
106 |
|
|
|
107 |
SortMethods.ImprovedBubbleSort(array);
|
|
|
108 |
|
|
|
109 |
System.out.println( generateOutputLine("Impr. BubbleSort"));
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public void testSelectionSort( Entry[] array ) {
|
|
|
113 |
resetSwapCount();
|
|
|
114 |
resetCompCount();
|
|
|
115 |
|
|
|
116 |
SortMethods.SelectionSort(array);
|
|
|
117 |
|
|
|
118 |
System.out.println( generateOutputLine("SelectionSort"));
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public void testInsertionSort( Entry[] array ) {
|
|
|
122 |
resetSwapCount();
|
|
|
123 |
resetCompCount();
|
|
|
124 |
|
|
|
125 |
SortMethods.InsertionSort(array);
|
|
|
126 |
|
|
|
127 |
System.out.println( generateOutputLine("InsertionSort"));
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public void testShellSort( Entry[] array ) {
|
|
|
131 |
resetSwapCount();
|
|
|
132 |
resetCompCount();
|
|
|
133 |
|
|
|
134 |
SortMethods.ShellSort(array);
|
|
|
135 |
|
|
|
136 |
System.out.println( generateOutputLine("ShellSort"));
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public void testMergeSort( Entry[] array ) {
|
|
|
140 |
resetSwapCount();
|
|
|
141 |
resetCompCount();
|
|
|
142 |
|
|
|
143 |
SortMethods.MergeSort(array);
|
|
|
144 |
|
|
|
145 |
System.out.println( generateOutputLine("MergeSort"));
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public void testHeapSort( Entry[] array ) {
|
|
|
149 |
resetSwapCount();
|
|
|
150 |
resetCompCount();
|
|
|
151 |
|
|
|
152 |
SortMethods.HeapSort(array);
|
|
|
153 |
|
|
|
154 |
System.out.println( generateOutputLine("HeapSort"));
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public void testQuickSort( Entry[] array ) {
|
|
|
158 |
resetSwapCount();
|
|
|
159 |
resetCompCount();
|
|
|
160 |
|
|
|
161 |
SortMethods.QuickSort(array);
|
|
|
162 |
|
|
|
163 |
System.out.println( generateOutputLine("QuickSort"));
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public void runTest() {
|
|
|
167 |
|
|
|
168 |
//make 6 arrays
|
|
|
169 |
Entry[] array = generateNumbers(512,0);
|
|
|
170 |
Entry[] arrayc1 = new Entry[array.length];
|
|
|
171 |
Entry[] arrayc2 = new Entry[array.length];
|
|
|
172 |
Entry[] arrayc3 = new Entry[array.length];
|
|
|
173 |
Entry[] arrayc4 = new Entry[array.length];
|
|
|
174 |
Entry[] arrayc5 = new Entry[array.length];
|
|
|
175 |
Entry[] arrayc6 = new Entry[array.length];
|
|
|
176 |
|
|
|
177 |
//fill the copies from the original
|
|
|
178 |
System.arraycopy(array,0,arrayc1,0,array.length);
|
|
|
179 |
System.arraycopy(array,0,arrayc2,0,array.length);
|
|
|
180 |
System.arraycopy(array,0,arrayc3,0,array.length);
|
|
|
181 |
System.arraycopy(array,0,arrayc4,0,array.length);
|
|
|
182 |
System.arraycopy(array,0,arrayc5,0,array.length);
|
|
|
183 |
System.arraycopy(array,0,arrayc6,0,array.length);
|
|
|
184 |
|
|
|
185 |
System.out.println("Test Case 1: Array Length 512 Composed of Random Elements");
|
|
|
186 |
StringBuffer header = generateBlankSB();
|
|
|
187 |
header.insert(0,"Method");
|
|
|
188 |
header.insert(20,"Number Compares");
|
|
|
189 |
header.insert(48,"Number Swaps");
|
|
|
190 |
System.out.println(header);
|
|
|
191 |
|
|
|
192 |
testBubbleSort(array);
|
|
|
193 |
testImprovedBubbleSort(arrayc1);
|
|
|
194 |
testSelectionSort(arrayc2);
|
|
|
195 |
testInsertionSort(arrayc3);
|
|
|
196 |
testShellSort(arrayc4);
|
|
|
197 |
testMergeSort(arrayc5);
|
|
|
198 |
testQuickSort(arrayc6);
|
|
|
199 |
}
|
51 |
irasnyd |
200 |
}
|
|
|
201 |
|