Handling of descending runs

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

View File

@ -67,13 +67,36 @@ public class FinnSort {
}
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;
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 <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) {
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);
}
*/
}