-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortRevisitAnalytics.java
121 lines (105 loc) · 3.81 KB
/
MergeSortRevisitAnalytics.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
import edu.princeton.cs.algs4.StdRandom;
import java.util.Arrays;
public class MergeSortRevisitAnalytics {
private static Comparable[] items;
private static MergeSortRevisit sorter;
private static boolean newSort;
private static boolean sorted;
private static SortAnalyzer analyzer;
private static int inputType;
public static void sort(MergeSortRevisit ms, Comparable[] items, int inType) {
newSort = true;
inputType = inType;
sorter = ms;
sorter.sort(items);
sorted = true;
}
public static void sort(MergeSortRevisit ms, Comparable[] items) { // random input
sort(ms, items, 1);
}
private static Integer[] sample(int n) {
Integer[] out = new Integer[n];
for (int i = 0; i < n; i++) {
out[i] = StdRandom.uniformInt(-1000, 1000);
}
return out;
}
private static void resort() {
if (!sorted) {
throw new UnsupportedOperationException("Can only call resort() after items have been sorted.");
}
analyzer.refresh();
inputType = 0;
sort(sorter, items, inputType);
newSort = false;
}
private static float growthConstant(int statType) {
float r;
int N = items.length;
float nLogN = (float) (N * Math.log(N) / Math.log(2));
switch (statType) {
case 1: // compares
r = (float) analyzer.getCompares() / nLogN;
break;
case 2: // accesses
r = (float) analyzer.getAccesses() / (6*nLogN);
break;
default: // total
r = (float) analyzer.total() / (7*nLogN);
break;
}
return r;
}
private static void analyze() {
if (!sorted) System.out.println("[!] items have NOT been sorted.");
String[] exps;;
String caseName;
switch (inputType) {
case 0: // best
caseName = "[BEST] Total / (7*N*lg(N)) \t Compares / (N*lg(N)) \t Accesses / (6*N*log(N))";
exps = new String[]{"~1", "~1", "~1"};
break;
case 2: // worst
caseName = "[WORST] Total / (7*N*lg(N)) \t Compares / (N*log(N)) \t Accesses / (6*N*log(N))";
exps = new String[]{"~1", "~1", "~1"};
break;
default: // random
caseName = "[RAND] Total / (7*N*lg(N)) \t Compares / (N*log(N)) \t Accesses / (6*N*log(N))";
exps = new String[]{"~1", "~1", "~1"};
break;
}
int N = items.length;
System.out.printf("[*] N: %d, N*log(N): %f, comp.: %d, acc.: %d, total: %d\n" +
"\t%s\n\t\t(Exp. %s): %f" +
"\t(Exp. %s): %f" +
"\t(Exp. %s): %f\n\n",
N, N*Math.log(N)/Math.log(2), analyzer.getCompares(),
analyzer.getAccesses(), analyzer.total(), caseName,
exps[0], growthConstant(0),
exps[1], growthConstant(1),
exps[2], growthConstant(2));
}
public static void main(String[] args) {
analyzer = new SortAnalyzer();
MergeSortRevisit sorter = new MergeSortRevisit(analyzer);
items = new Integer[]{3, 2, 10, 15, 9, 4, -2, 0, 102, -12, 88, 50,
22, 19, -1, 10, 31, 7, 22};
sort(sorter, items);
System.out.printf("[sorted] items: %s\n", Arrays.toString(items));
analyze();
// worst case
int N = 500;
items = new Integer[N];
for (int i = 0; i < N; i++) {
items[N-1-i] = i;
}
sort(sorter, items, 2);
analyze();
items = sample(500);
sort(sorter, items);
analyze();
// best case
resort();
analyze();
}
}