-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortDemo.java
98 lines (86 loc) · 3.01 KB
/
SortDemo.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
import SortingMethods.*;
import Framework.*;
import java.util.*;
import java.util.concurrent.*;
public class SortDemo {
public static final int ARRAY_LENGTH = 1024;
public static final int ARRAY_HEIGHT = 768;
static Map<String, Sorter> methods = registerSorters();
static Map<String, Sorter> registerSorters() {
// TODO(durandal): load all implementations of Sorter automatically, derive names.
Map<String, Sorter> sorters = new HashMap<String, Sorter>();
sorters.put("bidirectionalbubble", new BidirectionalBubbleSorter());
sorters.put("comb", new CombSorter());
sorters.put("bubble", new BubbleSorter());
sorters.put("exchange", new ExchangeSorter());
sorters.put("selection", new SelectionSorter());
sorters.put("counting", new CountingSorter());
sorters.put("merge", new MergeSorter());
sorters.put("shaker", new ShakerSorter());
sorters.put("insertion", new InsertionSorter());
sorters.put("shell", new ShellSorter());
sorters.put("heap", new HeapSorter());
sorters.put("quick", new QuickSorter());
sorters.put("radix", new RadixSorter());
sorters.put("binaryradix", new BinaryRadixSorter());
sorters.put("silly", new SillySorter());
sorters.put("threestooges", new ThreeStoogesSorter());
return java.util.Collections.unmodifiableMap(sorters);
}
// For parallel demos.
static ExecutorService threadPool = Executors.newCachedThreadPool();
public static void main(String[] args) {
if (args.length == 0) {
//usage();
for (String method : methods.keySet()) {
demo(method);
}
} else {
for (String method : args) {
demo(method);
}
}
}
public static void demo(final String method) {
if (!methods.containsKey(method)) usage();
threadPool.submit(new Callable<Void>() {
public Void call() {
IntegerArray ia = new AnimatedIntegerArray(ARRAY_LENGTH, ARRAY_HEIGHT, method);
randomize(ia);
ia.log("Sorting: " + method);
methods.get(method).sort(ia);
if (verify(ia)) {
ia.log("Success: " + method);
ia.destroy();
}
return null;
}
});
}
// TODO(durandal): implement other strategies for populating initial data
public static void randomize(IntegerArray ia) {
ia.log("Filling with random data...");
Random r = new Random();
for (int i = 0; i < ia.length(); i++) {
ia.write(i, r.nextInt(ia.height()));
}
}
public static boolean verify(IntegerArray ia) {
ia.log("Verifying sort...");
for (int i = 0; i < ia.length()-1; i++) {
if (ia.compare(i, i+1)) {
ia.log("Out-of-order elements found at " + i + "," + (i+1));
return false;
}
}
return true;
}
public static void usage() {
System.out.println("java SortDemo <sort-method> [<additional sort-method> ...]");
System.out.println("sort-method can be any of the following:");
for (String method : methods.keySet()) {
System.out.println("\t" + method);
}
System.exit(0);
}
}