Faster Merging of FinnSort

This commit is contained in:
finnm 2025-01-05 12:33:08 +01:00
parent a2dd6ac325
commit 3974f575a9

View File

@ -66,6 +66,25 @@ public class FinnSort {
//printList(a, c);
}
private static <T> void merge(T[] a, Run r1, Run r2, Comparator<? super T> c) {
T[] left = java.util.Arrays.copyOfRange(a, r1.start, r1.end);
int l = 0;
int r = r2.start;
int k = r1.start;
while (l < left.length && r < r2.end) {
if (c.compare(left[l], a[r]) <= 0) {
a[k++] = left[l++];
} else {
a[k++] = a[r++];
}
}
while (l < left.length) {
a[k++] = left[l++];
}
}
private static <T> int extendRunRight(T[] a, int i, Comparator<? super T> c) {
if (i >= a.length - 1)
return i + 1;