Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
55 irasnyd 61
        if( order < 0 || order > 3 ) throw new IllegalArgumentException();
54 irasnyd 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
55 irasnyd 67
            else if( order == 2 ){ answer[i] = new Entry(howMany-i-1); } //descending
68
            else { answer[i] = new Entry(1); } //set all to 1.0
54 irasnyd 69
        }
70
 
71
        return answer;
72
    }
73
 
55 irasnyd 74
    private String generateHeader() {
75
        StringBuffer output = new StringBuffer("Method");
76
        StringBuffer spaces = new StringBuffer();
77
 
78
        for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }
79
        output.append(spaces);
80
        output.append("Number Compares");
81
 
82
        spaces = new StringBuffer();
83
        for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }
84
        output.append(spaces);
85
        output.append("Number Swaps");
86
 
87
        return output.toString();
54 irasnyd 88
    }
89
 
90
    private String generateOutputLine( String name ) {
91
        StringBuffer output = new StringBuffer(name); //put the name
92
        StringBuffer spaces = new StringBuffer();
51 irasnyd 93
 
54 irasnyd 94
        for( int i=output.length(); i<20; i++ ) { spaces.append(" "); }
95
        output.append(spaces);
96
        output.append(getCompCount()); //appends the current number of comps
51 irasnyd 97
 
54 irasnyd 98
        spaces = new StringBuffer();
99
        for( int i=output.length(); i<48; i++ ) { spaces.append(" "); }
100
        output.append(spaces);
101
        output.append(getSwapCount()); //appends the current number of swaps
102
 
103
        return output.toString();
51 irasnyd 104
    }
54 irasnyd 105
 
106
 
107
    public void testBubbleSort( Entry[] array ) {
108
        resetSwapCount();
109
        resetCompCount();
110
 
111
        SortMethods.BubbleSort(array);
112
 
113
        System.out.println( generateOutputLine("BubbleSort"));
114
    }
51 irasnyd 115
 
54 irasnyd 116
    public void testImprovedBubbleSort( Entry[] array ) {
117
        resetSwapCount();
118
        resetCompCount();
119
 
120
        SortMethods.ImprovedBubbleSort(array);
121
 
122
        System.out.println( generateOutputLine("Impr. BubbleSort"));
123
    }
124
 
125
    public void testSelectionSort( Entry[] array ) {
126
        resetSwapCount();
127
        resetCompCount();
128
 
129
        SortMethods.SelectionSort(array);
130
 
131
        System.out.println( generateOutputLine("SelectionSort"));
132
    }
133
 
134
    public void testInsertionSort( Entry[] array ) {
135
        resetSwapCount();
136
        resetCompCount();
137
 
138
        SortMethods.InsertionSort(array);
139
 
140
        System.out.println( generateOutputLine("InsertionSort"));
141
    }
142
 
143
    public void testShellSort( Entry[] array ) {
144
        resetSwapCount();
145
        resetCompCount();
146
 
147
        SortMethods.ShellSort(array);
148
 
149
        System.out.println( generateOutputLine("ShellSort"));
150
    }
151
 
152
    public void testMergeSort( Entry[] array ) {
153
        resetSwapCount();
154
        resetCompCount();
155
 
156
        SortMethods.MergeSort(array);
157
 
158
        System.out.println( generateOutputLine("MergeSort"));
159
    }
160
 
161
    public void testHeapSort( Entry[] array ) {
162
        resetSwapCount();
163
        resetCompCount();
164
 
165
        SortMethods.HeapSort(array);
166
 
167
        System.out.println( generateOutputLine("HeapSort"));
168
    }
169
 
170
    public void testQuickSort( Entry[] array ) { 
171
        resetSwapCount();
172
        resetCompCount();
173
 
174
        SortMethods.QuickSort(array);
175
 
176
        System.out.println( generateOutputLine("QuickSort"));
177
    }
178
 
55 irasnyd 179
    public void runTest( Entry[] array, String testHeader ) {
54 irasnyd 180
        //make 6 arrays
181
        Entry[] arrayc1 = new Entry[array.length];
182
        Entry[] arrayc2 = new Entry[array.length];
183
        Entry[] arrayc3 = new Entry[array.length];
184
        Entry[] arrayc4 = new Entry[array.length];
185
        Entry[] arrayc5 = new Entry[array.length];
186
        Entry[] arrayc6 = new Entry[array.length];
55 irasnyd 187
        Entry[] arrayc7 = new Entry[array.length];
54 irasnyd 188
 
189
        //fill the copies from the original
190
        System.arraycopy(array,0,arrayc1,0,array.length);
191
        System.arraycopy(array,0,arrayc2,0,array.length);
192
        System.arraycopy(array,0,arrayc3,0,array.length);
193
        System.arraycopy(array,0,arrayc4,0,array.length);
194
        System.arraycopy(array,0,arrayc5,0,array.length);
195
        System.arraycopy(array,0,arrayc6,0,array.length);
55 irasnyd 196
        System.arraycopy(array,0,arrayc7,0,array.length);
54 irasnyd 197
 
55 irasnyd 198
        System.out.println(testHeader);
199
        System.out.println( generateHeader() );
54 irasnyd 200
 
201
        testBubbleSort(array);
202
        testImprovedBubbleSort(arrayc1);
203
        testSelectionSort(arrayc2);
204
        testInsertionSort(arrayc3);
205
        testShellSort(arrayc4);
206
        testMergeSort(arrayc5);
55 irasnyd 207
        testHeapSort(arrayc6);
208
        testQuickSort(arrayc7);
54 irasnyd 209
    }
55 irasnyd 210
 
211
    public void test1() {
212
        Entry[] array = generateNumbers(512,0);
213
        String header = "Test 1: Array Length 512 Composed of Random Elements";
214
        runTest(array,header);
215
    }
216
 
217
    public void test2() {
218
        Entry[] array = generateNumbers(1024,0);
219
        String header = "Test 2: Array Length 1024 Composed of Random Elements";
220
        runTest(array,header);
221
    }
222
 
223
    public void test3() {
224
        Entry[] array = generateNumbers(2048,0);
225
        String header = "Test 3: Array Length 2048 Composed of Random Elements";
226
        runTest(array,header);
227
    }
228
 
229
    public void test4() {
230
        Entry[] array = generateNumbers(4096,0);
231
        String header = "Test 4: Array Length 4096 Composed of Random Elements";
232
        runTest(array,header);
233
    }
234
 
235
    public void test5() {
236
        Entry[] array = generateNumbers(1024,1);
237
        String header = "Test 5: Array Length 1024 Composed of Ascending Elements";
238
        runTest(array,header);
239
    }
240
 
241
    public void test6() {
242
        Entry[] array = generateNumbers(1024,2);
243
        String header = "Test 6: Array Length 1024 Composed of Descending Elements";
244
        runTest(array,header);
245
    }
246
 
247
    public void test7() {
248
        Entry[] array = generateNumbers(1024,3);
249
        String header = "Test 7: Array Length 1024 Composed of Elements of value 1.0";
250
        runTest(array,header);
251
    }
51 irasnyd 252
}
253