Skip to content

Commit 12bf1d9

Browse files
committed
Make main methods instantiate the surrounding class
1 parent cd6ea8b commit 12bf1d9

10 files changed

+65
-25
lines changed

src/main/java/eu/happycoders/sort/CountOperations.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,25 @@ public class CountOperations {
2020
private static final int MAX_COUNTING_TIME_SECS = 20;
2121

2222
public static void main(String[] args) {
23+
new CountOperations().run();
24+
}
25+
26+
private void run() {
2327
for (SortAlgorithm algorithm : UltimateTest.ALGORITHMS) {
2428
if (algorithm.supportsCounting()) {
2529
countOps(algorithm);
2630
}
2731
}
2832
}
2933

30-
private static void countOps(SortAlgorithm algorithm) {
34+
private void countOps(SortAlgorithm algorithm) {
3135
// Test with a random, a sorted, and a reversed (= sorted descending) array
3236
countOps(algorithm, false, "random", ArrayUtils::createRandomArray);
3337
countOps(algorithm, true, "ascending", ArrayUtils::createSortedArray);
3438
countOps(algorithm, true, "descending", ArrayUtils::createReversedArray);
3539
}
3640

37-
private static void countOps(
41+
private void countOps(
3842
SortAlgorithm algorithm,
3943
boolean sorted,
4044
String inputOrder,
@@ -67,7 +71,7 @@ private static void countOps(
6771
}
6872
}
6973

70-
private static Counters countOps(SortAlgorithm algorithm, int[] elements) {
74+
private Counters countOps(SortAlgorithm algorithm, int[] elements) {
7175
Counters counters = new Counters();
7276
algorithm.sort(elements, counters);
7377
return counters;

src/main/java/eu/happycoders/sort/FindMinimumTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@
1111
public class FindMinimumTest {
1212
private static final int NUM_SIZES = 29;
1313
private static final int NUM_TESTS = 100;
14-
private static final int[] counts = new int[NUM_SIZES];
14+
private final int[] counts = new int[NUM_SIZES];
1515

1616
public static void main(String[] args) {
17+
new FindMinimumTest().run();
18+
}
19+
20+
private void run() {
1721
for (int i = 0; i < NUM_TESTS; i++) {
1822
test();
1923
printResults(i + 1);
2024
}
2125
}
2226

23-
private static void test() {
27+
private void test() {
2428
for (int i = 0; i < NUM_SIZES; i++) {
2529
int size = 2 << i;
2630
int assignments = countAssignmentsForSize(size);
2731
counts[i] += assignments;
2832
}
2933
}
3034

31-
private static int countAssignmentsForSize(int size) {
35+
private int countAssignmentsForSize(int size) {
3236
int[] array = ArrayUtils.createRandomArray(size);
3337
int min = Integer.MAX_VALUE;
3438
int assignments = 0;
@@ -42,7 +46,7 @@ private static int countAssignmentsForSize(int size) {
4246
return assignments;
4347
}
4448

45-
private static void printResults(int iterations) {
49+
private void printResults(int iterations) {
4650
System.out.printf(Locale.US, "Results after %d iterations:%n", iterations);
4751
for (int i = 0; i < NUM_SIZES; i++) {
4852
int size = 2 << i;

src/main/java/eu/happycoders/sort/UltimateTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ public class UltimateTest {
6464

6565
private static final boolean TEST_SORTED_INPUT = true;
6666

67-
private static final Map<String, Scorecard> scorecards = new HashMap<>();
67+
private final Map<String, Scorecard> scorecards = new HashMap<>();
6868

6969
public static void main(String[] args) {
70+
new UltimateTest().run();
71+
}
72+
73+
private void run() {
7074
for (int i = 1; i <= WARM_UPS; i++) {
7175
System.out.printf("%n===== Warm up %d of %d =====%n", i, WARM_UPS);
7276
for (SortAlgorithm algorithm : ALGORITHMS) {
@@ -90,7 +94,7 @@ public static void main(String[] args) {
9094
}
9195
}
9296

93-
private static void test(SortAlgorithm algorithm, boolean warmingUp) {
97+
private void test(SortAlgorithm algorithm, boolean warmingUp) {
9498
// Test with a random, a sorted, and a reversed (= sorted descending) array
9599
test(algorithm, InputOrder.RANDOM, ArrayUtils::createRandomArray, warmingUp);
96100

@@ -100,7 +104,7 @@ private static void test(SortAlgorithm algorithm, boolean warmingUp) {
100104
}
101105
}
102106

103-
private static void test(
107+
private void test(
104108
SortAlgorithm algorithm,
105109
InputOrder inputOrder,
106110
Function<Integer, int[]> arraySupplier,
@@ -134,20 +138,20 @@ private static void test(
134138
}
135139
}
136140

137-
private static long measureTime(SortAlgorithm algorithm, int[] elements) {
141+
private long measureTime(SortAlgorithm algorithm, int[] elements) {
138142
System.gc();
139143
long time = System.nanoTime();
140144
algorithm.sort(elements);
141145
return System.nanoTime() - time;
142146
}
143147

144-
private static Scorecard scorecard(
148+
private Scorecard scorecard(
145149
SortAlgorithm algorithm, String inputOrder, int size, boolean create) {
146150
String key = algorithm.getName() + "/" + inputOrder + "/" + size;
147151
return create ? scorecards.computeIfAbsent(key, Scorecard::new) : scorecards.get(key);
148152
}
149153

150-
private static void printResults(int iteration, SortAlgorithm algorithm, String inputOrder) {
154+
private void printResults(int iteration, SortAlgorithm algorithm, String inputOrder) {
151155
System.out.printf(
152156
"%n--- Results for iteration %d for: %s (order: %s) ---%n",
153157
iteration, algorithm.getName(), inputOrder);

src/main/java/eu/happycoders/sort/comparisons/CompareBubbleSorts.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ public class CompareBubbleSorts extends DirectComparison {
1919
private static final int SIZE = 40_000; // ~500 ms for Bubble Sort
2020

2121
public static void main(String[] args) {
22+
new CompareBubbleSorts().run();
23+
}
24+
25+
private void run() {
2226
List<SortAlgorithm> algorithms =
2327
List.of(
2428
new BubbleSort(),
2529
new BubbleSortOpt1(),
2630
new BubbleSortOpt2(),
2731
new BubbleSortParallelOddEven(),
2832
new BubbleSortParallelDivideAndConquer());
33+
2934
runTest(algorithms.toArray(SortAlgorithm[]::new), SIZE);
3035
}
3136
}

src/main/java/eu/happycoders/sort/comparisons/CompareImprovedDualPivotQuicksort.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class CompareImprovedDualPivotQuicksort extends DirectComparison {
1717
private static final int SIZE = 5_555_555; // ~500 ms for Quicksort
1818

1919
public static void main(String[] args) {
20+
new CompareImprovedDualPivotQuicksort().run();
21+
}
22+
23+
private void run() {
2024
List<SortAlgorithm> algorithms = new ArrayList<>();
2125
algorithms.add(new DualPivotQuicksort(DualPivotQuicksort.PivotStrategy.THIRDS));
2226
for (int threshold = 2; threshold < 1 << 8; threshold <<= 1) {
@@ -32,6 +36,7 @@ public static void main(String[] args) {
3236
threshold + threshold / 2, DualPivotQuicksort.PivotStrategy.THIRDS));
3337
}
3438
}
39+
3540
runTest(algorithms.toArray(SortAlgorithm[]::new), SIZE);
3641
}
3742
}

src/main/java/eu/happycoders/sort/comparisons/CompareImprovedQuicksort.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class CompareImprovedQuicksort extends DirectComparison {
2020
private static final int SIZE = 5_555_555; // ~500 ms for Quicksort
2121

2222
public static void main(String[] args) {
23+
new CompareImprovedQuicksort().run();
24+
}
25+
26+
private void run() {
2327
List<SortAlgorithm> algorithms = new ArrayList<>();
2428
algorithms.add(new QuicksortVariant1(PivotStrategy.MIDDLE));
2529
algorithms.add(new QuicksortVariant1(PivotStrategy.MEDIAN3));
@@ -42,7 +46,7 @@ public static void main(String[] args) {
4246
runTest(algorithms.toArray(SortAlgorithm[]::new), SIZE);
4347
}
4448

45-
private static void addAllVariantsForThreshold(List<SortAlgorithm> algorithms, int threshold) {
49+
private void addAllVariantsForThreshold(List<SortAlgorithm> algorithms, int threshold) {
4650
// Variant 1
4751
algorithms.add(new QuicksortImproved(threshold, new QuicksortVariant1(PivotStrategy.MIDDLE)));
4852
algorithms.add(new QuicksortImproved(threshold, new QuicksortVariant1(PivotStrategy.MEDIAN3)));

src/main/java/eu/happycoders/sort/comparisons/CompareMergeSorts.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ public class CompareMergeSorts extends DirectComparison {
1515
private static final int SIZE = 4_444_444; // ~600 ms for Merge Sort
1616

1717
public static void main(String[] args) {
18+
new CompareMergeSorts().run();
19+
}
20+
21+
private void run() {
1822
SortAlgorithm[] algorithms =
1923
new SortAlgorithm[] {new MergeSort(), new MergeSort2(), new MergeSort3()};
24+
2025
runTest(algorithms, SIZE);
2126
}
2227
}

src/main/java/eu/happycoders/sort/comparisons/CompareQuicksorts.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class CompareQuicksorts extends DirectComparison {
1919
private static final int SIZE = 5_555_555; // ~500 ms for Quicksort
2020

2121
public static void main(String[] args) {
22+
new CompareQuicksorts().run();
23+
}
24+
25+
private void run() {
2226
List<SortAlgorithm> algorithms = new ArrayList<>();
2327

2428
algorithms.add(new QuicksortSimple());

src/main/java/eu/happycoders/sort/comparisons/DirectComparison.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public class DirectComparison {
1818
private static final int ITERATIONS = 100;
1919
private static final int PRINT_RESULTS_ALL_X_ITERATIONS = 10;
2020

21-
private static final Map<String, Scorecard> scorecards = new HashMap<>();
21+
private final Map<String, Scorecard> scorecards = new HashMap<>();
2222

23-
private static int longestNameLength;
23+
private int longestNameLength;
2424

25-
public static void runTest(SortAlgorithm[] algorithms, int size) {
25+
public void runTest(SortAlgorithm[] algorithms, int size) {
2626
longestNameLength = Scorecard.findLongestAlgorithmName(algorithms);
2727

2828
int numAlgorithms = algorithms.length;
@@ -69,7 +69,7 @@ private static int[] getShuffledIndices(int numTestSets) {
6969
return indices;
7070
}
7171

72-
private static long measure(SortAlgorithm sortAlgorithm, int[] elements) {
72+
private long measure(SortAlgorithm sortAlgorithm, int[] elements) {
7373
System.gc();
7474

7575
long time = System.nanoTime();
@@ -89,11 +89,11 @@ private static long measure(SortAlgorithm sortAlgorithm, int[] elements) {
8989
return time;
9090
}
9191

92-
private static Scorecard scorecardForAlgorithm(SortAlgorithm algorithm) {
92+
private Scorecard scorecardForAlgorithm(SortAlgorithm algorithm) {
9393
return scorecards.computeIfAbsent(algorithm.getName(), Scorecard::new);
9494
}
9595

96-
private static void printResults(SortAlgorithm[] algorithms, int iterations) {
96+
private void printResults(SortAlgorithm[] algorithms, int iterations) {
9797
System.out.printf("%n----- Results after %d iterations-----%n", iterations);
9898

9999
// 1. Find the fastest median

src/main/java/eu/happycoders/sort/pivot/PivotComparator.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,27 @@ public class PivotComparator {
2525
private static final int MIN_SIZE = 500;
2626
private static final int MAX_SIZE = 1_000;
2727

28-
private static final Map<String, PivotScorecard> scorecards = new HashMap<>();
28+
private final Map<String, PivotScorecard> scorecards = new HashMap<>();
2929

30-
private static int longestNameLength;
30+
private int longestNameLength;
3131

3232
public static void main(String[] args) {
33+
new PivotComparator().run();
34+
}
35+
36+
private void run() {
3337
PartitioningAlgorithm[] algorithms =
3438
new PartitioningAlgorithm[] {
3539
new QuicksortVariant1(PivotStrategy.MIDDLE),
3640
new QuicksortVariant1(PivotStrategy.RANDOM),
3741
new QuicksortVariant1(PivotStrategy.RIGHT),
3842
new QuicksortVariant1(PivotStrategy.MEDIAN3)
3943
};
44+
4045
runTest(algorithms);
4146
}
4247

43-
private static void runTest(PartitioningAlgorithm[] algorithms) {
48+
private void runTest(PartitioningAlgorithm[] algorithms) {
4449
longestNameLength = Scorecard.findLongestAlgorithmName(algorithms);
4550

4651
int numAlgorithms = algorithms.length;
@@ -78,7 +83,7 @@ private static void runTest(PartitioningAlgorithm[] algorithms) {
7883
* @return the percentage of the larger partition; e.g., if we get 100 and 50 elements, the result
7984
* is 66.7.
8085
*/
81-
private static double partition(PartitioningAlgorithm algorithm, int[] elements) {
86+
private double partition(PartitioningAlgorithm algorithm, int[] elements) {
8287
int numElements = elements.length;
8388
int pivotPos = algorithm.partition(elements, 0, numElements - 1);
8489

@@ -87,7 +92,7 @@ private static double partition(PartitioningAlgorithm algorithm, int[] elements)
8792
return longerPartSize * 100.0 / (numElements - 1);
8893
}
8994

90-
private static PivotScorecard scorecardForAlgorithm(SortAlgorithm algorithm) {
95+
private PivotScorecard scorecardForAlgorithm(SortAlgorithm algorithm) {
9196
return scorecards.computeIfAbsent(algorithm.getName(), PivotScorecard::new);
9297
}
9398
}

0 commit comments

Comments
 (0)