Subversion Repositories programming

Rev

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

Rev 54 Rev 55
Line 56... Line 56...
56
    //        2 = descending
56
    //        2 = descending
57
    public Entry[] generateNumbers( int howMany, int order ) {
57
    public Entry[] generateNumbers( int howMany, int order ) {
58
        Random generator = new Random();
58
        Random generator = new Random();
59
        Entry[] answer = new Entry[howMany];
59
        Entry[] answer = new Entry[howMany];
60
        
60
        
61
        if( order < 0 || order > 2 ) throw new IllegalArgumentException();
61
        if( order < 0 || order > 3 ) throw new IllegalArgumentException();
62
        
62
        
63
        for( int i=0; i<howMany; i++ ) {
63
        for( int i=0; i<howMany; i++ ) {
64
            //I deemed random int's from 0 to 10239 to be a good test
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)); }
65
            if( order == 0 ) { answer[i] = new Entry(generator.nextInt(10240)); }
66
            else if( order == 1) { answer[i] = new Entry(i); } //ascending
66
            else if( order == 1) { answer[i] = new Entry(i); } //ascending
67
            else { answer[i] = new Entry(howMany-i-1); } //descending
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
68
        }
69
        }
69
        
70
        
70
        return answer;
71
        return answer;
71
    }
72
    }
72
    
73
    
73
    private StringBuffer generateBlankSB() {
74
    private String generateHeader() {
74
        return new StringBuffer("                                ");
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();
75
    }
88
    }
76
    
89
    
77
    private String generateOutputLine( String name ) {
90
    private String generateOutputLine( String name ) {
78
        StringBuffer output = new StringBuffer(name); //put the name
91
        StringBuffer output = new StringBuffer(name); //put the name
79
        StringBuffer spaces = new StringBuffer();
92
        StringBuffer spaces = new StringBuffer();
Line 161... Line 174...
161
        SortMethods.QuickSort(array);
174
        SortMethods.QuickSort(array);
162
 
175
 
163
        System.out.println( generateOutputLine("QuickSort"));
176
        System.out.println( generateOutputLine("QuickSort"));
164
    }
177
    }
165
    
178
    
166
    public void runTest() {
179
    public void runTest( Entry[] array, String testHeader ) {
167
        
-
 
168
        //make 6 arrays
180
        //make 6 arrays
169
        Entry[] array = generateNumbers(512,0);
-
 
170
        Entry[] arrayc1 = new Entry[array.length];
181
        Entry[] arrayc1 = new Entry[array.length];
171
        Entry[] arrayc2 = new Entry[array.length];
182
        Entry[] arrayc2 = new Entry[array.length];
172
        Entry[] arrayc3 = new Entry[array.length];
183
        Entry[] arrayc3 = new Entry[array.length];
173
        Entry[] arrayc4 = new Entry[array.length];
184
        Entry[] arrayc4 = new Entry[array.length];
174
        Entry[] arrayc5 = new Entry[array.length];
185
        Entry[] arrayc5 = new Entry[array.length];
175
        Entry[] arrayc6 = new Entry[array.length];
186
        Entry[] arrayc6 = new Entry[array.length];
-
 
187
        Entry[] arrayc7 = new Entry[array.length];
176
        
188
        
177
        //fill the copies from the original
189
        //fill the copies from the original
178
        System.arraycopy(array,0,arrayc1,0,array.length);
190
        System.arraycopy(array,0,arrayc1,0,array.length);
179
        System.arraycopy(array,0,arrayc2,0,array.length);
191
        System.arraycopy(array,0,arrayc2,0,array.length);
180
        System.arraycopy(array,0,arrayc3,0,array.length);
192
        System.arraycopy(array,0,arrayc3,0,array.length);
181
        System.arraycopy(array,0,arrayc4,0,array.length);
193
        System.arraycopy(array,0,arrayc4,0,array.length);
182
        System.arraycopy(array,0,arrayc5,0,array.length);
194
        System.arraycopy(array,0,arrayc5,0,array.length);
183
        System.arraycopy(array,0,arrayc6,0,array.length);
195
        System.arraycopy(array,0,arrayc6,0,array.length);
-
 
196
        System.arraycopy(array,0,arrayc7,0,array.length);
184
        
197
        
185
        System.out.println("Test Case 1: Array Length 512 Composed of Random Elements");
-
 
186
        StringBuffer header = generateBlankSB();
-
 
187
        header.insert(0,"Method");
198
        System.out.println(testHeader);
188
        header.insert(20,"Number Compares");
-
 
189
        header.insert(48,"Number Swaps");
-
 
190
        System.out.println(header);
199
        System.out.println( generateHeader() );
191
 
200
 
192
        testBubbleSort(array);
201
        testBubbleSort(array);
193
        testImprovedBubbleSort(arrayc1);
202
        testImprovedBubbleSort(arrayc1);
194
        testSelectionSort(arrayc2);
203
        testSelectionSort(arrayc2);
195
        testInsertionSort(arrayc3);
204
        testInsertionSort(arrayc3);
196
        testShellSort(arrayc4);
205
        testShellSort(arrayc4);
197
        testMergeSort(arrayc5);
206
        testMergeSort(arrayc5);
-
 
207
        testHeapSort(arrayc6);
198
        testQuickSort(arrayc6);
208
        testQuickSort(arrayc7);
-
 
209
    }
-
 
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);
199
    }
251
    }
200
}
252
}
201
 
253