fix: QuickSort mergeRuns

This commit is contained in:
Daniel Langbein 2024-12-30 16:22:17 +00:00
parent 4528a544b7
commit 48a2db7bee
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 7 additions and 6 deletions

View File

@ -818,13 +818,13 @@ public final class DualPivotQuicksort {
a1 = mergeRuns(a, b, offset, -aim, run, lo, mi);
a2 = mergeRuns(a, b, offset, 0, run, mi, hi);
Object[] dst = NaturalOrder.INSTANCE.compare(a1, a) == 0 ? b : a;
Object[] dst = a1 == a ? b : a;
int k = NaturalOrder.INSTANCE.compare(a1 , a)==0 ? run[lo] - offset : run[lo];
int lo1 = NaturalOrder.INSTANCE.compare(a1 , b)==0 ? run[lo] - offset : run[lo];
int hi1 = NaturalOrder.INSTANCE.compare(a1 , b)==0 ? run[mi] - offset : run[mi];
int lo2 = NaturalOrder.INSTANCE.compare(a2 , b)==0 ? run[mi] - offset : run[mi];
int hi2 = NaturalOrder.INSTANCE.compare(a2 , b)==0 ? run[hi] - offset : run[hi];
int k = a1 == a ? run[lo] - offset : run[lo];
int lo1 = a1 == b ? run[lo] - offset : run[lo];
int hi1 = a1 == b ? run[mi] - offset : run[mi];
int lo2 = a2 == b ? run[mi] - offset : run[mi];
int hi2 = a2 == b ? run[hi] - offset : run[hi];
mergeParts(dst, k, a1, lo1, hi1, a2, lo2, hi2);
return dst;

View File

@ -32,6 +32,7 @@ public abstract class AbstractSortTest {
"3,7,-3",
"10,10,-5",
"17,17,-17",
"2,128,-64", // Large runs to trigger function `mergeRuns` of DualPivotQuicksort
})
void test2(int numOfRuns, int runLength, int decreaseBetweenRuns) {
Integer[] array = AscendingRuns.newAscendingRuns(numOfRuns, runLength, decreaseBetweenRuns).getCopy();