diff --git a/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java b/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java index ad7e189..09f7d72 100644 --- a/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java +++ b/app/src/main/java/de/uni_marburg/powersort/FinnSort/FinnSort.java @@ -7,43 +7,43 @@ public class FinnSort { private static final ArrayList runs = new ArrayList<>(); - public static void sort(Integer[] a) { + static void sort(T[] a, Comparator c) { int n = a.length; int i = 0; - int j = extendRunRight(a, i); + int j = extendRunRight(a, i, c); - printList(a); + printList(a, c); runs.add(new Run(i, j, 0)); i = j; while (i < n) { - j = extendRunRight(a, i); + j = extendRunRight(a, i, c); //printRuns(); int p = power(runs.getLast(), new Run(i, j, 0), n); while (runs.size() >= 2 && p < power(runs.getLast(), runs.get(runs.size() - 2), n)) { - basicMerge(a, runs.removeFirst(), runs.removeFirst()); + basicMerge(a, runs.removeFirst(), runs.removeFirst(), c); } runs.add(new Run(i, j, p)); i = j; } while (runs.size() >= 2) { - basicMerge(a, runs.removeLast(), runs.removeLast()); + basicMerge(a, runs.removeLast(), runs.removeLast(), c); } } - private static void basicMerge(Integer[] a, Run r1, Run r2) { - ArrayList run1 = new ArrayList<>(Arrays.asList(a).subList(r1.start, r1.end)); - ArrayList run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end)); - ArrayList merge = new ArrayList<>(); + private static void basicMerge(T[] a, Run r1, Run r2, Comparator c) { + ArrayList run1 = new ArrayList<>(Arrays.asList(a).subList(r1.start, r1.end)); + ArrayList run2 = new ArrayList<>(Arrays.asList(a).subList(r2.start, r2.end)); + ArrayList merge = new ArrayList<>(); while (!run1.isEmpty() || !run2.isEmpty()) { - if (run2.isEmpty() || !run1.isEmpty() && run1.getFirst() < run2.getFirst()) { + if (run2.isEmpty() || !run1.isEmpty() && c.compare(run1.getFirst(), run2.getFirst()) < 0) { merge.add(run1.removeFirst()); } else { merge.add(run2.removeFirst()); @@ -53,12 +53,12 @@ public class FinnSort { System.arraycopy(merge.toArray(), 0, a, min(r1.start, r2.start), merge.size()); Run r = new Run(min(r1.start, r2.start), max(r1.end, r2.end), min(r1.power, r2.power)); runs.add(r); - printList(a); + printList(a, c); } - private static int extendRunRight(Integer[] a, int i) { + private static int extendRunRight(T[] a, int i, Comparator c) { int j = i + 1; - while (j < a.length && a[j-1] <= a[j]) { + while (j < a.length && c.compare(a[j-1], a[j]) <= 0) { j++; } return j; @@ -87,12 +87,12 @@ public class FinnSort { } System.out.println(s); } - public static void printList(Integer[] arr) { + public static void printList(T[] arr, Comparator c) { String s = ""; int i = 0; while (i < arr.length) { String run = "["; - int j = extendRunRight(arr, i); + int j = extendRunRight(arr, i, c); for (int t = i; t < j; t++) { run += arr[t] + ", "; } @@ -106,7 +106,7 @@ public class FinnSort { Integer[] numbers = new Integer[]{24, 25, 26, 27, 28, 21, 22, 23, 18, 19, 20, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 1, 2}; - sort(numbers); + sort(numbers, Comparator.naturalOrder()); System.out.println("Result: "); System.out.println(new ArrayList<>(List.of(numbers))); }