Handling of descending runs

This commit is contained in:
finnm 2024-12-17 17:55:36 +01:00
parent 8c75ff36cd
commit a2dd6ac325

View File

@ -15,7 +15,7 @@ public class FinnSort {
int i = 0; int i = 0;
int j = extendRunRight(a, i, c); int j = extendRunRight(a, i, c);
// printList(a, c); //printList(a, c);
runs.add(new Run(i, j, 0)); 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()); 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)); Run r = new Run(min(r1.start, r2.start), max(r1.end, r2.end), min(r1.power, r2.power));
runs.add(r); runs.add(r);
// printList(a, c); //printList(a, c);
} }
private static <T> int extendRunRight(T[] a, int i, Comparator<? super T> c) { private static <T> int extendRunRight(T[] a, int i, Comparator<? super T> c) {
if (i >= a.length - 1)
return i + 1;
int j = i + 1; int j = i + 1;
while (j < a.length && c.compare(a[j-1], a[j]) <= 0) { if (c.compare(a[i], a[j]) < 0) { //
j++; 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; return j;
} }
private static <T> 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 <T> 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) { private static int power(Run run1, Run run2, int n) {
if (run1.start == 0) { if (run1.start == 0) {
return 0; return 0;
@ -89,7 +112,7 @@ public class FinnSort {
} }
return l; return l;
} }
/*
public static void printRuns() { public static void printRuns() {
String s = ""; String s = "";
for (Run run : runs) { for (Run run : runs) {
@ -111,4 +134,6 @@ public class FinnSort {
} }
System.out.println(s); System.out.println(s);
} }
*/
} }