-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSorts.java
399 lines (339 loc) · 8.34 KB
/
Sorts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.util.Arrays;
import java.util.Random;
/**
* Timings of various sorting algorithms.
*
* Sorry for the interface not being very intuitive... hopefully if you can read
* the code, you can work out how to run it.
*
* I suggest that you redirect the output to a file (with a .csv extension) and
* open with a spreadsheet. Or plot in GnuPlot!
*
* eg run with "java Sorts 6 > quicksort.csv"
*
* You will probably have to kill the process (with ctrl-c) unless you fancy
* waiting forever. Even if you decrease the max value of inputSize, you'll
* still be waiting a long time, especially for bogosort and bozosort.
*
* @author Alex Pinkney
* @since 2013-06-04
*
*
*/
public class Sorts {
private static Random r = new Random(System.currentTimeMillis());
public static boolean isSorted(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Error: need integer argument (0-11)");
System.exit(-1);
}
int type = Integer.parseInt(args[0]);
System.err.println("Running sort #" + type);
for (int inputSize = 1; inputSize < 1000; inputSize++) {
int totalRuns = 1000;
System.out.print(inputSize);
// System.err.println(inputSize);
long totalruntime = 0;
for (int run = 0; run < totalRuns; run++) {
int[] nums = new int[inputSize];
for (int i = 0; i < nums.length; i++) {
nums[i] = r.nextInt(5 * inputSize);
}
long time = System.nanoTime();
switch (type) {
case 0:
bubbleSort(nums);
break;
case 1:
bubbleSortBetter(nums);
break;
case 2:
bubbleSortBest(nums);
break;
case 3:
cocktailSort(nums);
break;
case 4:
selectionSort(nums);
break;
case 5:
mergeSort(nums);
break;
case 6:
quickSort(nums);
break;
case 7:
heapSort(nums);
break;
case 8:
Arrays.sort(nums); // Java's default sorting algorithm (Timsort?)
break;
case 9:
bogoSort(nums);
break;
case 10:
bozoSort(nums);
break;
case 11:
quantumBogoSort(nums);
break;
default:
System.err.printf("\nBad sort ID '%d'", type);
System.exit(-2);
}
// For testing purposes
// if (!isSorted(nums)) {
// System.err.printf("Sort %d is broken:\n%s\n", type,
// Arrays.toString(nums));
// }
totalruntime += System.nanoTime() - time;
}
System.out.println(", " + (totalruntime / totalRuns));
}
}
// ***************************** Bubble Sorts *****************************
// Checks all values on each pass
public static void bubbleSort(int[] nums) {
boolean swaps;
do {
swaps = false;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
swaps = true;
}
}
} while (swaps);
}
// Checks one fewer value each pass
public static void bubbleSortBetter(int[] nums) {
boolean swaps;
int topsorted = 1;
do {
swaps = false;
for (int i = 0; i < nums.length - topsorted; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
swaps = true;
}
}
topsorted++;
} while (swaps);
}
// Doesn't check anything after the top swap on the last pass
public static void bubbleSortBest(int[] nums) {
int topswap = nums.length - 1;
do {
int newtopswap = 0;
for (int i = 0; i < topswap; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
newtopswap = i;
}
}
topswap = newtopswap;
} while (topswap > 0);
}
// Like the above, but does passes in alternate directions
public static void cocktailSort(int[] nums) {
int topswap = nums.length - 1;
int botswap = 0;
do {
int newtopswap = 0;
for (int i = botswap; i < topswap; i++) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
newtopswap = i;
}
}
topswap = newtopswap;
int newbotswap = topswap;
for (int i = topswap - 1; i >= botswap; i--) {
if (nums[i] > nums[i + 1]) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
newbotswap = i;
}
}
botswap = newbotswap;
} while (topswap > botswap);
}
// ***************************** Merge Sort *****************************
public static void mergeSort(int[] nums) {
if (nums.length <= 1) { // base case
return;
}
// Split into 2
int middle = nums.length / 2;
int[] left = Arrays.copyOfRange(nums, 0, middle);
int[] right = Arrays.copyOfRange(nums, middle, nums.length);
// Recursive calls
mergeSort(left);
mergeSort(right);
// Merge sub-arrays back together
int l = 0;
int r = 0;
while (l + r < nums.length) {
if (l < left.length && r < right.length) {
if (left[l] < right[r]) {
nums[l + r] = left[l++];
} else {
nums[l + r] = right[r++];
}
} else if (l < left.length) {
nums[l + r] = left[l++];
} else {
nums[l + r] = right[r++];
}
}
}
// ***************************** Quicksort *****************************
public static void quickSort(int[] nums) {
quickSort(nums, 0, nums.length - 1);
}
private static void quickSort(int[] nums, int first, int last) {
if (last > first) {
int pivot = partition(nums, first, last);
quickSort(nums, first, pivot - 1);
quickSort(nums, pivot + 1, last);
}
}
private static int partition(int[] nums, int first, int last) {
int pivot = nums[first];
int low = first + 1;
int high = last;
while (high > low) {
while (low <= high && nums[low] <= pivot)
low++;
while (low <= high && nums[high] > pivot)
high--;
if (high > low) {
int temp = nums[high];
nums[high] = nums[low];
nums[low] = temp;
}
}
while (high > first && nums[high] >= pivot)
high--;
if (pivot > nums[high]) {
nums[first] = nums[high];
nums[high] = pivot;
return high;
} else {
return first;
}
}
// ***************************** Selection Sort ****************************
public static void selectionSort(int[] nums) {
int minindex;
for (int i = 0; i < nums.length - 1; i++) {
minindex = i;
for (int j = i; j < nums.length; j++) {
if (nums[j] < nums[minindex]) {
minindex = j;
}
}
if (minindex != i) {
int tmp = nums[i];
nums[i] = nums[minindex];
nums[minindex] = tmp;
}
}
}
// ***************************** Heap Sort *****************************
public static void heapSort(int[] nums) {
heapify(nums);
int end = nums.length - 1;
while (end > 0) {
int tmp = nums[end];
nums[end] = nums[0];
nums[0] = tmp;
end--;
siftDown(nums, 0, end);
}
}
private static void heapify(int[] nums) {
int start = (nums.length - 1) / 2;
while (start >= 0) {
siftDown(nums, start, nums.length - 1);
start--;
}
}
private static void siftDown(int[] nums, int start, int end) {
int root = start;
while (root * 2 + 1 <= end) {
int child = root * 2 + 1;
int swap = root;
if (nums[swap] < nums[child]) {
swap = child;
}
if (child + 1 <= end && nums[swap] < nums[child + 1]) {
swap = child + 1;
}
if (swap != root) {
int tmp = nums[root];
nums[root] = nums[swap];
nums[swap] = tmp;
root = swap;
} else {
return;
}
}
}
public static void bogoSort(int[] nums) {
while (!isSorted(nums)) {
shuffle(nums);
}
}
private static void shuffle(int[] nums) { // Knuth Shuffle
int n, tmp;
for (int i = nums.length - 1; i > 0; i--) {
n = r.nextInt(i + 1);
tmp = nums[i];
nums[i] = nums[n];
nums[n] = tmp;
}
}
public static void bozoSort(int[] nums) {
int n1, n2, tmp;
while (!isSorted(nums)) {
// swap 2 random elements
n1 = r.nextInt(nums.length);
n2 = r.nextInt(nums.length - 1);
if (n2 >= n1) {
n2++;
}
tmp = nums[n1];
nums[n1] = nums[n2];
nums[n2] = tmp;
}
}
public static void quantumBogoSort(int[] nums) {
shuffle(nums);
if (!isSorted(nums)) {
destroyUniverse();
}
}
private static void destroyUniverse() {
// TODO: Implement this...
throw new UnsupportedOperationException(
"The implementation of this method is left as an exercise to the reader");
}
}