From a2dd6ac325c47fbc306d34528512edb49642166f Mon Sep 17 00:00:00 2001 From: finnm Date: Tue, 17 Dec 2024 17:55:36 +0100 Subject: [PATCH] Handling of descending runs --- .../powersort/FinnSort/FinnSort.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) 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 ae5f1c5..657e532 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 @@ -15,7 +15,7 @@ public class FinnSort { int i = 0; int j = extendRunRight(a, i, c); -// printList(a, c); + //printList(a, c); runs.add(new Run(i, j, 0)); @@ -63,17 +63,40 @@ 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, c); + //printList(a, c); } private static int extendRunRight(T[] a, int i, Comparator c) { + if (i >= a.length - 1) + return i + 1; + + int j = i + 1; - while (j < a.length && c.compare(a[j-1], a[j]) <= 0) { - j++; + if (c.compare(a[i], a[j]) < 0) { // + while (j < a.length && c.compare(a[j - 1], a[j]) <= 0) { + j++; + } + } else { + while (j < a.length && c.compare(a[j - 1], a[j]) >= 0) { + j++; + } + makeAscending(a, i, j); } return j; } + private static void makeAscending(T[] a, int i, int j) { + for (int p = 0; p < (j - i) / 2; p++) { + swap(a, i + p ,j - p - 1); + } + } + + private static void swap(T[] a, int i, int j) { + T temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + private static int power(Run run1, Run run2, int n) { if (run1.start == 0) { return 0; @@ -89,7 +112,7 @@ public class FinnSort { } return l; } - +/* public static void printRuns() { String s = ""; for (Run run : runs) { @@ -111,4 +134,6 @@ public class FinnSort { } System.out.println(s); } + + */ } \ No newline at end of file