Sorting.java
package ch.hslu.exercises.sw09;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
public final class Sorting {
private List<Pair<String, Consumer<int[]>>> sortingAlgos;
private int[] sorted100;
private int[] sorted200;
private int[] sorted400;
private int[] reversed100;
private int[] reversed200;
private int[] reversed400;
private int[] random100;
private int[] random200;
private int[] random400;
private static final Logger LOG = LoggerFactory.getLogger(Sorting.class);
public Sorting(final List<Pair<String, Consumer<int[]>>> sortingAlgos) {
this.sortingAlgos = sortingAlgos;
initTestData();
}
public void test() {
for (Pair<String, Consumer<int[]>> sortingAlgo : sortingAlgos) {
Instant start;
start = Instant.now();
sortingAlgo.getRight().accept(sorted100);
Duration duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 100 000 sorted: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(sorted200);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 200 000 sorted: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(sorted400);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 400 000 sorted: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(reversed100);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 100 000 reversed: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(reversed200);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 200 000 reversed: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(reversed400);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 400 000 reversed: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(random100);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 100 000 random: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(random200);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 200 000 random: " + duration);
start = Instant.now();
sortingAlgo.getRight().accept(random400);
duration = Duration.between(start, Instant.now());
LOG.info(sortingAlgo.getLeft() + " with 400 000 random: " + duration);
initTestData();
}
}
private void initTestData() {
sorted100 = new int[100_000];
for (int i = 0; i < sorted100.length; i++) {
sorted100[i] = i;
}
sorted200 = new int[200_000];
for (int i = 0; i < sorted200.length; i++) {
sorted200[i] = i;
}
sorted400 = new int[400_000];
for (int i = 0; i < sorted400.length; i++) {
sorted400[i] = i;
}
reversed100 = new int[100_000];
int element = reversed100.length;
for (int i = 0; i < reversed100.length; i++) {
element--;
reversed100[i] = element;
}
reversed200 = new int[200_000];
element = reversed200.length;
for (int i = 0; i < reversed200.length; i++) {
element--;
reversed200[i] = element;
}
reversed400 = new int[400_000];
element = reversed400.length;
for (int i = 0; i < reversed400.length; i++) {
element--;
reversed400[i] = element;
}
Random random = new Random(0);
random100 = new int[100_000];
for (int i = 0; i < random100.length; i++) {
random100[i] = random.nextInt();
}
random200 = new int[200_000];
for (int i = 0; i < random200.length; i++) {
random200[i] = random.nextInt();
}
random400 = new int[400_000];
for (int i = 0; i < random400.length; i++) {
random400[i] = random.nextInt();
}
}
}