adjust QuickSort

This commit is contained in:
Daniel Langbein 2024-12-17 20:28:46 +00:00
parent 438d0099da
commit fe7fe86dee
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002

View File

@ -28,8 +28,6 @@ package de.uni_marburg.powersort.sort.dpqs;
*/
import java.util.Arrays;
import java.util.concurrent.CountedCompleter;
import java.util.concurrent.RecursiveTask;
/**
* This class implements powerful and fully optimized versions, both
@ -37,7 +35,7 @@ import java.util.concurrent.RecursiveTask;
* Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm
* offers O(n log(n)) performance on all data sets, and is typically
* faster than traditional (one-pivot) Quicksort implementations.
*
* <p>
* There are also additional algorithms, invoked from the Dual-Pivot
* Quicksort, such as mixed insertion sort, merging of runs and heap
* sort, counting sort and parallel merge sort.
@ -46,9 +44,7 @@ import java.util.concurrent.RecursiveTask;
* @author Jon Bentley
* @author Josh Bloch
* @author Doug Lea
*
* @version 2018.08.18
*
* @since 1.7 * 14
*/
public final class DualPivotQuicksort {
@ -56,7 +52,8 @@ public final class DualPivotQuicksort {
/**
* Prevents instantiation.
*/
private DualPivotQuicksort() {}
private DualPivotQuicksort() {
}
/**
* Max array size to use mixed insertion sort.
@ -103,7 +100,7 @@ public final class DualPivotQuicksort {
* of the array into ascending order.
*/
@FunctionalInterface
private static interface SortOperation<A> {
private interface SortOperation<A> {
/**
* Sorts the specified range of the array.
*
@ -117,16 +114,14 @@ public final class DualPivotQuicksort {
/**
* Sorts the specified range of the array into ascending numerical order.
*
* @param elemType the class of the elements of the array to be sorted
* @param array the array to be sorted
* @param offset the relative offset, in bytes, from the base address of
* the array to sort, otherwise if the array is {@code null},an absolute
* address pointing to the first element to sort from.
* @param low the index of the first element, inclusive, to be sorted
* @param high the index of the last element, exclusive, to be sorted
* @param so the method reference for the fallback implementation
*/
private static <A> void sort(Class<?> elemType, A array, long offset, int low, int high, SortOperation<A> so) {
private static <A> void sort(A array, int low, int high, SortOperation<A> so) {
so.sort(array, low, high);
}
@ -151,25 +146,20 @@ public final class DualPivotQuicksort {
/**
* Partitions the specified range of the array using the two pivots provided.
*
* @param elemType the class of the array to be partitioned
* @param array the array to be partitioned
* @param offset the relative offset, in bytes, from the base address of
* the array to partition, otherwise if the array is {@code null},an absolute
* address pointing to the first element to partition from.
* @param low the index of the first element, inclusive, to be partitioned
* @param high the index of the last element, exclusive, to be partitioned
* @param pivotIndex1 the index of pivot1, the first pivot
* @param pivotIndex2 the index of pivot2, the second pivot
* @param po the method reference for the fallback implementation
*/
private static <A> int[] partition(Class<?> elemType, A array, long offset, int low, int high, int pivotIndex1, int pivotIndex2, PartitionOperation<A> po) {
return po.partition(array, low, high, pivotIndex1, pivotIndex2);
private static <A> int[] partition(A array, int high, int pivotIndex1, int pivotIndex2, PartitionOperation<A> po) {
return po.partition(array, Unsafe.ARRAY_INT_BASE_OFFSET, high, pivotIndex1, pivotIndex2);
}
/**
* Sorts the specified range of the array using parallel merge
* sort and/or Dual-Pivot Quicksort.
*
* <p>
* To balance the faster splitting and parallelism of merge sort
* with the faster element partitioning of Quicksort, ranges are
* subdivided in tiers such that, if there is enough parallelism,
@ -181,8 +171,6 @@ public final class DualPivotQuicksort {
* @param high the index of the last element, exclusive, to be sorted
*/
public static void sort(int[] a, int low, int high) {
int size = high - low;
sort(a, 0, low, high);
}
@ -203,7 +191,7 @@ public final class DualPivotQuicksort {
* Run mixed insertion sort on small non-leftmost parts.
*/
if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) {
sort(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, low, high, DualPivotQuicksort::mixedInsertionSort);
sort(a, low, high, DualPivotQuicksort::mixedInsertionSort);
return;
}
@ -211,7 +199,7 @@ public final class DualPivotQuicksort {
* Invoke insertion sort on small leftmost part.
*/
if (size < MAX_INSERTION_SORT_SIZE) {
sort(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, low, high, DualPivotQuicksort::insertionSort);
sort(a, low, high, DualPivotQuicksort::insertionSort);
return;
}
@ -264,23 +252,49 @@ public final class DualPivotQuicksort {
* | |
* 1 ------------o-----o------------
*/
if (a[e5] < a[e2]) { int t = a[e5]; a[e5] = a[e2]; a[e2] = t; }
if (a[e4] < a[e1]) { int t = a[e4]; a[e4] = a[e1]; a[e1] = t; }
if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t; }
if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
if (a[e4] < a[e2]) { int t = a[e4]; a[e4] = a[e2]; a[e2] = t; }
if (a[e5] < a[e2]) {
int t = a[e5];
a[e5] = a[e2];
a[e2] = t;
}
if (a[e4] < a[e1]) {
int t = a[e4];
a[e4] = a[e1];
a[e1] = t;
}
if (a[e5] < a[e4]) {
int t = a[e5];
a[e5] = a[e4];
a[e4] = t;
}
if (a[e2] < a[e1]) {
int t = a[e2];
a[e2] = a[e1];
a[e1] = t;
}
if (a[e4] < a[e2]) {
int t = a[e4];
a[e4] = a[e2];
a[e2] = t;
}
if (a3 < a[e2]) {
if (a3 < a[e1]) {
a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3;
a[e3] = a[e2];
a[e2] = a[e1];
a[e1] = a3;
} else {
a[e3] = a[e2]; a[e2] = a3;
a[e3] = a[e2];
a[e2] = a3;
}
} else if (a3 > a[e4]) {
if (a3 > a[e5]) {
a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3;
a[e3] = a[e4];
a[e4] = a[e5];
a[e5] = a3;
} else {
a[e3] = a[e4]; a[e4] = a3;
a[e3] = a[e4];
a[e4] = a3;
}
}
@ -297,7 +311,7 @@ public final class DualPivotQuicksort {
* the pivots. These values are inexpensive approximation
* of tertiles. Note, that pivot1 < pivot2.
*/
int[] pivotIndices = partition(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, low, high, e1, e5, DualPivotQuicksort::partitionDualPivot);
int[] pivotIndices = partition(a, high, e1, e5, DualPivotQuicksort::partitionDualPivot);
lower = pivotIndices[0];
upper = pivotIndices[1];
@ -316,7 +330,7 @@ public final class DualPivotQuicksort {
* Use the third of the five sorted elements as the pivot.
* This value is inexpensive approximation of the median.
*/
int[] pivotIndices = partition(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, low, high, e3, e3, DualPivotQuicksort::partitionSinglePivot);
int[] pivotIndices = partition(a, high, e3, e3, DualPivotQuicksort::partitionSinglePivot);
lower = pivotIndices[0];
upper = pivotIndices[1];
/*
@ -338,7 +352,6 @@ public final class DualPivotQuicksort {
* @param high the index of the last element, exclusive, for partitioning
* @param pivotIndex1 the index of pivot1, the first pivot
* @param pivotIndex2 the index of pivot2, the second pivot
*
*/
private static int[] partitionDualPivot(int[] a, int low, int high, int pivotIndex1, int pivotIndex2) {
int end = high - 1;
@ -410,8 +423,10 @@ public final class DualPivotQuicksort {
/*
* Swap the pivots into their final positions.
*/
a[low] = a[lower]; a[lower] = pivot1;
a[end] = a[upper]; a[upper] = pivot2;
a[low] = a[lower];
a[lower] = pivot1;
a[end] = a[upper];
a[upper] = pivot2;
return new int[]{lower, upper};
}
@ -424,7 +439,6 @@ public final class DualPivotQuicksort {
* @param high the index of the last element, exclusive, for partitioning
* @param pivotIndex1 the index of pivot1, the first pivot
* @param pivotIndex2 the index of pivot2, the second pivot
*
*/
private static int[] partitionSinglePivot(int[] a, int low, int high, int pivotIndex1, int pivotIndex2) {
@ -484,16 +498,17 @@ public final class DualPivotQuicksort {
/*
* Swap the pivot into its final position.
*/
a[low] = a[lower]; a[lower] = pivot;
a[low] = a[lower];
a[lower] = pivot;
return new int[]{lower, upper};
}
/**
* Sorts the specified range of the array using mixed insertion sort.
*
* <p>
* Mixed insertion sort is combination of simple insertion sort,
* pin insertion sort and pair insertion sort.
*
* <p>
* In the context of Dual-Pivot Quicksort, the pivot element
* from the left part plays the role of sentinel, because it
* is less than any elements from the given part. Therefore,
@ -715,7 +730,9 @@ public final class DualPivotQuicksort {
// Reverse into ascending order
for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) {
int ai = a[i]; a[i] = a[j]; a[j] = ai;
int ai = a[i];
a[i] = a[j];
a[j] = ai;
}
} else { // Identify constant sequence
for (int ak = a[k]; ++k < high && ak == a[k]; ) ;
@ -784,7 +801,8 @@ public final class DualPivotQuicksort {
* Merge runs of highly structured array.
*/
if (count > 1) {
int[] b; int offset = low;
int[] b;
int offset = low;
b = new int[size];
mergeRuns(a, b, offset, 1, run, 0, count);
@ -813,7 +831,8 @@ public final class DualPivotQuicksort {
}
for (int i = run[hi], j = i - offset, low = run[lo]; i > low;
b[--j] = a[--i]
);
)
;
return b;
}