-
Notifications
You must be signed in to change notification settings - Fork 6
/
Harness.java
104 lines (88 loc) · 2.99 KB
/
Harness.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class Harness {
public static void main(String[] args) {
List<Solution> solutions = new ArrayList<>();
solutions.add(new AlecSolution());
solutions.add(new SortSolution());
for (Solution solution : solutions) {
Harness harness = new Harness(solution, 10);
harness.checkAllPermutations();
harness.printReport();
}
for (Solution solution : solutions) {
Harness harness = new Harness(solution, 100);
harness.checkRandomPermutations(10_000);
harness.printReport();
}
}
private final Solution solution;
private final int arrayLength;
private final int[] timesWrong;
private final long[] sumOfSquaredDifferences;
private final List<Integer> originals;
private int comparisons;
private int permutations;
private final Comparator<Integer> comparator = (a, b) -> {
comparisons++;
return -Integer.compare(a, b);
};
private Harness(Solution solution, int arrayLength) {
this.solution = solution;
this.arrayLength = arrayLength;
this.timesWrong = new int[arrayLength];
this.sumOfSquaredDifferences = new long[arrayLength];
originals = new ArrayList<>(arrayLength);
for (int i = 0; i < arrayLength; i++) {
originals.add(i);
}
}
private void checkRandomPermutations(int trials) {
Random random = new Random(0);
for (int i = 0; i < trials; i++) {
Collections.sort(originals);
Collections.shuffle(originals, random);
measurePermutation(originals);
}
}
private void checkAllPermutations() {
permute(originals, 0);
}
private void permute(List<Integer> list, int k){
for(int i = k; i < list.size(); i++){
Collections.swap(list, i, k);
permute(list, k+1);
Collections.swap(list, k, i);
}
if (k == list.size() - 1) {
measurePermutation(list);
}
}
private void measurePermutation(List<Integer> permutation) {
permutations++;
List<Integer> sorted = solution.pizzaSort(new ArrayList<>(permutation), comparator);
for (int i = 0; i < permutation.size(); i++) {
int actual = sorted.get(i);
int expected = arrayLength - i - 1;
if (actual != expected) {
int difference = actual - expected;
timesWrong[i]++;
sumOfSquaredDifferences[i] += (difference * difference);
}
}
}
private void printReport() {
System.out.printf("%s%n", solution.getClass().getSimpleName());
for (int i = 0; i < arrayLength; i++) {
double standardDeviation = Math.sqrt((double) sumOfSquaredDifferences[i] / permutations);
System.out.printf("Got %dth place wrong %.2f%% of the time (σ=%.2f)%n",
i, ((double)timesWrong[i]/permutations)*100, standardDeviation);
}
System.out.printf("%.2f comparisons per element (%d total comparisons)%n",
((double) comparisons/permutations/arrayLength), comparisons);
System.out.println();
}
}