diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 306113fbbeb87..f775aecbacce4 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -26,10 +26,9 @@ package java.util; import java.util.concurrent.CountedCompleter; -import java.util.concurrent.RecursiveTask; import jdk.internal.misc.Unsafe; -import jdk.internal.vm.annotation.IntrinsicCandidate; import jdk.internal.vm.annotation.ForceInline; +import jdk.internal.vm.annotation.IntrinsicCandidate; /** * This class implements powerful and fully optimized versions, both @@ -39,17 +38,18 @@ * faster than traditional (one-pivot) Quicksort implementations. * * 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. + * Quicksort such as merging sort, sorting network, Radix sort, heap + * sort, mixed (simple, pin, pair) insertion sort, counting sort and + * parallel merge sort. * * @author Vladimir Yaroslavskiy * @author Jon Bentley * @author Josh Bloch * @author Doug Lea * - * @version 2018.08.18 + * @version 2022.06.14 * - * @since 1.7 * 14 + * @since 1.7 * 14 ^ 22 */ final class DualPivotQuicksort { @@ -58,77 +58,80 @@ final class DualPivotQuicksort { */ private DualPivotQuicksort() {} + /* ---------------- Insertion sort section ---------------- */ + /** * Max array size to use mixed insertion sort. */ - private static final int MAX_MIXED_INSERTION_SORT_SIZE = 65; + private static final int MAX_MIXED_INSERTION_SORT_SIZE = 124; /** * Max array size to use insertion sort. */ private static final int MAX_INSERTION_SORT_SIZE = 44; - /** - * Min array size to perform sorting in parallel. - */ - private static final int MIN_PARALLEL_SORT_SIZE = 4 << 10; + /* ----------------- Merging sort section ----------------- */ /** - * Min array size to try merging of runs. + * Min array size to use merging sort. */ - private static final int MIN_TRY_MERGE_SIZE = 4 << 10; + private static final int MIN_MERGING_SORT_SIZE = 512; /** - * Min size of the first run to continue with scanning. + * Min size of run to continue scanning. */ - private static final int MIN_FIRST_RUN_SIZE = 16; + private static final int MIN_RUN_SIZE = 128; - /** - * Min factor for the first runs to continue scanning. - */ - private static final int MIN_FIRST_RUNS_FACTOR = 7; + /* ------------------ Radix sort section ------------------ */ /** - * Max capacity of the index array for tracking runs. + * Min array size to use Radix sort. */ - private static final int MAX_RUN_CAPACITY = 5 << 10; + private static final int MIN_RADIX_SORT_SIZE = 800; + + /* ------------------ Counting sort section --------------- */ /** - * Min number of runs, required by parallel merging. + * Min size of a byte array to use counting sort. */ - private static final int MIN_RUN_COUNT = 4; + private static final int MIN_BYTE_COUNTING_SORT_SIZE = 36; /** - * Min array size to use parallel merging of parts. + * Min size of a char array to use counting sort. */ - private static final int MIN_PARALLEL_MERGE_PARTS_SIZE = 4 << 10; + private static final int MIN_CHAR_COUNTING_SORT_SIZE = 1700; /** - * Min size of a byte array to use counting sort. + * Min size of a short array to use counting sort. */ - private static final int MIN_BYTE_COUNTING_SORT_SIZE = 64; + private static final int MIN_SHORT_COUNTING_SORT_SIZE = 2100; + + /* -------------------- Common section -------------------- */ /** - * Min size of a short or char array to use counting sort. + * Min array size to perform sorting in parallel. */ - private static final int MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE = 1750; + private static final int MIN_PARALLEL_SORT_SIZE = 768; /** - * Threshold of mixed insertion sort is incremented by this value. + * Max recursive depth before switching to heap sort. */ - private static final int DELTA = 3 << 1; + private static final int MAX_RECURSION_DEPTH = 64 << 1; /** - * Max recursive partitioning depth before using heap sort. + * Max size of additional buffer in bytes, + * limited by max_heap / 16 or 2 GB max. */ - private static final int MAX_RECURSION_DEPTH = 64 * DELTA; + private static final int MAX_BUFFER_SIZE = + (int) Math.min(Runtime.getRuntime().maxMemory() >>> 4, Integer.MAX_VALUE); /** - * Represents a function that accepts the array and sorts the specified range - * of the array into ascending order. + * Represents a function that accepts the array and sorts + * the specified range of the array into ascending order. */ @FunctionalInterface - private static interface SortOperation { + private interface SortOperation { + /** * Sorts the specified range of the array. * @@ -136,80 +139,70 @@ private static interface SortOperation { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - void sort(A a, int low, int high); + void sort(T a, int low, int high); } /** * 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 a the array to be sorted + * @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 sorted * @param high the index of the last element, exclusive, to be sorted * @param so the method reference for the fallback implementation */ - @IntrinsicCandidate @ForceInline - private static void sort(Class elemType, A array, long offset, int low, int high, SortOperation so) { - so.sort(array, low, high); + @IntrinsicCandidate + private static void sort(Class elemType, T a, long offset, + int low, int high, SortOperation so) { + so.sort(a, low, high); } /** - * Represents a function that accepts the array and partitions the specified range - * of the array using the pivots provided. + * Represents a function that accepts the array and partitions + * the specified range of the array using the given pivots. */ @FunctionalInterface - interface PartitionOperation { + interface PartitionOperation { + /** * Partitions the specified range of the array using the given pivots. * - * @param a the array to be partitioned - * @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 a the array for partitioning + * @param low the index of the first element, inclusive, for partitioning + * @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 + * @return indices of parts after partitioning */ - int[] partition(A a, int low, int high, int pivotIndex1, int pivotIndex2); + int[] partition(T a, int low, int high, int pivotIndex1, int pivotIndex2); } /** - * Partitions the specified range of the array using the two pivots provided. + * Partitions the specified range of the array using the given pivots. * - * @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 elemType the class of the array for partitioning + * @param a the array for partitioning + * @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, for partitioning + * @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 * @param po the method reference for the fallback implementation + * @return indices of parts after partitioning */ - @IntrinsicCandidate @ForceInline - private static int[] partition(Class elemType, A array, long offset, int low, int high, int pivotIndex1, int pivotIndex2, PartitionOperation po) { - return po.partition(array, low, high, pivotIndex1, pivotIndex2); - } - - /** - * Calculates the double depth of parallel merging. - * Depth is negative, if tasks split before sorting. - * - * @param parallelism the parallelism level - * @param size the target size - * @return the depth of parallel merging - */ - private static int getDepth(int parallelism, int size) { - int depth = 0; - - while ((parallelism >>= 3) > 0 && (size >>= 2) > 0) { - depth -= 2; - } - return depth; + @IntrinsicCandidate + private static int[] partition(Class elemType, T a, long offset, + int low, int high, int pivotIndex1, int pivotIndex2, PartitionOperation po) { + return po.partition(a, low, high, pivotIndex1, pivotIndex2); } /** @@ -228,36 +221,33 @@ private static int getDepth(int parallelism, int size) { * @param high the index of the last element, exclusive, to be sorted */ static void sort(int[] a, int parallelism, int low, int high) { - int size = high - low; - - if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { - int depth = getDepth(parallelism, size >> 12); - int[] b = depth == 0 ? null : new int[size]; - new Sorter(null, a, b, low, size, low, depth).invoke(); + if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } } /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param sorter parallel context * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - static void sort(Sorter sorter, int[] a, int bits, int low, int high) { + static void sort(Sorter sorter, int[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; + /* - * Run mixed insertion sort on small non-leftmost parts. + * Run adaptive 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(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, + low, high, DualPivotQuicksort::mixedInsertionSort); return; } @@ -265,33 +255,25 @@ static void sort(Sorter sorter, int[] a, int bits, int low, int high) { * 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(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, + low, high, DualPivotQuicksort::insertionSort); return; } /* - * Check if the whole array or large non-leftmost - * parts are nearly sorted and then merge runs. + * Try merging sort on large part. */ - if ((bits == 0 || size > MIN_TRY_MERGE_SIZE && (bits & 1) > 0) - && tryMergeRuns(sorter, a, low, size)) { + if (size > MIN_MERGING_SORT_SIZE * bits + && tryMergingSort(sorter, a, low, high)) { return; } /* - * Switch to heap sort if execution - * time is becoming quadratic. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); - return; - } - - /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. - */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -300,30 +282,42 @@ && tryMergeRuns(sorter, a, low, size)) { * determined to work well on a wide variety of inputs. */ int e1 = low + step; - int e5 = end - step; + int e5 = high - step; int e3 = (e1 + e5) >>> 1; int e2 = (e1 + e3) >>> 1; int e4 = (e3 + e5) >>> 1; int a3 = a[e3]; /* - * Sort these elements in place by the combination + * Check if part is large and contains random + * data, taking into account parallel context. + */ + boolean isLargeRandom = + sorter != null && bits > 2 && size > MIN_RADIX_SORT_SIZE && +// size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > 0) && + (a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]); + + /* + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------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[e1] > a[e4]) { int t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { int t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { int t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { int t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { int t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -338,81 +332,91 @@ && tryMergeRuns(sorter, a, low, size)) { } } - // Pointers - int lower; // The index of the last element of the left part - int upper; // The index of the first element of the right part + /* + * Try Radix sort on large fully random data. + */ + if (isLargeRandom + && a[e2] < a[e3] && a[e3] < a[e4] + && tryRadixSort(sorter, a, low, high)) { + return; + } /* - * Partitioning with 2 pivots in case of different elements. + * Switch to heap sort, if execution time is quadratic. */ - if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Use the first and fifth of the five sorted elements as - * 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); - lower = pivotIndices[0]; - upper = pivotIndices[1]; + if ((bits += 2) > MAX_RECURSION_DEPTH) { + heapSort(a, low, high); + return; + } + + /* + * indices[0] - the index of the last element of the left part + * indices[1] - the index of the first element of the right part + */ + int[] indices; + /* + * Partitioning with two pivots on array of fully random elements. + */ + if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { + indices = partition(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, + low, high, e1, e5, DualPivotQuicksort::partitionWithTwoPivots); /* * Sort non-left parts recursively (possibly in parallel), * excluding known pivots. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, lower + 1, upper); - sorter.forkSorter(bits | 1, upper + 1, high); + sorter.fork(bits | 1, indices[0] + 1, indices[1]); + sorter.fork(bits | 1, indices[1] + 1, high); } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); + sort(sorter, a, bits | 1, indices[0] + 1, indices[1]); + sort(sorter, a, bits | 1, indices[1] + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot + + indices = partition(int.class, a, Unsafe.ARRAY_INT_BASE_OFFSET, + low, high, e3, e3, DualPivotQuicksort::partitionWithOnePivot); - /* - * 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); - lower = pivotIndices[0]; - upper = pivotIndices[1]; /* * Sort the right part (possibly in parallel), excluding * known pivot. All elements from the central part are * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, indices[1], high); } else { - sort(sorter, a, bits | 1, upper, high); + sort(sorter, a, bits | 1, indices[1], high); } } - high = lower; // Iterate along the left part + high = indices[0]; // Iterate along the left part } } /** - * Partitions the specified range of the array using the two pivots provided. + * Partitions the specified range of the array using two given pivots. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionDualPivot(int[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - int end = high - 1; + private static int[] partitionWithTwoPivots( + int[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = --high; int lower = low; - int upper = end; - int e1 = pivotIndex1; - int e5 = pivotIndex2; - int pivot1 = a[e1]; - int pivot2 = a[e5]; + /* + * Use the first and fifth of the five sorted elements as + * the pivots. These values are inexpensive approximation + * of tertiles. Note, that pivot1 < pivot2. + */ + int pivot1 = a[pivotIndex1]; + int pivot2 = a[pivotIndex2]; /* * The first and the last elements to be sorted are moved @@ -421,8 +425,8 @@ private static int[] partitionDualPivot(int[] a, int low, int high, int pivotInd * into their final positions, and excluded from the next * subsequent sorting. */ - a[e1] = a[lower]; - a[e5] = a[upper]; + a[pivotIndex1] = a[lower]; + a[pivotIndex2] = a[upper]; /* * Skip elements, which are less or greater than the pivots. @@ -433,39 +437,35 @@ private static int[] partitionDualPivot(int[] a, int low, int high, int pivotInd /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { int ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -474,30 +474,32 @@ private static int[] partitionDualPivot(int[] a, int low, int high, int pivotInd /* * 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[high] = a[upper]; a[upper] = pivot2; - return new int[] {lower, upper}; + return new int[] { lower, upper }; } /** - * Partitions the specified range of the array using a single pivot provided. + * Partitions the specified range of the array using one given pivot. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @param pivotIndex1 the index of single pivot + * @param pivotIndex2 the index of single pivot + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionSinglePivot(int[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - - int end = high - 1; + private static int[] partitionWithOnePivot( + int[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = high; int lower = low; - int upper = end; - int e3 = pivotIndex1; - int pivot = a[e3]; + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + int pivot = a[pivotIndex1]; /* * The first element to be sorted is moved to the @@ -506,41 +508,37 @@ private static int[] partitionSinglePivot(int[] a, int low, int high, int pivotI * back into its final position, and excluded from * the next subsequent sorting. */ - a[e3] = a[lower]; + a[pivotIndex1] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ - for (int k = ++upper; --k > lower; ) { + for (int k = upper; --k > lower; ) { int ak = a[k]; if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -550,14 +548,15 @@ private static int[] partitionSinglePivot(int[] a, int low, int high, int pivotI * Swap the pivot into its final position. */ a[low] = a[lower]; a[lower] = pivot; - return new int[] {lower, upper}; + + return new int[] { lower, upper }; } /** * Sorts the specified range of the array using mixed insertion sort. * - * Mixed insertion sort is combination of simple insertion sort, - * pin insertion sort and pair insertion sort. + * Mixed insertion sort is combination of pin insertion sort, + * simple insertion sort and pair insertion sort. * * In the context of Dual-Pivot Quicksort, the pivot element * from the left part plays the role of sentinel, because it @@ -569,110 +568,85 @@ private static int[] partitionSinglePivot(int[] a, int low, int high, int pivotI * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(int[] a, int low, int high) { - int size = high - low; - int end = high - 3 * ((size >> 5) << 3); - if (end == high) { + static void mixedInsertionSort(int[] a, int low, int high) { - /* - * Invoke simple insertion sort on tiny array. - */ - for (int i; ++low < end; ) { + /* + * Split part for pin and pair insertion sorts. + */ + int end = high - 3 * ((high - low) >> 3 << 1); + + /* + * Invoke simple insertion sort on small part. + */ + if (end == high) { + for (int i; ++low < high; ) { int ai = a[i = low]; - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[i - 1]) { + a[i] = a[--i]; } - a[i + 1] = ai; + a[i] = ai; } - } else { + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + int ai = a[i = low], pin = a[--p]; /* - * Start with pin insertion sort on small part. - * - * Pin insertion sort is extended simple insertion sort. - * The main idea of this sort is to put elements larger - * than an element called pin to the end of array (the - * proper area for such elements). It avoids expensive - * movements of these elements through the whole array. + * Swap larger element with pin. */ - int pin = a[end]; - - for (int i, p = high; ++low < end; ) { - int ai = a[i = low]; - - if (ai < a[i - 1]) { // Small element - - /* - * Insert small element into sorted part. - */ - a[i] = a[--i]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - - } else if (p > i && ai > pin) { // Large element - - /* - * Find element smaller than pin. - */ - while (a[--p] > pin); - - /* - * Swap it with large element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } - - /* - * Insert small element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } + if (ai > pin) { + ai = pin; + a[p] = a[i]; } /* - * Continue with pair insertion sort on remain part. + * Insert element into sorted part. */ - for (int i; low < high; ++low) { - int a1 = a[i = low], a2 = a[++low]; + while (ai < a[i - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } - /* - * Insert two elements per iteration: at first, insert the - * larger element and then insert the smaller element, but - * from the position where the larger element was inserted. - */ - if (a1 > a2) { + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + int a1 = a[i = low], a2 = a[++low]; - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + /* + * Insert two elements per iteration: at first, insert the + * larger element and then insert the smaller element, but + * from the position where the larger element was inserted. + */ + if (a1 > a2) { - while (a2 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a2; + while (a1 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a1; - } else if (a1 < a[i - 1]) { + while (a2 < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = a2; - while (a2 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a2; + } else if (a1 < a[i - 1]) { - while (a1 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a1; + while (a2 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a2; + + while (a1 < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = a1; } } } @@ -684,90 +658,45 @@ private static void mixedInsertionSort(int[] a, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(int[] a, int low, int high) { + static void insertionSort(int[] a, int low, int high) { for (int i, k = low; ++k < high; ) { int ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Sorts the specified range of the array using heap sort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void heapSort(int[] a, int low, int high) { - for (int k = (low + high) >>> 1; k > low; ) { - pushDown(a, --k, a[k], low, high); - } - while (--high > low) { - int max = a[low]; - pushDown(a, low, a[high], low, high); - a[high] = max; - } - } - - /** - * Pushes specified element down during heap sort. - * - * @param a the given array - * @param p the start index - * @param value the given element - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void pushDown(int[] a, int p, int value, int low, int high) { - for (int k ;; a[p] = a[p = k]) { - k = (p << 1) - low + 2; // Index of the right child + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + a[i ] = ai; } } - a[p] = value; } /** - * Tries to sort the specified range of the array. + * Tries to sort the specified range of the array using merging sort. * * @param sorter parallel context * @param a the array to be sorted - * @param low the index of the first element to be sorted - * @param size the array size - * @return true if finally sorted, false otherwise + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, int[] a, int low, int high) { /* - * The run array is constructed only if initial runs are - * long enough to continue, run[i] then holds start index - * of the i-th sequence of elements in non-descending order. + * The element run[i] holds the start index + * of i-th sequence in non-descending order. */ + int count = 1; int[] run = null; - int high = low + size; - int count = 1, last = low; /* * Identify all possible runs. */ - for (int k = low + 1; k < high; ) { + for (int k = low + 1, last = low; k < high; ) { /* - * Find the end index of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -792,70 +721,61 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { } /* - * Check special cases. + * Check if the runs are too long to continue scanning. + */ + if (count > 6 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { - if (k == high) { + if (k == high) { /* - * The array is monotonous sequence, + * Array is monotonous sequence * and therefore already sorted. */ return true; } - if (k - low < MIN_FIRST_RUN_SIZE) { - - /* - * The first run is too small - * to proceed with scanning. - */ - return false; - } - - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } - - if (++count == MAX_RUN_CAPACITY) { + } else if (a[last - 1] > a[last]) { // Start the new run + if (++count == run.length) { /* * Array is not highly structured. */ return false; } + } - if (count == run.length) { + /* + * Save the current run. + */ + run[count] = (last = k); - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; } - run[count] = (last = k); } /* - * Merge runs of highly structured array. + * Merge all runs. */ if (count > 1) { int[] b; int offset = low; - if (sorter == null || (b = (int[]) sorter.b) == null) { - b = new int[size]; - } else { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; + } else if ((b = tryAllocate(int[].class, high - low)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -876,15 +796,13 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { * @return the destination where runs are merged */ private static int[] mergeRuns(int[] a, int[] b, int offset, - int aim, boolean parallel, int[] run, int lo, int hi) { + int aim, boolean parallel, int[] run, int lo, int hi) { if (hi - lo == 1) { if (aim >= 0) { return a; } - for (int i = run[hi], j = i - offset, low = run[lo]; i > low; - b[--j] = a[--i] - ); + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); return b; } @@ -895,19 +813,10 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, while (run[++mi + 1] <= rmi); /* - * Merge the left and right parts. + * Merge runs of each part. */ - int[] a1, a2; - - if (parallel && hi - lo > MIN_RUN_COUNT) { - RunMerger merger = new RunMerger(a, b, offset, 0, run, mi, hi).forkMe(); - a1 = mergeRuns(a, b, offset, -aim, true, run, lo, mi); - a2 = (int[]) merger.getDestination(); - } else { - a1 = mergeRuns(a, b, offset, -aim, false, run, lo, mi); - a2 = mergeRuns(a, b, offset, 0, false, run, mi, hi); - } - + int[] a1 = mergeRuns(a, b, offset, -aim, parallel, run, lo, mi); + int[] a2 = mergeRuns(a, b, offset, 0, parallel, run, mi, hi); int[] dst = a1 == a ? b : a; int k = a1 == a ? run[lo] - offset : run[lo]; @@ -916,8 +825,11 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, int lo2 = a2 == b ? run[mi] - offset : run[mi]; int hi2 = a2 == b ? run[hi] - offset : run[hi]; - if (parallel) { - new Merger(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); + /* + * Merge the left and right parts. + */ + if (hi1 - lo1 > MIN_PARALLEL_SORT_SIZE && parallel) { + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -937,9 +849,12 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, * @param lo2 the start index of the second part, inclusive * @param hi2 the end index of the second part, exclusive */ - private static void mergeParts(Merger merger, int[] dst, int k, - int[] a1, int lo1, int hi1, int[] a2, int lo2, int hi2) { + private static void mergeParts(Merger merger, int[] dst, int k, + int[] a1, int lo1, int hi1, int[] a2, int lo2, int hi2) { + /* + * Merge sorted parts in parallel. + */ if (merger != null && a1 == a2) { while (true) { @@ -952,10 +867,18 @@ private static void mergeParts(Merger merger, int[] dst, int k, int hi = hi1; hi1 = hi2; hi2 = hi; } + /* + * Terminate, if the second part is empty. + */ + if (lo2 == hi2) { + System.arraycopy(a1, lo1, dst, k, hi1 - lo1); + return; + } + /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -967,27 +890,30 @@ private static void mergeParts(Merger merger, int[] dst, int k, int mi2 = hi2; /* - * Partition the smaller part. + * Divide the smaller part. */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; + for (int mi0 = lo2; mi0 < mi2; ) { + int m = (mi0 + mi2) >>> 1; - if (key > a2[t]) { - loo = t + 1; + if (key > a2[m]) { + mi0 = m + 1; } else { - mi2 = t; + mi2 = m; } } - int d = mi2 - lo2 + mi1 - lo1; + /* + * Reserve space for the left parts. + */ + int space = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge other parts in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + space, mi1, hi1, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left parts. */ hi1 = mi1; hi2 = mi2; @@ -997,8 +923,10 @@ private static void mergeParts(Merger merger, int[] dst, int k, /* * Merge small parts sequentially. */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + if (lo2 < hi2 && a1[hi1 - 1] > a2[lo2]) { + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } } if (dst != a1 || k < lo1) { while (lo1 < hi1) { @@ -1012,15 +940,184 @@ private static void mergeParts(Merger merger, int[] dst, int k, } } -// [long] - /** - * Sorts the specified range of the array using parallel merge - * sort and/or Dual-Pivot Quicksort. + * Tries to sort the specified range of the array + * using LSD (The Least Significant Digit) Radix sort. * - * 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, + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} + */ + static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { + int[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = sorter.b) != null) { + offset = sorter.offset; + } else if ((b = tryAllocate(int[].class, size)) == null) { + return false; + } + + int start = low - offset; + int last = high - offset; + + /* + * Count the number of all digits. + */ + int[] count1 = new int[1024]; + int[] count2 = new int[2048]; + int[] count3 = new int[2048]; + + for (int i = low; i < high; ++i) { + ++count1[ a[i] & 0x3FF]; + ++count2[(a[i] >>> 10) & 0x7FF]; + ++count3[(a[i] >>> 21) ^ 0x400]; // Reverse the sign bit + } + + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, size, low); + boolean processDigit2 = processDigit(count2, size, low); + boolean processDigit3 = processDigit(count3, size, low); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = high; i > low; ) { + b[--count1[a[--i] & 0x3FF] - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = last; i > start; ) { + a[--count2[(b[--i] >>> 10) & 0x7FF]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count2[(a[--i] >>> 10) & 0x7FF] - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = last; i > start; ) { + a[--count3[(b[--i] >>> 21) ^ 0x400]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count3[(a[--i] >>> 21) ^ 0x400] - offset] = a[i]; + } + } + } + + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3) { + System.arraycopy(b, low - offset, a, low, size); + } + return true; + } + + /** + * Checks the count array and then computes the histogram. + * + * @param count the count array + * @param total the total number of elements + * @param low the index of the first element, inclusive + * @return {@code true} if the digit must be processed, otherwise {@code false} + */ + private static boolean processDigit(int[] count, int total, int low) { + + /* + * Check if we can skip the given digit. + */ + for (int c : count) { + if (c == total) { + return false; + } + if (c > 0) { + break; + } + } + + /* + * Compute the histogram. + */ + count[0] += low; + + for (int i = 0; ++i < count.length; ) { + count[i] += count[i - 1]; + } + return true; + } + + /** + * Sorts the specified range of the array using heap sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + static void heapSort(int[] a, int low, int high) { + for (int k = (low + high) >>> 1; k > low; ) { + pushDown(a, --k, a[k], low, high); + } + while (--high > low) { + int max = a[low]; + pushDown(a, low, a[high], low, high); + a[high] = max; + } + } + + /** + * Pushes specified element down during heap sort. + * + * @param a the given array + * @param p the start index + * @param value the given element + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + private static void pushDown(int[] a, int p, int value, int low, int high) { + for (int k ;; a[p] = a[p = k]) { + k = (p << 1) - low + 2; // Index of the right child + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + +// #[long] + + /** + * Sorts the specified range of the array using parallel merge + * sort and/or Dual-Pivot Quicksort. + * + * 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, * the four-way parallel merge is started, still ensuring enough * parallelism to process the partitions. * @@ -1030,37 +1127,33 @@ private static void mergeParts(Merger merger, int[] dst, int k, * @param high the index of the last element, exclusive, to be sorted */ static void sort(long[] a, int parallelism, int low, int high) { - int size = high - low; - - if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { - int depth = getDepth(parallelism, size >> 12); - long[] b = depth == 0 ? null : new long[size]; - new Sorter(null, a, b, low, size, low, depth).invoke(); + if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } } /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param sorter parallel context * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - static void sort(Sorter sorter, long[] a, int bits, int low, int high) { + static void sort(Sorter sorter, long[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; /* - * Run mixed insertion sort on small non-leftmost parts. + * Run adaptive mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - sort(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, low, high, DualPivotQuicksort::mixedInsertionSort); + sort(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, + low, high, DualPivotQuicksort::mixedInsertionSort); return; } @@ -1068,33 +1161,25 @@ static void sort(Sorter sorter, long[] a, int bits, int low, int high) { * Invoke insertion sort on small leftmost part. */ if (size < MAX_INSERTION_SORT_SIZE) { - sort(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, low, high, DualPivotQuicksort::insertionSort); - return; - } - - /* - * Check if the whole array or large non-leftmost - * parts are nearly sorted and then merge runs. - */ - if ((bits == 0 || size > MIN_TRY_MERGE_SIZE && (bits & 1) > 0) - && tryMergeRuns(sorter, a, low, size)) { + sort(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, + low, high, DualPivotQuicksort::insertionSort); return; } /* - * Switch to heap sort if execution - * time is becoming quadratic. + * Try merging sort on large part. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); + if (size > MIN_MERGING_SORT_SIZE * bits + && tryMergingSort(sorter, a, low, high)) { return; } /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -1103,30 +1188,42 @@ && tryMergeRuns(sorter, a, low, size)) { * determined to work well on a wide variety of inputs. */ int e1 = low + step; - int e5 = end - step; + int e5 = high - step; int e3 = (e1 + e5) >>> 1; int e2 = (e1 + e3) >>> 1; int e4 = (e3 + e5) >>> 1; long a3 = a[e3]; /* - * Sort these elements in place by the combination + * Check if part is large and contains random + * data, taking into account parallel context. + */ + boolean isLargeRandom = + sorter != null && bits > 2 && size > MIN_RADIX_SORT_SIZE && +// size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > 0) && + (a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]); + + /* + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------o-------o----------------- */ - if (a[e5] < a[e2]) { long t = a[e5]; a[e5] = a[e2]; a[e2] = t; } - if (a[e4] < a[e1]) { long t = a[e4]; a[e4] = a[e1]; a[e1] = t; } - if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t; } - if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; } - if (a[e4] < a[e2]) { long t = a[e4]; a[e4] = a[e2]; a[e2] = t; } + if (a[e1] > a[e4]) { long t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { long t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { long t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { long t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { long t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -1141,79 +1238,91 @@ && tryMergeRuns(sorter, a, low, size)) { } } - // Pointers - int lower; // The index of the last element of the left part - int upper; // The index of the first element of the right part + /* + * Try Radix sort on large fully random data. + */ + if (isLargeRandom + && a[e2] < a[e3] && a[e3] < a[e4] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Switch to heap sort, if execution time is quadratic. + */ + if ((bits += 2) > MAX_RECURSION_DEPTH) { + heapSort(a, low, high); + return; + } + + /* + * indices[0] - the index of the last element of the left part + * indices[1] - the index of the first element of the right part + */ + int[] indices; /* - * Partitioning with 2 pivots in case of different elements. + * Partitioning with two pivots on array of fully random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Use the first and fifth of the five sorted elements as - * the pivots. These values are inexpensive approximation - * of tertiles. Note, that pivot1 < pivot2. - */ - int[] pivotIndices = partition(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, low, high, e1, e5, DualPivotQuicksort::partitionDualPivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; + indices = partition(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, + low, high, e1, e5, DualPivotQuicksort::partitionWithTwoPivots); + /* * Sort non-left parts recursively (possibly in parallel), * excluding known pivots. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, lower + 1, upper); - sorter.forkSorter(bits | 1, upper + 1, high); + sorter.fork(bits | 1, indices[0] + 1, indices[1]); + sorter.fork(bits | 1, indices[1] + 1, high); } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); + sort(sorter, a, bits | 1, indices[0] + 1, indices[1]); + sort(sorter, a, bits | 1, indices[1] + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot + + indices = partition(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, + low, high, e3, e3, DualPivotQuicksort::partitionWithOnePivot); - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - int[] pivotIndices = partition(long.class, a, Unsafe.ARRAY_LONG_BASE_OFFSET, low, high, e3, e3, DualPivotQuicksort::partitionSinglePivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; /* * Sort the right part (possibly in parallel), excluding * known pivot. All elements from the central part are * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, indices[1], high); } else { - sort(sorter, a, bits | 1, upper, high); + sort(sorter, a, bits | 1, indices[1], high); } } - high = lower; // Iterate along the left part + high = indices[0]; // Iterate along the left part } } /** - * Partitions the specified range of the array using the two pivots provided. + * Partitions the specified range of the array using two given pivots. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionDualPivot(long[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - int end = high - 1; + private static int[] partitionWithTwoPivots( + long[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = --high; int lower = low; - int upper = end; - int e1 = pivotIndex1; - int e5 = pivotIndex2; - long pivot1 = a[e1]; - long pivot2 = a[e5]; + /* + * Use the first and fifth of the five sorted elements as + * the pivots. These values are inexpensive approximation + * of tertiles. Note, that pivot1 < pivot2. + */ + long pivot1 = a[pivotIndex1]; + long pivot2 = a[pivotIndex2]; /* * The first and the last elements to be sorted are moved @@ -1222,8 +1331,8 @@ private static int[] partitionDualPivot(long[] a, int low, int high, int pivotIn * into their final positions, and excluded from the next * subsequent sorting. */ - a[e1] = a[lower]; - a[e5] = a[upper]; + a[pivotIndex1] = a[lower]; + a[pivotIndex2] = a[upper]; /* * Skip elements, which are less or greater than the pivots. @@ -1234,39 +1343,35 @@ private static int[] partitionDualPivot(long[] a, int low, int high, int pivotIn /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { long ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -1275,31 +1380,32 @@ private static int[] partitionDualPivot(long[] a, int low, int high, int pivotIn /* * 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[high] = a[upper]; a[upper] = pivot2; - return new int[] {lower, upper}; + return new int[] { lower, upper }; } /** - * Partitions the specified range of the array using a single pivot provided. + * Partitions the specified range of the array using one given pivot. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @param pivotIndex1 the index of single pivot + * @param pivotIndex2 the index of single pivot + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionSinglePivot(long[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - - int end = high - 1; + private static int[] partitionWithOnePivot( + long[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = high; int lower = low; - int upper = end; - int e3 = pivotIndex1; - long pivot = a[e3]; + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + long pivot = a[pivotIndex1]; /* * The first element to be sorted is moved to the @@ -1308,41 +1414,37 @@ private static int[] partitionSinglePivot(long[] a, int low, int high, int pivot * back into its final position, and excluded from * the next subsequent sorting. */ - a[e3] = a[lower]; + a[pivotIndex1] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ - for (int k = ++upper; --k > lower; ) { + for (int k = upper; --k > lower; ) { long ak = a[k]; if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -1352,14 +1454,15 @@ private static int[] partitionSinglePivot(long[] a, int low, int high, int pivot * Swap the pivot into its final position. */ a[low] = a[lower]; a[lower] = pivot; - return new int[] {lower, upper}; + + return new int[] { lower, upper }; } /** * Sorts the specified range of the array using mixed insertion sort. * - * Mixed insertion sort is combination of simple insertion sort, - * pin insertion sort and pair insertion sort. + * Mixed insertion sort is combination of pin insertion sort, + * simple insertion sort and pair insertion sort. * * In the context of Dual-Pivot Quicksort, the pivot element * from the left part plays the role of sentinel, because it @@ -1371,110 +1474,85 @@ private static int[] partitionSinglePivot(long[] a, int low, int high, int pivot * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(long[] a, int low, int high) { - int size = high - low; - int end = high - 3 * ((size >> 5) << 3); - if (end == high) { + static void mixedInsertionSort(long[] a, int low, int high) { - /* - * Invoke simple insertion sort on tiny array. - */ - for (int i; ++low < end; ) { + /* + * Split part for pin and pair insertion sorts. + */ + int end = high - 3 * ((high - low) >> 3 << 1); + + /* + * Invoke simple insertion sort on small part. + */ + if (end == high) { + for (int i; ++low < high; ) { long ai = a[i = low]; - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[i - 1]) { + a[i] = a[--i]; } - a[i + 1] = ai; + a[i] = ai; } - } else { + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + long ai = a[i = low], pin = a[--p]; /* - * Start with pin insertion sort on small part. - * - * Pin insertion sort is extended simple insertion sort. - * The main idea of this sort is to put elements larger - * than an element called pin to the end of array (the - * proper area for such elements). It avoids expensive - * movements of these elements through the whole array. + * Swap larger element with pin. */ - long pin = a[end]; - - for (int i, p = high; ++low < end; ) { - long ai = a[i = low]; - - if (ai < a[i - 1]) { // Small element - - /* - * Insert small element into sorted part. - */ - a[i] = a[--i]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - - } else if (p > i && ai > pin) { // Large element - - /* - * Find element smaller than pin. - */ - while (a[--p] > pin); - - /* - * Swap it with large element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } - - /* - * Insert small element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } + if (ai > pin) { + ai = pin; + a[p] = a[i]; } /* - * Continue with pair insertion sort on remain part. + * Insert element into sorted part. */ - for (int i; low < high; ++low) { - long a1 = a[i = low], a2 = a[++low]; + while (ai < a[i - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } - /* - * Insert two elements per iteration: at first, insert the - * larger element and then insert the smaller element, but - * from the position where the larger element was inserted. - */ - if (a1 > a2) { + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + long a1 = a[i = low], a2 = a[++low]; - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + /* + * Insert two elements per iteration: at first, insert the + * larger element and then insert the smaller element, but + * from the position where the larger element was inserted. + */ + if (a1 > a2) { - while (a2 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a2; + while (a1 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a1; + + while (a2 < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = a2; - } else if (a1 < a[i - 1]) { + } else if (a1 < a[i - 1]) { - while (a2 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a2; + while (a2 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a2; - while (a1 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a1; + while (a1 < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = a1; } } } @@ -1486,90 +1564,45 @@ private static void mixedInsertionSort(long[] a, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(long[] a, int low, int high) { + static void insertionSort(long[] a, int low, int high) { for (int i, k = low; ++k < high; ) { long ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Sorts the specified range of the array using heap sort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void heapSort(long[] a, int low, int high) { - for (int k = (low + high) >>> 1; k > low; ) { - pushDown(a, --k, a[k], low, high); - } - while (--high > low) { - long max = a[low]; - pushDown(a, low, a[high], low, high); - a[high] = max; - } - } - - /** - * Pushes specified element down during heap sort. - * - * @param a the given array - * @param p the start index - * @param value the given element - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void pushDown(long[] a, int p, long value, int low, int high) { - for (int k ;; a[p] = a[p = k]) { - k = (p << 1) - low + 2; // Index of the right child + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + a[i ] = ai; } } - a[p] = value; } /** - * Tries to sort the specified range of the array. + * Tries to sort the specified range of the array using merging sort. * * @param sorter parallel context * @param a the array to be sorted - * @param low the index of the first element to be sorted - * @param size the array size - * @return true if finally sorted, false otherwise + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, long[] a, int low, int high) { /* - * The run array is constructed only if initial runs are - * long enough to continue, run[i] then holds start index - * of the i-th sequence of elements in non-descending order. + * The element run[i] holds the start index + * of i-th sequence in non-descending order. */ + int count = 1; int[] run = null; - int high = low + size; - int count = 1, last = low; /* * Identify all possible runs. */ - for (int k = low + 1; k < high; ) { + for (int k = low + 1, last = low; k < high; ) { /* - * Find the end index of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -1594,70 +1627,61 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) } /* - * Check special cases. + * Check if the runs are too long to continue scanning. */ - if (run == null) { - if (k == high) { - - /* - * The array is monotonous sequence, - * and therefore already sorted. - */ - return true; - } - - if (k - low < MIN_FIRST_RUN_SIZE) { - - /* - * The first run is too small - * to proceed with scanning. - */ - return false; - } - - run = new int[((size >> 10) | 0x7F) & 0x3FF]; - run[0] = low; - - } else if (a[last - 1] > a[last]) { + if (count > 6 && k - low < count * MIN_RUN_SIZE) { + return false; + } - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { + /* + * Process the run. + */ + if (run == null) { + if (k == high) { /* - * The first runs are not long - * enough to continue scanning. + * Array is monotonous sequence + * and therefore already sorted. */ - return false; + return true; } - if (++count == MAX_RUN_CAPACITY) { + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; + run[0] = low; + + } else if (a[last - 1] > a[last]) { // Start the new run + if (++count == run.length) { /* * Array is not highly structured. */ return false; } + } - if (count == run.length) { + /* + * Save the current run. + */ + run[count] = (last = k); - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; } - run[count] = (last = k); } /* - * Merge runs of highly structured array. + * Merge all runs. */ if (count > 1) { long[] b; int offset = low; - if (sorter == null || (b = (long[]) sorter.b) == null) { - b = new long[size]; - } else { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; + } else if ((b = tryAllocate(long[].class, high - low)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -1678,15 +1702,13 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) * @return the destination where runs are merged */ private static long[] mergeRuns(long[] a, long[] b, int offset, - int aim, boolean parallel, int[] run, int lo, int hi) { + int aim, boolean parallel, int[] run, int lo, int hi) { if (hi - lo == 1) { if (aim >= 0) { return a; } - for (int i = run[hi], j = i - offset, low = run[lo]; i > low; - b[--j] = a[--i] - ); + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); return b; } @@ -1697,19 +1719,10 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, while (run[++mi + 1] <= rmi); /* - * Merge the left and right parts. + * Merge runs of each part. */ - long[] a1, a2; - - if (parallel && hi - lo > MIN_RUN_COUNT) { - RunMerger merger = new RunMerger(a, b, offset, 0, run, mi, hi).forkMe(); - a1 = mergeRuns(a, b, offset, -aim, true, run, lo, mi); - a2 = (long[]) merger.getDestination(); - } else { - a1 = mergeRuns(a, b, offset, -aim, false, run, lo, mi); - a2 = mergeRuns(a, b, offset, 0, false, run, mi, hi); - } - + long[] a1 = mergeRuns(a, b, offset, -aim, parallel, run, lo, mi); + long[] a2 = mergeRuns(a, b, offset, 0, parallel, run, mi, hi); long[] dst = a1 == a ? b : a; int k = a1 == a ? run[lo] - offset : run[lo]; @@ -1718,8 +1731,11 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, int lo2 = a2 == b ? run[mi] - offset : run[mi]; int hi2 = a2 == b ? run[hi] - offset : run[hi]; - if (parallel) { - new Merger(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); + /* + * Merge the left and right parts. + */ + if (hi1 - lo1 > MIN_PARALLEL_SORT_SIZE && parallel) { + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -1739,9 +1755,12 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, * @param lo2 the start index of the second part, inclusive * @param hi2 the end index of the second part, exclusive */ - private static void mergeParts(Merger merger, long[] dst, int k, - long[] a1, int lo1, int hi1, long[] a2, int lo2, int hi2) { + private static void mergeParts(Merger merger, long[] dst, int k, + long[] a1, int lo1, int hi1, long[] a2, int lo2, int hi2) { + /* + * Merge sorted parts in parallel. + */ if (merger != null && a1 == a2) { while (true) { @@ -1754,10 +1773,18 @@ private static void mergeParts(Merger merger, long[] dst, int k, int hi = hi1; hi1 = hi2; hi2 = hi; } + /* + * Terminate, if the second part is empty. + */ + if (lo2 == hi2) { + System.arraycopy(a1, lo1, dst, k, hi1 - lo1); + return; + } + /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -1769,27 +1796,30 @@ private static void mergeParts(Merger merger, long[] dst, int k, int mi2 = hi2; /* - * Partition the smaller part. + * Divide the smaller part. */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; + for (int mi0 = lo2; mi0 < mi2; ) { + int m = (mi0 + mi2) >>> 1; - if (key > a2[t]) { - loo = t + 1; + if (key > a2[m]) { + mi0 = m + 1; } else { - mi2 = t; + mi2 = m; } } - int d = mi2 - lo2 + mi1 - lo1; + /* + * Reserve space for the left parts. + */ + int space = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge other parts in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + space, mi1, hi1, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left parts. */ hi1 = mi1; hi2 = mi2; @@ -1799,8 +1829,10 @@ private static void mergeParts(Merger merger, long[] dst, int k, /* * Merge small parts sequentially. */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + if (lo2 < hi2 && a1[hi1 - 1] > a2[lo2]) { + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } } if (dst != a1 || k < lo1) { while (lo1 < hi1) { @@ -1814,53 +1846,218 @@ private static void mergeParts(Merger merger, long[] dst, int k, } } -// [byte] - /** - * Sorts the specified range of the array using - * counting sort or insertion sort. + * Tries to sort the specified range of the array + * using LSD (The Least Significant Digit) Radix sort. * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - static void sort(byte[] a, int low, int high) { - if (high - low > MIN_BYTE_COUNTING_SORT_SIZE) { - countingSort(a, low, high); - } else { - insertionSort(a, low, high); + static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { + long[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = sorter.b) != null) { + offset = sorter.offset; + } else if ((b = tryAllocate(long[].class, size)) == null) { + return false; + } + + int start = low - offset; + int last = high - offset; + + /* + * Count the number of all digits. + */ + int[] count1 = new int[1024]; + int[] count2 = new int[2048]; + int[] count3 = new int[2048]; + int[] count4 = new int[2048]; + int[] count5 = new int[2048]; + int[] count6 = new int[1024]; + + for (int i = low; i < high; ++i) { + ++count1[(int) (a[i] & 0x3FF)]; + ++count2[(int) ((a[i] >>> 10) & 0x7FF)]; + ++count3[(int) ((a[i] >>> 21) & 0x7FF)]; + ++count4[(int) ((a[i] >>> 32) & 0x7FF)]; + ++count5[(int) ((a[i] >>> 43) & 0x7FF)]; + ++count6[(int) ((a[i] >>> 54) ^ 0x200)]; // Reverse the sign bit + } + + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, size, low); + boolean processDigit2 = processDigit(count2, size, low); + boolean processDigit3 = processDigit(count3, size, low); + boolean processDigit4 = processDigit(count4, size, low); + boolean processDigit5 = processDigit(count5, size, low); + boolean processDigit6 = processDigit(count6, size, low); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = high; i > low; ) { + b[--count1[(int) (a[--i] & 0x3FF)] - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = last; i > start; ) { + a[--count2[(int) ((b[--i] >>> 10) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count2[(int) ((a[--i] >>> 10) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = last; i > start; ) { + a[--count3[(int) ((b[--i] >>> 21) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count3[(int) ((a[--i] >>> 21) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { + for (int i = last; i > start; ) { + a[--count4[(int) ((b[--i] >>> 32) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count4[(int) ((a[--i] >>> 32) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 5-th digit. + */ + if (processDigit5) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { + for (int i = last; i > start; ) { + a[--count5[(int) ((b[--i] >>> 43) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count5[(int) ((a[--i] >>> 43) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 6-th digit. + */ + if (processDigit6) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5) { + for (int i = last; i > start; ) { + a[--count6[(int) ((b[--i] >>> 54) ^ 0x200)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count6[(int) ((a[--i] >>> 54) ^ 0x200)] - offset] = a[i]; + } + } + } + + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5 ^ processDigit6) { + System.arraycopy(b, low - offset, a, low, size); } + return true; } /** - * Sorts the specified range of the array using insertion sort. + * Sorts the specified range of the array using heap sort. * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(byte[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - byte ai = a[i = k]; + static void heapSort(long[] a, int low, int high) { + for (int k = (low + high) >>> 1; k > low; ) { + pushDown(a, --k, a[k], low, high); + } + while (--high > low) { + long max = a[low]; + pushDown(a, low, a[high], low, high); + a[high] = max; + } + } - if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + /** + * Pushes specified element down during heap sort. + * + * @param a the given array + * @param p the start index + * @param value the given element + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + private static void pushDown(long[] a, int p, long value, int low, int high) { + for (int k ;; a[p] = a[p = k]) { + k = (p << 1) - low + 2; // Index of the right child + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; } } + a[p] = value; } +// #[byte] + /** - * The number of distinct byte values. + * Sorts the specified range of the array using + * counting sort or insertion sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted */ - private static final int NUM_BYTE_VALUES = 1 << 8; + static void sort(byte[] a, int low, int high) { + if (high - low > MIN_BYTE_COUNTING_SORT_SIZE) { + countingSort(a, low, high); + } else { + insertionSort(a, low, high); + } + } /** - * Max index of byte counter. + * The number of distinct byte values. */ - private static final int MAX_BYTE_INDEX = Byte.MAX_VALUE + NUM_BYTE_VALUES + 1; + private static final int NUM_BYTE_VALUES = 1 << 8; /** * Sorts the specified range of the array using counting sort. @@ -1873,36 +2070,46 @@ private static void countingSort(byte[] a, int low, int high) { int[] count = new int[NUM_BYTE_VALUES]; /* - * Compute a histogram with the number of each values. + * Compute the histogram. */ for (int i = high; i > low; ++count[a[--i] & 0xFF]); /* - * Place values on their final positions. + * Put values on their final positions. */ - if (high - low > NUM_BYTE_VALUES) { - for (int i = MAX_BYTE_INDEX; --i > Byte.MAX_VALUE; ) { - int value = i & 0xFF; + for (int i = Byte.MAX_VALUE + 1; high > low; ) { + while (count[--i & 0xFF] == 0); - for (low = high - count[value]; high > low; - a[--high] = (byte) value - ); - } - } else { - for (int i = MAX_BYTE_INDEX; high > low; ) { - while (count[--i & 0xFF] == 0); + int num = count[i & 0xFF]; + + do { + a[--high] = (byte) i; + } while (--num > 0); + } + } - int value = i & 0xFF; - int c = count[value]; + /** + * Sorts the specified range of the array using insertion sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + static void insertionSort(byte[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + byte ai = a[i = k]; + if (ai < a[i - 1]) { do { - a[--high] = (byte) value; - } while (--c > 0); + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; } } } -// [char] +// #[char] /** * Sorts the specified range of the array using @@ -1913,7 +2120,7 @@ private static void countingSort(byte[] a, int low, int high) { * @param high the index of the last element, exclusive, to be sorted */ static void sort(char[] a, int low, int high) { - if (high - low > MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE) { + if (high - low > MIN_CHAR_COUNTING_SORT_SIZE) { countingSort(a, low, high); } else { sort(a, 0, low, high); @@ -1921,21 +2128,62 @@ static void sort(char[] a, int low, int high) { } /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * The number of distinct char values. + */ + private static final int NUM_CHAR_VALUES = 1 << 16; + + /** + * Sorts the specified range of the array using counting sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + private static void countingSort(char[] a, int low, int high) { + int[] count = new int[NUM_CHAR_VALUES]; + + /* + * Compute the histogram. + */ + for (int i = high; i > low; ++count[a[--i]]); + + /* + * Put values on their final positions. + */ + if (high - low > NUM_CHAR_VALUES) { + for (int i = NUM_CHAR_VALUES; i > 0; ) { + for (low = high - count[--i]; high > low; ) { + a[--high] = (char) i; + } + } + } else { + for (int i = NUM_CHAR_VALUES; high > low; ) { + while (count[--i] == 0); + + int num = count[i]; + + do { + a[--high] = (char) i; + } while (--num > 0); + } + } + } + + /** + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ static void sort(char[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; /* - * Invoke insertion sort on small leftmost part. + * Invoke insertion sort on small part. */ if (size < MAX_INSERTION_SORT_SIZE) { insertionSort(a, low, high); @@ -1943,19 +2191,19 @@ static void sort(char[] a, int bits, int low, int high) { } /* - * Switch to counting sort if execution - * time is becoming quadratic. + * Switch to counting sort, if execution time is quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += 2) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -1963,6 +2211,7 @@ static void sort(char[] a, int bits, int low, int high) { * unequal choice of spacing these elements was empirically * determined to work well on a wide variety of inputs. */ + int end = high - 1; int e1 = low + step; int e5 = end - step; int e3 = (e1 + e5) >>> 1; @@ -1971,23 +2220,26 @@ static void sort(char[] a, int bits, int low, int high) { char a3 = a[e3]; /* - * Sort these elements in place by the combination + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------o-------o----------------- */ - if (a[e5] < a[e2]) { char t = a[e5]; a[e5] = a[e2]; a[e2] = t; } - if (a[e4] < a[e1]) { char t = a[e4]; a[e4] = a[e1]; a[e1] = t; } - if (a[e5] < a[e4]) { char t = a[e5]; a[e5] = a[e4]; a[e4] = t; } - if (a[e2] < a[e1]) { char t = a[e2]; a[e2] = a[e1]; a[e1] = t; } - if (a[e4] < a[e2]) { char t = a[e4]; a[e4] = a[e2]; a[e2] = t; } + if (a[e1] > a[e4]) { char t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { char t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { char t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { char t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { char t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2007,7 +2259,7 @@ static void sort(char[] a, int bits, int low, int high) { int upper = end; // The index of the first element of the right part /* - * Partitioning with 2 pivots in case of different elements. + * Partitioning with two pivots on array of fully random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2038,39 +2290,35 @@ static void sort(char[] a, int bits, int low, int high) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { char ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -2089,7 +2337,7 @@ static void sort(char[] a, int bits, int low, int high) { sort(a, bits | 1, lower + 1, upper); sort(a, bits | 1, upper + 1, high); - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -2107,23 +2355,19 @@ static void sort(char[] a, int bits, int low, int high) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int k = ++upper; --k > lower; ) { char ak = a[k]; @@ -2131,14 +2375,14 @@ static void sort(char[] a, int bits, int low, int high) { if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -2167,23 +2411,42 @@ static void sort(char[] a, int bits, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(char[] a, int low, int high) { + static void insertionSort(char[] a, int low, int high) { for (int i, k = low; ++k < high; ) { char ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; } } } +// #[short] + /** - * The number of distinct char values. + * Sorts the specified range of the array using + * counting sort or Dual-Pivot Quicksort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted */ - private static final int NUM_CHAR_VALUES = 1 << 16; + static void sort(short[] a, int low, int high) { + if (high - low > MIN_SHORT_COUNTING_SORT_SIZE) { + countingSort(a, low, high); + } else { + sort(a, 0, low, high); + } + } + + /** + * The number of distinct short values. + */ + private static final int NUM_SHORT_VALUES = 1 << 16; /** * Sorts the specified range of the array using counting sort. @@ -2192,69 +2455,51 @@ private static void insertionSort(char[] a, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void countingSort(char[] a, int low, int high) { - int[] count = new int[NUM_CHAR_VALUES]; + private static void countingSort(short[] a, int low, int high) { + int[] count = new int[NUM_SHORT_VALUES]; /* - * Compute a histogram with the number of each values. + * Compute the histogram. */ - for (int i = high; i > low; ++count[a[--i]]); + for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); /* * Place values on their final positions. */ - if (high - low > NUM_CHAR_VALUES) { - for (int i = NUM_CHAR_VALUES; i > 0; ) { - for (low = high - count[--i]; high > low; - a[--high] = (char) i + if (high - low > NUM_SHORT_VALUES) { + for (int i = Short.MAX_VALUE; i >= Short.MIN_VALUE; --i) { + for (low = high - count[i & 0xFFFF]; high > low; + a[--high] = (short) i ); } } else { - for (int i = NUM_CHAR_VALUES; high > low; ) { - while (count[--i] == 0); - int c = count[i]; + for (int i = Short.MAX_VALUE + 1; high > low; ) { + while (count[--i & 0xFFFF] == 0); + + int num = count[i & 0xFFFF]; do { - a[--high] = (char) i; - } while (--c > 0); + a[--high] = (short) i; + } while (--num > 0); } } } -// [short] - - /** - * Sorts the specified range of the array using - * counting sort or Dual-Pivot Quicksort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - static void sort(short[] a, int low, int high) { - if (high - low > MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE) { - countingSort(a, low, high); - } else { - sort(a, 0, low, high); - } - } - /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ static void sort(short[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; /* - * Invoke insertion sort on small leftmost part. + * Invoke insertion sort on small part. */ if (size < MAX_INSERTION_SORT_SIZE) { insertionSort(a, low, high); @@ -2262,19 +2507,19 @@ static void sort(short[] a, int bits, int low, int high) { } /* - * Switch to counting sort if execution - * time is becoming quadratic. + * Switch to counting sort, if execution time is quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += 2) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -2282,6 +2527,7 @@ static void sort(short[] a, int bits, int low, int high) { * unequal choice of spacing these elements was empirically * determined to work well on a wide variety of inputs. */ + int end = high - 1; int e1 = low + step; int e5 = end - step; int e3 = (e1 + e5) >>> 1; @@ -2290,23 +2536,26 @@ static void sort(short[] a, int bits, int low, int high) { short a3 = a[e3]; /* - * Sort these elements in place by the combination + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------o-------o----------------- */ - if (a[e5] < a[e2]) { short t = a[e5]; a[e5] = a[e2]; a[e2] = t; } - if (a[e4] < a[e1]) { short t = a[e4]; a[e4] = a[e1]; a[e1] = t; } - if (a[e5] < a[e4]) { short t = a[e5]; a[e5] = a[e4]; a[e4] = t; } - if (a[e2] < a[e1]) { short t = a[e2]; a[e2] = a[e1]; a[e1] = t; } - if (a[e4] < a[e2]) { short t = a[e4]; a[e4] = a[e2]; a[e2] = t; } + if (a[e1] > a[e4]) { short t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { short t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { short t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { short t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { short t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2326,7 +2575,7 @@ static void sort(short[] a, int bits, int low, int high) { int upper = end; // The index of the first element of the right part /* - * Partitioning with 2 pivots in case of different elements. + * Partitioning with two pivots on array of fully random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2357,39 +2606,35 @@ static void sort(short[] a, int bits, int low, int high) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { short ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -2408,7 +2653,7 @@ static void sort(short[] a, int bits, int low, int high) { sort(a, bits | 1, lower + 1, upper); sort(a, bits | 1, upper + 1, high); - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -2426,23 +2671,19 @@ static void sort(short[] a, int bits, int low, int high) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int k = ++upper; --k > lower; ) { short ak = a[k]; @@ -2450,14 +2691,14 @@ static void sort(short[] a, int bits, int low, int high) { if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -2486,70 +2727,21 @@ static void sort(short[] a, int bits, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(short[] a, int low, int high) { + static void insertionSort(short[] a, int low, int high) { for (int i, k = low; ++k < high; ) { short ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * The number of distinct short values. - */ - private static final int NUM_SHORT_VALUES = 1 << 16; - - /** - * Max index of short counter. - */ - private static final int MAX_SHORT_INDEX = Short.MAX_VALUE + NUM_SHORT_VALUES + 1; - - /** - * Sorts the specified range of the array using counting sort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void countingSort(short[] a, int low, int high) { - int[] count = new int[NUM_SHORT_VALUES]; - - /* - * Compute a histogram with the number of each values. - */ - for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); - - /* - * Place values on their final positions. - */ - if (high - low > NUM_SHORT_VALUES) { - for (int i = MAX_SHORT_INDEX; --i > Short.MAX_VALUE; ) { - int value = i & 0xFFFF; - - for (low = high - count[value]; high > low; - a[--high] = (short) value - ); - } - } else { - for (int i = MAX_SHORT_INDEX; high > low; ) { - while (count[--i & 0xFFFF] == 0); - - int value = i & 0xFFFF; - int c = count[value]; - do { - a[--high] = (short) value; - } while (--c > 0); + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; } } } -// [float] +// #[float] /** * Sorts the specified range of the array using parallel merge @@ -2590,12 +2782,8 @@ static void sort(float[] a, int parallelism, int low, int high) { * Phase 2. Sort everything except NaNs, * which are already in place. */ - int size = high - low; - - if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { - int depth = getDepth(parallelism, size >> 12); - float[] b = depth == 0 ? null : new float[size]; - new Sorter(null, a, b, low, size, low, depth).invoke(); + if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -2631,25 +2819,25 @@ static void sort(float[] a, int parallelism, int low, int high) { } /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param sorter parallel context * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - static void sort(Sorter sorter, float[] a, int bits, int low, int high) { + static void sort(Sorter sorter, float[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; /* - * Run mixed insertion sort on small non-leftmost parts. + * Run adaptive mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - sort(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, low, high, DualPivotQuicksort::mixedInsertionSort); + sort(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, + low, high, DualPivotQuicksort::mixedInsertionSort); return; } @@ -2657,33 +2845,25 @@ static void sort(Sorter sorter, float[] a, int bits, int low, int high) { * Invoke insertion sort on small leftmost part. */ if (size < MAX_INSERTION_SORT_SIZE) { - sort(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, low, high, DualPivotQuicksort::insertionSort); - return; - } - - /* - * Check if the whole array or large non-leftmost - * parts are nearly sorted and then merge runs. - */ - if ((bits == 0 || size > MIN_TRY_MERGE_SIZE && (bits & 1) > 0) - && tryMergeRuns(sorter, a, low, size)) { + sort(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, + low, high, DualPivotQuicksort::insertionSort); return; } /* - * Switch to heap sort if execution - * time is becoming quadratic. + * Try merging sort on large part. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); + if (size > MIN_MERGING_SORT_SIZE * bits + && tryMergingSort(sorter, a, low, high)) { return; } /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -2692,30 +2872,42 @@ && tryMergeRuns(sorter, a, low, size)) { * determined to work well on a wide variety of inputs. */ int e1 = low + step; - int e5 = end - step; + int e5 = high - step; int e3 = (e1 + e5) >>> 1; int e2 = (e1 + e3) >>> 1; int e4 = (e3 + e5) >>> 1; float a3 = a[e3]; /* - * Sort these elements in place by the combination + * Check if part is large and contains random + * data, taking into account parallel context. + */ + boolean isLargeRandom = + sorter != null && bits > 2 && size > MIN_RADIX_SORT_SIZE && +// size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > 0) && + (a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]); + + /* + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------o-------o----------------- */ - if (a[e5] < a[e2]) { float t = a[e5]; a[e5] = a[e2]; a[e2] = t; } - if (a[e4] < a[e1]) { float t = a[e4]; a[e4] = a[e1]; a[e1] = t; } - if (a[e5] < a[e4]) { float t = a[e5]; a[e5] = a[e4]; a[e4] = t; } - if (a[e2] < a[e1]) { float t = a[e2]; a[e2] = a[e1]; a[e1] = t; } - if (a[e4] < a[e2]) { float t = a[e4]; a[e4] = a[e2]; a[e2] = t; } + if (a[e1] > a[e4]) { float t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { float t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { float t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { float t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { float t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2730,79 +2922,91 @@ && tryMergeRuns(sorter, a, low, size)) { } } - // Pointers - int lower; // The index of the last element of the left part - int upper; // The index of the first element of the right part + /* + * Try Radix sort on large fully random data. + */ + if (isLargeRandom + && a[e2] < a[e3] && a[e3] < a[e4] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Switch to heap sort, if execution time is quadratic. + */ + if ((bits += 2) > MAX_RECURSION_DEPTH) { + heapSort(a, low, high); + return; + } + + /* + * indices[0] - the index of the last element of the left part + * indices[1] - the index of the first element of the right part + */ + int[] indices; /* - * Partitioning with 2 pivots in case of different elements. + * Partitioning with two pivots on array of fully random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Use the first and fifth of the five sorted elements as - * the pivots. These values are inexpensive approximation - * of tertiles. Note, that pivot1 < pivot2. - */ - int[] pivotIndices = partition(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, low, high, e1, e5, DualPivotQuicksort::partitionDualPivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; + indices = partition(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, + low, high, e1, e5, DualPivotQuicksort::partitionWithTwoPivots); + /* * Sort non-left parts recursively (possibly in parallel), * excluding known pivots. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, lower + 1, upper); - sorter.forkSorter(bits | 1, upper + 1, high); + sorter.fork(bits | 1, indices[0] + 1, indices[1]); + sorter.fork(bits | 1, indices[1] + 1, high); } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); + sort(sorter, a, bits | 1, indices[0] + 1, indices[1]); + sort(sorter, a, bits | 1, indices[1] + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot + + indices = partition(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, + low, high, e3, e3, DualPivotQuicksort::partitionWithOnePivot); - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - int[] pivotIndices = partition(float.class, a, Unsafe.ARRAY_FLOAT_BASE_OFFSET, low, high, e3, e3, DualPivotQuicksort::partitionSinglePivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; /* * Sort the right part (possibly in parallel), excluding * known pivot. All elements from the central part are * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, indices[1], high); } else { - sort(sorter, a, bits | 1, upper, high); + sort(sorter, a, bits | 1, indices[1], high); } } - high = lower; // Iterate along the left part + high = indices[0]; // Iterate along the left part } } /** - * Partitions the specified range of the array using the two pivots provided. + * Partitions the specified range of the array using two given pivots. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionDualPivot(float[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - int end = high - 1; + private static int[] partitionWithTwoPivots( + float[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = --high; int lower = low; - int upper = end; - int e1 = pivotIndex1; - int e5 = pivotIndex2; - float pivot1 = a[e1]; - float pivot2 = a[e5]; + /* + * Use the first and fifth of the five sorted elements as + * the pivots. These values are inexpensive approximation + * of tertiles. Note, that pivot1 < pivot2. + */ + float pivot1 = a[pivotIndex1]; + float pivot2 = a[pivotIndex2]; /* * The first and the last elements to be sorted are moved @@ -2811,8 +3015,8 @@ private static int[] partitionDualPivot(float[] a, int low, int high, int pivotI * into their final positions, and excluded from the next * subsequent sorting. */ - a[e1] = a[lower]; - a[e5] = a[upper]; + a[pivotIndex1] = a[lower]; + a[pivotIndex2] = a[upper]; /* * Skip elements, which are less or greater than the pivots. @@ -2823,39 +3027,35 @@ private static int[] partitionDualPivot(float[] a, int low, int high, int pivotI /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { float ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -2864,30 +3064,32 @@ private static int[] partitionDualPivot(float[] a, int low, int high, int pivotI /* * 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[high] = a[upper]; a[upper] = pivot2; - return new int[] {lower, upper}; + return new int[] { lower, upper }; } /** - * Partitions the specified range of the array using a single pivot provided. + * Partitions the specified range of the array using one given pivot. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @param pivotIndex1 the index of single pivot + * @param pivotIndex2 the index of single pivot + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionSinglePivot(float[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - int end = high - 1; + private static int[] partitionWithOnePivot( + float[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = high; int lower = low; - int upper = end; - int e3 = pivotIndex1; - float pivot = a[e3]; + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + float pivot = a[pivotIndex1]; /* * The first element to be sorted is moved to the @@ -2896,41 +3098,37 @@ private static int[] partitionSinglePivot(float[] a, int low, int high, int pivo * back into its final position, and excluded from * the next subsequent sorting. */ - a[e3] = a[lower]; + a[pivotIndex1] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ - for (int k = ++upper; --k > lower; ) { + for (int k = upper; --k > lower; ) { float ak = a[k]; if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -2940,14 +3138,15 @@ private static int[] partitionSinglePivot(float[] a, int low, int high, int pivo * Swap the pivot into its final position. */ a[low] = a[lower]; a[lower] = pivot; - return new int[] {lower, upper}; + + return new int[] { lower, upper }; } /** * Sorts the specified range of the array using mixed insertion sort. * - * Mixed insertion sort is combination of simple insertion sort, - * pin insertion sort and pair insertion sort. + * Mixed insertion sort is combination of pin insertion sort, + * simple insertion sort and pair insertion sort. * * In the context of Dual-Pivot Quicksort, the pivot element * from the left part plays the role of sentinel, because it @@ -2959,110 +3158,85 @@ private static int[] partitionSinglePivot(float[] a, int low, int high, int pivo * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(float[] a, int low, int high) { - int size = high - low; - int end = high - 3 * ((size >> 5) << 3); - if (end == high) { + static void mixedInsertionSort(float[] a, int low, int high) { - /* - * Invoke simple insertion sort on tiny array. - */ - for (int i; ++low < end; ) { + /* + * Split part for pin and pair insertion sorts. + */ + int end = high - 3 * ((high - low) >> 3 << 1); + + /* + * Invoke simple insertion sort on small part. + */ + if (end == high) { + for (int i; ++low < high; ) { float ai = a[i = low]; - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[i - 1]) { + a[i] = a[--i]; } - a[i + 1] = ai; + a[i] = ai; } - } else { + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + float ai = a[i = low], pin = a[--p]; /* - * Start with pin insertion sort on small part. - * - * Pin insertion sort is extended simple insertion sort. - * The main idea of this sort is to put elements larger - * than an element called pin to the end of array (the - * proper area for such elements). It avoids expensive - * movements of these elements through the whole array. + * Swap larger element with pin. */ - float pin = a[end]; - - for (int i, p = high; ++low < end; ) { - float ai = a[i = low]; - - if (ai < a[i - 1]) { // Small element - - /* - * Insert small element into sorted part. - */ - a[i] = a[--i]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - - } else if (p > i && ai > pin) { // Large element - - /* - * Find element smaller than pin. - */ - while (a[--p] > pin); - - /* - * Swap it with large element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } - - /* - * Insert small element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } + if (ai > pin) { + ai = pin; + a[p] = a[i]; } /* - * Continue with pair insertion sort on remain part. + * Insert element into sorted part. */ - for (int i; low < high; ++low) { - float a1 = a[i = low], a2 = a[++low]; + while (ai < a[i - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } - /* - * Insert two elements per iteration: at first, insert the - * larger element and then insert the smaller element, but - * from the position where the larger element was inserted. - */ - if (a1 > a2) { + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + float a1 = a[i = low], a2 = a[++low]; - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + /* + * Insert two elements per iteration: at first, insert the + * larger element and then insert the smaller element, but + * from the position where the larger element was inserted. + */ + if (a1 > a2) { - while (a2 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a2; + while (a1 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a1; - } else if (a1 < a[i - 1]) { + while (a2 < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = a2; - while (a2 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a2; + } else if (a1 < a[i - 1]) { - while (a1 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a1; + while (a2 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a2; + + while (a1 < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = a1; } } } @@ -3074,90 +3248,45 @@ private static void mixedInsertionSort(float[] a, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(float[] a, int low, int high) { + static void insertionSort(float[] a, int low, int high) { for (int i, k = low; ++k < high; ) { float ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Sorts the specified range of the array using heap sort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void heapSort(float[] a, int low, int high) { - for (int k = (low + high) >>> 1; k > low; ) { - pushDown(a, --k, a[k], low, high); - } - while (--high > low) { - float max = a[low]; - pushDown(a, low, a[high], low, high); - a[high] = max; - } - } - - /** - * Pushes specified element down during heap sort. - * - * @param a the given array - * @param p the start index - * @param value the given element - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void pushDown(float[] a, int p, float value, int low, int high) { - for (int k ;; a[p] = a[p = k]) { - k = (p << 1) - low + 2; // Index of the right child + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + a[i ] = ai; } } - a[p] = value; } /** - * Tries to sort the specified range of the array. + * Tries to sort the specified range of the array using merging sort. * * @param sorter parallel context * @param a the array to be sorted - * @param low the index of the first element to be sorted - * @param size the array size - * @return true if finally sorted, false otherwise + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, float[] a, int low, int high) { /* - * The run array is constructed only if initial runs are - * long enough to continue, run[i] then holds start index - * of the i-th sequence of elements in non-descending order. + * The element run[i] holds the start index + * of i-th sequence in non-descending order. */ + int count = 1; int[] run = null; - int high = low + size; - int count = 1, last = low; /* * Identify all possible runs. */ - for (int k = low + 1; k < high; ) { + for (int k = low + 1, last = low; k < high; ) { /* - * Find the end index of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -3182,70 +3311,61 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) } /* - * Check special cases. + * Check if the runs are too long to continue scanning. + */ + if (count > 6 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { - if (k == high) { + if (k == high) { /* - * The array is monotonous sequence, + * Array is monotonous sequence * and therefore already sorted. */ return true; } - if (k - low < MIN_FIRST_RUN_SIZE) { - - /* - * The first run is too small - * to proceed with scanning. - */ - return false; - } - - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } - - if (++count == MAX_RUN_CAPACITY) { + } else if (a[last - 1] > a[last]) { // Start the new run + if (++count == run.length) { /* * Array is not highly structured. */ return false; } + } - if (count == run.length) { + /* + * Save the current run. + */ + run[count] = (last = k); - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; } - run[count] = (last = k); } /* - * Merge runs of highly structured array. + * Merge all runs. */ if (count > 1) { float[] b; int offset = low; - if (sorter == null || (b = (float[]) sorter.b) == null) { - b = new float[size]; - } else { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; + } else if ((b = tryAllocate(float[].class, high - low)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -3266,15 +3386,13 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) * @return the destination where runs are merged */ private static float[] mergeRuns(float[] a, float[] b, int offset, - int aim, boolean parallel, int[] run, int lo, int hi) { + int aim, boolean parallel, int[] run, int lo, int hi) { if (hi - lo == 1) { if (aim >= 0) { return a; } - for (int i = run[hi], j = i - offset, low = run[lo]; i > low; - b[--j] = a[--i] - ); + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); return b; } @@ -3285,19 +3403,10 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, while (run[++mi + 1] <= rmi); /* - * Merge the left and right parts. + * Merge runs of each part. */ - float[] a1, a2; - - if (parallel && hi - lo > MIN_RUN_COUNT) { - RunMerger merger = new RunMerger(a, b, offset, 0, run, mi, hi).forkMe(); - a1 = mergeRuns(a, b, offset, -aim, true, run, lo, mi); - a2 = (float[]) merger.getDestination(); - } else { - a1 = mergeRuns(a, b, offset, -aim, false, run, lo, mi); - a2 = mergeRuns(a, b, offset, 0, false, run, mi, hi); - } - + float[] a1 = mergeRuns(a, b, offset, -aim, parallel, run, lo, mi); + float[] a2 = mergeRuns(a, b, offset, 0, parallel, run, mi, hi); float[] dst = a1 == a ? b : a; int k = a1 == a ? run[lo] - offset : run[lo]; @@ -3306,8 +3415,11 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, int lo2 = a2 == b ? run[mi] - offset : run[mi]; int hi2 = a2 == b ? run[hi] - offset : run[hi]; - if (parallel) { - new Merger(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); + /* + * Merge the left and right parts. + */ + if (hi1 - lo1 > MIN_PARALLEL_SORT_SIZE && parallel) { + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -3327,9 +3439,12 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, * @param lo2 the start index of the second part, inclusive * @param hi2 the end index of the second part, exclusive */ - private static void mergeParts(Merger merger, float[] dst, int k, - float[] a1, int lo1, int hi1, float[] a2, int lo2, int hi2) { + private static void mergeParts(Merger merger, float[] dst, int k, + float[] a1, int lo1, int hi1, float[] a2, int lo2, int hi2) { + /* + * Merge sorted parts in parallel. + */ if (merger != null && a1 == a2) { while (true) { @@ -3342,10 +3457,18 @@ private static void mergeParts(Merger merger, float[] dst, int k, int hi = hi1; hi1 = hi2; hi2 = hi; } + /* + * Terminate, if the second part is empty. + */ + if (lo2 == hi2) { + System.arraycopy(a1, lo1, dst, k, hi1 - lo1); + return; + } + /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -3357,27 +3480,30 @@ private static void mergeParts(Merger merger, float[] dst, int k, int mi2 = hi2; /* - * Partition the smaller part. + * Divide the smaller part. */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; + for (int mi0 = lo2; mi0 < mi2; ) { + int m = (mi0 + mi2) >>> 1; - if (key > a2[t]) { - loo = t + 1; + if (key > a2[m]) { + mi0 = m + 1; } else { - mi2 = t; + mi2 = m; } } - int d = mi2 - lo2 + mi1 - lo1; + /* + * Reserve space for the left parts. + */ + int space = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge other parts in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + space, mi1, hi1, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left parts. */ hi1 = mi1; hi2 = mi2; @@ -3387,8 +3513,10 @@ private static void mergeParts(Merger merger, float[] dst, int k, /* * Merge small parts sequentially. */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + if (lo2 < hi2 && a1[hi1 - 1] > a2[lo2]) { + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } } if (dst != a1 || k < lo1) { while (lo1 < hi1) { @@ -3402,7 +3530,154 @@ private static void mergeParts(Merger merger, float[] dst, int k, } } -// [double] + /** + * Tries to sort the specified range of the array + * using LSD (The Least Significant Digit) Radix sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} + */ + static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { + float[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = sorter.b) != null) { + offset = sorter.offset; + } else if ((b = tryAllocate(float[].class, size)) == null) { + return false; + } + + int start = low - offset; + int last = high - offset; + + /* + * Count the number of all digits. + */ + int[] count1 = new int[1024]; + int[] count2 = new int[2048]; + int[] count3 = new int[2048]; + + for (int i = low; i < high; ++i) { + ++count1[ fti(a[i]) & 0x3FF]; + ++count2[(fti(a[i]) >>> 10) & 0x7FF]; + ++count3[(fti(a[i]) >>> 21) & 0x7FF]; + } + + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, size, low); + boolean processDigit2 = processDigit(count2, size, low); + boolean processDigit3 = processDigit(count3, size, low); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = high; i > low; ) { + b[--count1[fti(a[--i]) & 0x3FF] - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = last; i > start; ) { + a[--count2[(fti(b[--i]) >>> 10) & 0x7FF]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count2[(fti(a[--i]) >>> 10) & 0x7FF] - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = last; i > start; ) { + a[--count3[(fti(b[--i]) >>> 21) & 0x7FF]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count3[(fti(a[--i]) >>> 21) & 0x7FF] - offset] = a[i]; + } + } + } + + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3) { + System.arraycopy(b, low - offset, a, low, size); + } + return true; + } + + /** + * Returns masked bits that represent the float value. + * + * @param f the given value + * @return masked bits + */ + private static int fti(float f) { + int x = Float.floatToRawIntBits(f); + return x ^ ((x >> 31) | 0x80000000); + } + + /** + * Sorts the specified range of the array using heap sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + static void heapSort(float[] a, int low, int high) { + for (int k = (low + high) >>> 1; k > low; ) { + pushDown(a, --k, a[k], low, high); + } + while (--high > low) { + float max = a[low]; + pushDown(a, low, a[high], low, high); + a[high] = max; + } + } + + /** + * Pushes specified element down during heap sort. + * + * @param a the given array + * @param p the start index + * @param value the given element + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + private static void pushDown(float[] a, int p, float value, int low, int high) { + for (int k ;; a[p] = a[p = k]) { + k = (p << 1) - low + 2; // Index of the right child + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + +// #[double] /** * Sorts the specified range of the array using parallel merge @@ -3443,12 +3718,8 @@ static void sort(double[] a, int parallelism, int low, int high) { * Phase 2. Sort everything except NaNs, * which are already in place. */ - int size = high - low; - - if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { - int depth = getDepth(parallelism, size >> 12); - double[] b = depth == 0 ? null : new double[size]; - new Sorter(null, a, b, low, size, low, depth).invoke(); + if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -3484,24 +3755,25 @@ static void sort(double[] a, int parallelism, int low, int high) { } /** - * Sorts the specified array using the Dual-Pivot Quicksort and/or - * other sorts in special-cases, possibly with parallel partitions. + * Sorts the specified range of the array using Dual-Pivot Quicksort. * * @param sorter parallel context * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where - * the right bit "0" indicates that array is the leftmost part + * the right bit "0" indicates that range is the leftmost part * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - static void sort(Sorter sorter, double[] a, int bits, int low, int high) { + static void sort(Sorter sorter, double[] a, int bits, int low, int high) { while (true) { - int end = high - 1, size = high - low; + int size = high - low; + /* - * Run mixed insertion sort on small non-leftmost parts. + * Run adaptive mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - sort(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, low, high, DualPivotQuicksort::mixedInsertionSort); + sort(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, + low, high, DualPivotQuicksort::mixedInsertionSort); return; } @@ -3509,33 +3781,25 @@ static void sort(Sorter sorter, double[] a, int bits, int low, int high) { * Invoke insertion sort on small leftmost part. */ if (size < MAX_INSERTION_SORT_SIZE) { - sort(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, low, high, DualPivotQuicksort::insertionSort); - return; - } - - /* - * Check if the whole array or large non-leftmost - * parts are nearly sorted and then merge runs. - */ - if ((bits == 0 || size > MIN_TRY_MERGE_SIZE && (bits & 1) > 0) - && tryMergeRuns(sorter, a, low, size)) { + sort(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, + low, high, DualPivotQuicksort::insertionSort); return; } /* - * Switch to heap sort if execution - * time is becoming quadratic. + * Try merging sort on large part. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); + if (size > MIN_MERGING_SORT_SIZE * bits + && tryMergingSort(sorter, a, low, high)) { return; } /* - * Use an inexpensive approximation of the golden ratio - * to select five sample elements and determine pivots. + * Divide the given array into the golden ratio using + * an inexpensive approximation to select five sample + * elements and determine pivots. */ - int step = (size >> 3) * 3 + 3; + int step = (size >> 2) + (size >> 3) + (size >> 7); /* * Five elements around (and including) the central element @@ -3544,30 +3808,42 @@ && tryMergeRuns(sorter, a, low, size)) { * determined to work well on a wide variety of inputs. */ int e1 = low + step; - int e5 = end - step; + int e5 = high - step; int e3 = (e1 + e5) >>> 1; int e2 = (e1 + e3) >>> 1; int e4 = (e3 + e5) >>> 1; double a3 = a[e3]; /* - * Sort these elements in place by the combination + * Check if part is large and contains random + * data, taking into account parallel context. + */ + boolean isLargeRandom = + sorter != null && bits > 2 && size > MIN_RADIX_SORT_SIZE && +// size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > 0) && + (a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]); + + /* + * Sort these elements in-place by the combination * of 4-element sorting network and insertion sort. * - * 5 ------o-----------o------------ - * | | - * 4 ------|-----o-----o-----o------ - * | | | - * 2 ------o-----|-----o-----o------ - * | | - * 1 ------------o-----o------------ + * 1 ---------o---------------o----------------- + * | | + * 2 ---------|-------o-------o-------o--------- + * | | | + * 4 ---------o-------|-------o-------o--------- + * | | + * 5 -----------------o-------o----------------- */ - if (a[e5] < a[e2]) { double t = a[e5]; a[e5] = a[e2]; a[e2] = t; } - if (a[e4] < a[e1]) { double t = a[e4]; a[e4] = a[e1]; a[e1] = t; } - if (a[e5] < a[e4]) { double t = a[e5]; a[e5] = a[e4]; a[e4] = t; } - if (a[e2] < a[e1]) { double t = a[e2]; a[e2] = a[e1]; a[e1] = t; } - if (a[e4] < a[e2]) { double t = a[e4]; a[e4] = a[e2]; a[e2] = t; } + if (a[e1] > a[e4]) { double t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e2] > a[e5]) { double t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e4] > a[e5]) { double t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e1] > a[e2]) { double t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e2] > a[e4]) { double t = a[e2]; a[e2] = a[e4]; a[e4] = t; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -3582,44 +3858,53 @@ && tryMergeRuns(sorter, a, low, size)) { } } - // Pointers - int lower; // The index of the last element of the left part - int upper; // The index of the first element of the right part + /* + * Try Radix sort on large fully random data. + */ + if (isLargeRandom + && a[e2] < a[e3] && a[e3] < a[e4] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Switch to heap sort, if execution time is quadratic. + */ + if ((bits += 2) > MAX_RECURSION_DEPTH) { + heapSort(a, low, high); + return; + } + + /* + * indices[0] - the index of the last element of the left part + * indices[1] - the index of the first element of the right part + */ + int[] indices; /* - * Partitioning with 2 pivots in case of different elements. + * Partitioning with two pivots on array of fully random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Use the first and fifth of the five sorted elements as - * the pivots. These values are inexpensive approximation - * of tertiles. Note, that pivot1 < pivot2. - */ - int[] pivotIndices = partition(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, low, high, e1, e5, DualPivotQuicksort::partitionDualPivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; + indices = partition(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, + low, high, e1, e5, DualPivotQuicksort::partitionWithTwoPivots); + /* * Sort non-left parts recursively (possibly in parallel), * excluding known pivots. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, lower + 1, upper); - sorter.forkSorter(bits | 1, upper + 1, high); + sorter.fork(bits | 1, indices[0] + 1, indices[1]); + sorter.fork(bits | 1, indices[1] + 1, high); } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); + sort(sorter, a, bits | 1, indices[0] + 1, indices[1]); + sort(sorter, a, bits | 1, indices[1] + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - int[] pivotIndices = partition(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, low, high, e3, e3, DualPivotQuicksort::partitionSinglePivot); - lower = pivotIndices[0]; - upper = pivotIndices[1]; + indices = partition(double.class, a, Unsafe.ARRAY_DOUBLE_BASE_OFFSET, + low, high, e3, e3, DualPivotQuicksort::partitionWithOnePivot); /* * Sort the right part (possibly in parallel), excluding @@ -3627,88 +3912,86 @@ && tryMergeRuns(sorter, a, low, size)) { * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, indices[1], high); } else { - sort(sorter, a, bits | 1, upper, high); + sort(sorter, a, bits | 1, indices[1], high); } } - high = lower; // Iterate along the left part + high = indices[0]; // Iterate along the left part } } /** - * Partitions the specified range of the array using the two pivots provided. + * Partitions the specified range of the array using two given pivots. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 - * + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionDualPivot(double[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - int end = high - 1; + private static int[] partitionWithTwoPivots( + double[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = --high; int lower = low; - int upper = end; - int e1 = pivotIndex1; - int e5 = pivotIndex2; - double pivot1 = a[e1]; - double pivot2 = a[e5]; + /* + * Use the first and fifth of the five sorted elements as + * the pivots. These values are inexpensive approximation + * of tertiles. Note, that pivot1 < pivot2. + */ + double pivot1 = a[pivotIndex1]; + double pivot2 = a[pivotIndex2]; /* - * The first and the last elements to be sorted are moved - * to the locations formerly occupied by the pivots. When - * partitioning is completed, the pivots are swapped back - * into their final positions, and excluded from the next - * subsequent sorting. - */ - a[e1] = a[lower]; - a[e5] = a[upper]; + * The first and the last elements to be sorted are moved + * to the locations formerly occupied by the pivots. When + * partitioning is completed, the pivots are swapped back + * into their final positions, and excluded from the next + * subsequent sorting. + */ + a[pivotIndex1] = a[lower]; + a[pivotIndex2] = a[upper]; /* - * Skip elements, which are less or greater than the pivots. - */ + * Skip elements, which are less or greater than the pivots. + */ while (a[++lower] < pivot1); while (a[--upper] > pivot2); /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * left part central part right part + * +-------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= .. <= pivot2 | > pivot2 | + * +-------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ for (int unused = --lower, k = ++upper; --k > lower; ) { double ak = a[k]; - if (ak < pivot1) { // Move a[k] to the left side - while (lower < k) { - if (a[++lower] >= pivot1) { - if (a[lower] > pivot2) { - a[k] = a[--upper]; - a[upper] = a[lower]; - } else { - a[k] = a[lower]; - } - a[lower] = ak; + if (ak < pivot1) { // Move a[k] to the left part + while (a[++lower] < pivot1) { + if (lower == k) { break; } } - } else if (ak > pivot2) { // Move a[k] to the right side + if (a[lower] > pivot2) { + a[k] = a[--upper]; + a[upper] = a[lower]; + } else { + a[k] = a[lower]; + } + a[lower] = ak; + } else if (ak > pivot2) { // Move a[k] to the right part a[k] = a[--upper]; a[upper] = ak; } @@ -3717,73 +4000,71 @@ private static int[] partitionDualPivot(double[] a, int low, int high, int pivot /* * 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[high] = a[upper]; a[upper] = pivot2; - return new int[] {lower, upper}; + return new int[] { lower, upper }; } /** - * Partitions the specified range of the array using a single pivot provided. + * Partitions the specified range of the array using one given pivot. * - * @param array the array to be partitioned + * @param a the array for partitioning * @param low the index of the first element, inclusive, for partitioning * @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 + * @param pivotIndex1 the index of single pivot + * @param pivotIndex2 the index of single pivot + * @return indices of parts after partitioning */ - @ForceInline - private static int[] partitionSinglePivot(double[] a, int low, int high, int pivotIndex1, int pivotIndex2) { - - int end = high - 1; + private static int[] partitionWithOnePivot( + double[] a, int low, int high, int pivotIndex1, int pivotIndex2) { + int upper = high; int lower = low; - int upper = end; - int e3 = pivotIndex1; - double pivot = a[e3]; + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + double pivot = a[pivotIndex1]; /* - * The first element to be sorted is moved to the - * location formerly occupied by the pivot. After - * completion of partitioning the pivot is swapped - * back into its final position, and excluded from - * the next subsequent sorting. - */ - a[e3] = a[lower]; + * The first element to be sorted is moved to the + * location formerly occupied by the pivot. After + * completion of partitioning the pivot is swapped + * back into its final position, and excluded from + * the next subsequent sorting. + */ + a[pivotIndex1] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > pivot | + * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * ^ ^ ^ + * | | | + * lower k upper * * Pointer k is the last index of ?-part + * Pointer lower is the last index of left part + * Pointer upper is the first index of right part */ - for (int k = ++upper; --k > lower; ) { + for (int k = upper; --k > lower; ) { double ak = a[k]; if (ak != pivot) { a[k] = pivot; - if (ak < pivot) { // Move a[k] to the left side + if (ak < pivot) { // Move a[k] to the left part while (a[++lower] < pivot); if (a[lower] > pivot) { a[--upper] = a[lower]; } a[lower] = ak; - } else { // ak > pivot - Move a[k] to the right side + } else { // ak > pivot - Move a[k] to the right part a[--upper] = ak; } } @@ -3793,14 +4074,15 @@ private static int[] partitionSinglePivot(double[] a, int low, int high, int piv * Swap the pivot into its final position. */ a[low] = a[lower]; a[lower] = pivot; - return new int[] {lower, upper}; + + return new int[] { lower, upper }; } /** * Sorts the specified range of the array using mixed insertion sort. * - * Mixed insertion sort is combination of simple insertion sort, - * pin insertion sort and pair insertion sort. + * Mixed insertion sort is combination of pin insertion sort, + * simple insertion sort and pair insertion sort. * * In the context of Dual-Pivot Quicksort, the pivot element * from the left part plays the role of sentinel, because it @@ -3812,110 +4094,85 @@ private static int[] partitionSinglePivot(double[] a, int low, int high, int piv * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(double[] a, int low, int high) { - int size = high - low; - int end = high - 3 * ((size >> 5) << 3); - if (end == high) { + static void mixedInsertionSort(double[] a, int low, int high) { - /* - * Invoke simple insertion sort on tiny array. - */ - for (int i; ++low < end; ) { + /* + * Split part for pin and pair insertion sorts. + */ + int end = high - 3 * ((high - low) >> 3 << 1); + + /* + * Invoke simple insertion sort on small part. + */ + if (end == high) { + for (int i; ++low < high; ) { double ai = a[i = low]; - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[i - 1]) { + a[i] = a[--i]; } - a[i + 1] = ai; + a[i] = ai; } - } else { + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + double ai = a[i = low], pin = a[--p]; /* - * Start with pin insertion sort on small part. - * - * Pin insertion sort is extended simple insertion sort. - * The main idea of this sort is to put elements larger - * than an element called pin to the end of array (the - * proper area for such elements). It avoids expensive - * movements of these elements through the whole array. + * Swap larger element with pin. */ - double pin = a[end]; - - for (int i, p = high; ++low < end; ) { - double ai = a[i = low]; - - if (ai < a[i - 1]) { // Small element - - /* - * Insert small element into sorted part. - */ - a[i] = a[--i]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - - } else if (p > i && ai > pin) { // Large element - - /* - * Find element smaller than pin. - */ - while (a[--p] > pin); - - /* - * Swap it with large element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } - /* - * Insert small element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } + /* + * Insert element into sorted part. + */ + while (ai < a[i - 1]) { + a[i] = a[--i]; } + a[i] = ai; + } + + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + double a1 = a[i = low], a2 = a[++low]; /* - * Continue with pair insertion sort on remain part. + * Insert two elements per iteration: at first, insert the + * larger element and then insert the smaller element, but + * from the position where the larger element was inserted. */ - for (int i; low < high; ++low) { - double a1 = a[i = low], a2 = a[++low]; - - /* - * Insert two elements per iteration: at first, insert the - * larger element and then insert the smaller element, but - * from the position where the larger element was inserted. - */ - if (a1 > a2) { + if (a1 > a2) { - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + while (a1 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a1; - while (a2 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a2; + while (a2 < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = a2; - } else if (a1 < a[i - 1]) { + } else if (a1 < a[i - 1]) { - while (a2 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a2; + while (a2 < a[--i]) { + a[i + 2] = a[i]; + } + a[++i + 1] = a2; - while (a1 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a1; + while (a1 < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = a1; } } } @@ -3927,90 +4184,45 @@ private static void mixedInsertionSort(double[] a, int low, int high) { * @param low the index of the first element, inclusive, to be sorted * @param high the index of the last element, exclusive, to be sorted */ - private static void insertionSort(double[] a, int low, int high) { + static void insertionSort(double[] a, int low, int high) { for (int i, k = low; ++k < high; ) { double ai = a[i = k]; if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Sorts the specified range of the array using heap sort. - * - * @param a the array to be sorted - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void heapSort(double[] a, int low, int high) { - for (int k = (low + high) >>> 1; k > low; ) { - pushDown(a, --k, a[k], low, high); - } - while (--high > low) { - double max = a[low]; - pushDown(a, low, a[high], low, high); - a[high] = max; - } - } - - /** - * Pushes specified element down during heap sort. - * - * @param a the given array - * @param p the start index - * @param value the given element - * @param low the index of the first element, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted - */ - private static void pushDown(double[] a, int p, double value, int low, int high) { - for (int k ;; a[p] = a[p = k]) { - k = (p << 1) - low + 2; // Index of the right child + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + a[i ] = ai; } } - a[p] = value; } /** - * Tries to sort the specified range of the array. + * Tries to sort the specified range of the array using merging sort. * * @param sorter parallel context * @param a the array to be sorted - * @param low the index of the first element to be sorted - * @param size the array size - * @return true if finally sorted, false otherwise + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, double[] a, int low, int high) { /* - * The run array is constructed only if initial runs are - * long enough to continue, run[i] then holds start index - * of the i-th sequence of elements in non-descending order. + * The element run[i] holds the start index + * of i-th sequence in non-descending order. */ + int count = 1; int[] run = null; - int high = low + size; - int count = 1, last = low; /* * Identify all possible runs. */ - for (int k = low + 1; k < high; ) { + for (int k = low + 1, last = low; k < high; ) { /* - * Find the end index of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -4035,70 +4247,61 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size } /* - * Check special cases. + * Check if the runs are too long to continue scanning. + */ + if (count > 6 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { - if (k == high) { + if (k == high) { /* - * The array is monotonous sequence, + * Array is monotonous sequence * and therefore already sorted. */ return true; } - if (k - low < MIN_FIRST_RUN_SIZE) { - - /* - * The first run is too small - * to proceed with scanning. - */ - return false; - } - - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } - - if (++count == MAX_RUN_CAPACITY) { + } else if (a[last - 1] > a[last]) { // Start the new run + if (++count == run.length) { /* * Array is not highly structured. */ return false; } + } - if (count == run.length) { + /* + * Save the current run. + */ + run[count] = (last = k); - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; } - run[count] = (last = k); } /* - * Merge runs of highly structured array. + * Merge all runs. */ if (count > 1) { double[] b; int offset = low; - if (sorter == null || (b = (double[]) sorter.b) == null) { - b = new double[size]; - } else { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; + } else if ((b = tryAllocate(double[].class, high - low)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -4119,15 +4322,13 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size * @return the destination where runs are merged */ private static double[] mergeRuns(double[] a, double[] b, int offset, - int aim, boolean parallel, int[] run, int lo, int hi) { + int aim, boolean parallel, int[] run, int lo, int hi) { if (hi - lo == 1) { if (aim >= 0) { return a; } - for (int i = run[hi], j = i - offset, low = run[lo]; i > low; - b[--j] = a[--i] - ); + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); return b; } @@ -4138,19 +4339,10 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, while (run[++mi + 1] <= rmi); /* - * Merge the left and right parts. + * Merge runs of each part. */ - double[] a1, a2; - - if (parallel && hi - lo > MIN_RUN_COUNT) { - RunMerger merger = new RunMerger(a, b, offset, 0, run, mi, hi).forkMe(); - a1 = mergeRuns(a, b, offset, -aim, true, run, lo, mi); - a2 = (double[]) merger.getDestination(); - } else { - a1 = mergeRuns(a, b, offset, -aim, false, run, lo, mi); - a2 = mergeRuns(a, b, offset, 0, false, run, mi, hi); - } - + double[] a1 = mergeRuns(a, b, offset, -aim, parallel, run, lo, mi); + double[] a2 = mergeRuns(a, b, offset, 0, parallel, run, mi, hi); double[] dst = a1 == a ? b : a; int k = a1 == a ? run[lo] - offset : run[lo]; @@ -4159,8 +4351,11 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, int lo2 = a2 == b ? run[mi] - offset : run[mi]; int hi2 = a2 == b ? run[hi] - offset : run[hi]; - if (parallel) { - new Merger(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); + /* + * Merge the left and right parts. + */ + if (hi1 - lo1 > MIN_PARALLEL_SORT_SIZE && parallel) { + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -4180,9 +4375,12 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, * @param lo2 the start index of the second part, inclusive * @param hi2 the end index of the second part, exclusive */ - private static void mergeParts(Merger merger, double[] dst, int k, - double[] a1, int lo1, int hi1, double[] a2, int lo2, int hi2) { + private static void mergeParts(Merger merger, double[] dst, int k, + double[] a1, int lo1, int hi1, double[] a2, int lo2, int hi2) { + /* + * Merge sorted parts in parallel. + */ if (merger != null && a1 == a2) { while (true) { @@ -4195,10 +4393,18 @@ private static void mergeParts(Merger merger, double[] dst, int k, int hi = hi1; hi1 = hi2; hi2 = hi; } + /* + * Terminate, if the second part is empty. + */ + if (lo2 == hi2) { + System.arraycopy(a1, lo1, dst, k, hi1 - lo1); + return; + } + /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -4210,27 +4416,30 @@ private static void mergeParts(Merger merger, double[] dst, int k, int mi2 = hi2; /* - * Partition the smaller part. + * Divide the smaller part. */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; + for (int mi0 = lo2; mi0 < mi2; ) { + int m = (mi0 + mi2) >>> 1; - if (key > a2[t]) { - loo = t + 1; + if (key > a2[m]) { + mi0 = m + 1; } else { - mi2 = t; + mi2 = m; } } - int d = mi2 - lo2 + mi1 - lo1; + /* + * Reserve space for the left parts. + */ + int space = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge other parts in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + space, mi1, hi1, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left parts. */ hi1 = mi1; hi2 = mi2; @@ -4240,8 +4449,10 @@ private static void mergeParts(Merger merger, double[] dst, int k, /* * Merge small parts sequentially. */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + if (lo2 < hi2 && a1[hi1 - 1] > a2[lo2]) { + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } } if (dst != a1 || k < lo1) { while (lo1 < hi1) { @@ -4255,19 +4466,236 @@ private static void mergeParts(Merger merger, double[] dst, int k, } } -// [class] + /** + * Tries to sort the specified range of the array + * using LSD (The Least Significant Digit) Radix sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + * @return {@code true} if the array is finally sorted, otherwise {@code false} + */ + static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { + double[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = sorter.b) != null) { + offset = sorter.offset; + } else if ((b = tryAllocate(double[].class, size)) == null) { + return false; + } + + int start = low - offset; + int last = high - offset; + + /* + * Count the number of all digits. + */ + int[] count1 = new int[1024]; + int[] count2 = new int[2048]; + int[] count3 = new int[2048]; + int[] count4 = new int[2048]; + int[] count5 = new int[2048]; + int[] count6 = new int[1024]; + + for (int i = low; i < high; ++i) { + ++count1[(int) (dtl(a[i]) & 0x3FF)]; + ++count2[(int) ((dtl(a[i]) >>> 10) & 0x7FF)]; + ++count3[(int) ((dtl(a[i]) >>> 21) & 0x7FF)]; + ++count4[(int) ((dtl(a[i]) >>> 32) & 0x7FF)]; + ++count5[(int) ((dtl(a[i]) >>> 43) & 0x7FF)]; + ++count6[(int) ((dtl(a[i]) >>> 54) & 0x3FF)]; + } + + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, size, low); + boolean processDigit2 = processDigit(count2, size, low); + boolean processDigit3 = processDigit(count3, size, low); + boolean processDigit4 = processDigit(count4, size, low); + boolean processDigit5 = processDigit(count5, size, low); + boolean processDigit6 = processDigit(count6, size, low); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = high; i > low; ) { + b[--count1[(int) (dtl(a[--i]) & 0x3FF)] - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = last; i > start; ) { + a[--count2[(int) ((dtl(b[--i]) >>> 10) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count2[(int) ((dtl(a[--i]) >>> 10) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = last; i > start; ) { + a[--count3[(int) ((dtl(b[--i]) >>> 21) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count3[(int) ((dtl(a[--i]) >>> 21) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { + for (int i = last; i > start; ) { + a[--count4[(int) ((dtl(b[--i]) >>> 32) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count4[(int) ((dtl(a[--i]) >>> 32) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 5-th digit. + */ + if (processDigit5) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { + for (int i = last; i > start; ) { + a[--count5[(int) ((dtl(b[--i]) >>> 43) & 0x7FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count5[(int) ((dtl(a[--i]) >>> 43) & 0x7FF)] - offset] = a[i]; + } + } + } + + /* + * Process the 6-th digit. + */ + if (processDigit6) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5) { + for (int i = last; i > start; ) { + a[--count6[(int) ((dtl(b[--i]) >>> 54) & 0x3FF)]] = b[i]; + } + } else { + for (int i = high; i > low; ) { + b[--count6[(int) ((dtl(a[--i]) >>> 54) & 0x3FF)] - offset] = a[i]; + } + } + } + + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5 ^ processDigit6) { + System.arraycopy(b, low - offset, a, low, size); + } + return true; + } + + /** + * Returns masked bits that represent the double value. + * + * @param d the given value + * @return masked bits + */ + private static long dtl(double d) { + long x = Double.doubleToRawLongBits(d); + return x ^ ((x >> 63) | 0x8000000000000000L); + } + + /** + * Sorts the specified range of the array using heap sort. + * + * @param a the array to be sorted + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + static void heapSort(double[] a, int low, int high) { + for (int k = (low + high) >>> 1; k > low; ) { + pushDown(a, --k, a[k], low, high); + } + while (--high > low) { + double max = a[low]; + pushDown(a, low, a[high], low, high); + a[high] = max; + } + } + + /** + * Pushes specified element down during heap sort. + * + * @param a the given array + * @param p the start index + * @param value the given element + * @param low the index of the first element, inclusive, to be sorted + * @param high the index of the last element, exclusive, to be sorted + */ + private static void pushDown(double[] a, int p, double value, int low, int high) { + for (int k ;; a[p] = a[p = k]) { + k = (p << 1) - low + 2; // Index of the right child + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + +// #[class] /** * This class implements parallel sorting. */ - private static final class Sorter extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final class Sorter extends CountedCompleter { + + private static final long serialVersionUID = 123456789L; + @SuppressWarnings("serial") - private final Object a, b; + private final T a, b; private final int low, size, offset, depth; + @SuppressWarnings("unchecked") + private Sorter(T a, int parallelism, int low, int size, int depth) { + this.a = a; + this.low = low; + this.size = size; + this.offset = low; + + while ((parallelism >>= 2) > 0 && (size >>= 2) > 0) { + depth -= 2; + } + this.b = (T) tryAllocate(a.getClass(), this.size); + this.depth = b == null ? 0 : depth; + } + private Sorter(CountedCompleter parent, - Object a, Object b, int low, int size, int offset, int depth) { + T a, T b, int low, int size, int offset, int depth) { super(parent); this.a = a; this.b = b; @@ -4278,66 +4706,67 @@ private Sorter(CountedCompleter parent, } @Override - public final void compute() { + @SuppressWarnings("unchecked") + public void compute() { if (depth < 0) { setPendingCount(2); int half = size >> 1; - new Sorter(this, b, a, low, half, offset, depth + 1).fork(); - new Sorter(this, b, a, low + half, size - half, offset, depth + 1).compute(); + new Sorter<>(this, b, a, low, half, offset, depth + 1).fork(); + new Sorter<>(this, b, a, low + half, size - half, offset, depth + 1).compute(); } else { if (a instanceof int[]) { - sort(this, (int[]) a, depth, low, low + size); + sort((Sorter) this, (int[]) a, depth, low, low + size); } else if (a instanceof long[]) { - sort(this, (long[]) a, depth, low, low + size); + sort((Sorter) this, (long[]) a, depth, low, low + size); } else if (a instanceof float[]) { - sort(this, (float[]) a, depth, low, low + size); + sort((Sorter) this, (float[]) a, depth, low, low + size); } else if (a instanceof double[]) { - sort(this, (double[]) a, depth, low, low + size); + sort((Sorter) this, (double[]) a, depth, low, low + size); } else { - throw new IllegalArgumentException( - "Unknown type of array: " + a.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); } } tryComplete(); } @Override - public final void onCompletion(CountedCompleter caller) { + public void onCompletion(CountedCompleter caller) { if (depth < 0) { int mi = low + (size >> 1); boolean src = (depth & 1) == 0; - new Merger(null, - a, - src ? low : low - offset, - b, - src ? low - offset : low, - src ? mi - offset : mi, - b, - src ? mi - offset : mi, - src ? low + size - offset : low + size + new Merger<>(null, + a, + src ? low : low - offset, + b, + src ? low - offset : low, + src ? mi - offset : mi, + b, + src ? mi - offset : mi, + src ? low + size - offset : low + size ).invoke(); } } - private void forkSorter(int depth, int low, int high) { + private void fork(int depth, int low, int high) { addToPendingCount(1); - Object a = this.a; // Use local variable for performance - new Sorter(this, a, b, low, high - low, offset, depth).fork(); + new Sorter<>(this, a, b, low, high - low, offset, depth).fork(); } } /** * This class implements parallel merging. */ - private static final class Merger extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final class Merger extends CountedCompleter { + + private static final long serialVersionUID = 123456789L; + @SuppressWarnings("serial") - private final Object dst, a1, a2; + private final T dst, a1, a2; private final int k, lo1, hi1, lo2, hi2; - private Merger(CountedCompleter parent, Object dst, int k, - Object a1, int lo1, int hi1, Object a2, int lo2, int hi2) { + private Merger(CountedCompleter parent, T dst, int k, + T a1, int lo1, int hi1, T a2, int lo2, int hi2) { super(parent); this.dst = dst; this.k = k; @@ -4350,80 +4779,51 @@ private Merger(CountedCompleter parent, Object dst, int k, } @Override - public final void compute() { + @SuppressWarnings("unchecked") + public void compute() { if (dst instanceof int[]) { - mergeParts(this, (int[]) dst, k, - (int[]) a1, lo1, hi1, (int[]) a2, lo2, hi2); + mergeParts((Merger) this, (int[]) dst, k, + (int[]) a1, lo1, hi1, (int[]) a2, lo2, hi2); } else if (dst instanceof long[]) { - mergeParts(this, (long[]) dst, k, - (long[]) a1, lo1, hi1, (long[]) a2, lo2, hi2); + mergeParts((Merger) this, (long[]) dst, k, + (long[]) a1, lo1, hi1, (long[]) a2, lo2, hi2); } else if (dst instanceof float[]) { - mergeParts(this, (float[]) dst, k, - (float[]) a1, lo1, hi1, (float[]) a2, lo2, hi2); + mergeParts((Merger) this, (float[]) dst, k, + (float[]) a1, lo1, hi1, (float[]) a2, lo2, hi2); } else if (dst instanceof double[]) { - mergeParts(this, (double[]) dst, k, - (double[]) a1, lo1, hi1, (double[]) a2, lo2, hi2); + mergeParts((Merger) this, (double[]) dst, k, + (double[]) a1, lo1, hi1, (double[]) a2, lo2, hi2); } else { - throw new IllegalArgumentException( - "Unknown type of array: " + dst.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + dst.getClass().getName()); } propagateCompletion(); } - private void forkMerger(Object dst, int k, - Object a1, int lo1, int hi1, Object a2, int lo2, int hi2) { + private void fork(int k, int lo1, int hi1, int lo2, int hi2) { addToPendingCount(1); - new Merger(this, dst, k, a1, lo1, hi1, a2, lo2, hi2).fork(); + new Merger<>(this, dst, k, a1, lo1, hi1, a2, lo2, hi2).fork(); } } /** - * This class implements parallel merging of runs. + * Tries to allocate additional buffer. + * + * @param clazz the given array class + * @param length the length of additional buffer + * @return {@code null} if requested buffer is too big or there is not enough memory, + * otherwise created buffer */ - private static final class RunMerger extends RecursiveTask { - private static final long serialVersionUID = 20180818L; - @SuppressWarnings("serial") - private final Object a, b; - private final int[] run; - private final int offset, aim, lo, hi; - - private RunMerger(Object a, Object b, int offset, - int aim, int[] run, int lo, int hi) { - this.a = a; - this.b = b; - this.offset = offset; - this.aim = aim; - this.run = run; - this.lo = lo; - this.hi = hi; - } - - @Override - protected final Object compute() { - if (a instanceof int[]) { - return mergeRuns((int[]) a, (int[]) b, offset, aim, true, run, lo, hi); - } - if (a instanceof long[]) { - return mergeRuns((long[]) a, (long[]) b, offset, aim, true, run, lo, hi); - } - if (a instanceof float[]) { - return mergeRuns((float[]) a, (float[]) b, offset, aim, true, run, lo, hi); - } - if (a instanceof double[]) { - return mergeRuns((double[]) a, (double[]) b, offset, aim, true, run, lo, hi); - } - throw new IllegalArgumentException( - "Unknown type of array: " + a.getClass().getName()); - } - - private RunMerger forkMe() { - fork(); - return this; - } - - private Object getDestination() { - join(); - return getRawResult(); + @SuppressWarnings("unchecked") + private static T tryAllocate(Class clazz, int length) { + try { + int maxLength = MAX_BUFFER_SIZE >> + (clazz == int[].class || clazz == float[].class ? 2 : 3); + return length > maxLength ? null : + (T) U.allocateUninitializedArray(clazz.componentType(), length); + } catch (OutOfMemoryError e) { + return null; } } + + private static final Unsafe U = Unsafe.getUnsafe(); } diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index f285b0c65b72c..ba8e9069fd484 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -24,7 +24,7 @@ /* * @test * @compile/module=java.base java/util/SortingHelper.java - * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297 + * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297 8266431 * @build Sorting * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:DisableIntrinsic=_arraySort,_arrayPartition Sorting -shortrun * @run main/othervm -XX:-TieredCompilation -XX:CompileCommand=CompileThresholdScaling,java.util.DualPivotQuicksort::sort,0.0001 Sorting -shortrun @@ -36,7 +36,7 @@ */ import java.io.PrintStream; -import java.util.Comparator; +import java.util.Arrays; import java.util.Random; import java.util.SortingHelper; @@ -45,29 +45,32 @@ public class Sorting { private static final PrintStream out = System.out; private static final PrintStream err = System.err; - // Array lengths used in a long run (default) - private static final int[] LONG_RUN_LENGTHS = { - 1, 3, 8, 21, 55, 100, 1_000, 10_000, 100_000 }; + // Lengths of arrays for short run + private static final int[] SHORT_RUN_LENGTHS = + { 1, 2, 14, 100, 500, 1_000, 10_000 }; - // Array lengths used in a short run - private static final int[] SHORT_RUN_LENGTHS = { - 1, 8, 55, 100, 10_000 }; + // Lengths of arrays for long run (default) + private static final int[] LONG_RUN_LENGTHS = + { 1, 2, 14, 100, 500, 1_000, 10_000, 50_000 }; - // Random initial values used in a long run (default) - private static final TestRandom[] LONG_RUN_RANDOMS = { - TestRandom.BABA, TestRandom.DEDA, TestRandom.C0FFEE }; + // Initial random values for short run + private static final TestRandom[] SHORT_RUN_RANDOMS = + { TestRandom.C0FFEE }; - // Random initial values used in a short run - private static final TestRandom[] SHORT_RUN_RANDOMS = { - TestRandom.C0FFEE }; + // Initial random values for long run (default) + private static final TestRandom[] LONG_RUN_RANDOMS = + { TestRandom.DEDA, TestRandom.BABA, TestRandom.C0FFEE }; - // Constants used in subarray sorting + // Constant to fill the left part of array private static final int A380 = 0xA380; + + // Constant to fill the right part of array private static final int B747 = 0xB747; private final SortingHelper sortingHelper; private final TestRandom[] randoms; private final int[] lengths; + private final boolean fix; private Object[] gold; private Object[] test; @@ -78,312 +81,140 @@ public static void main(String[] args) { int[] lengths = shortRun ? SHORT_RUN_LENGTHS : LONG_RUN_LENGTHS; TestRandom[] randoms = shortRun ? SHORT_RUN_RANDOMS : LONG_RUN_RANDOMS; + new Sorting(SortingHelper.MIXED_INSERTION_SORT, randoms).testBase(); + new Sorting(SortingHelper.MERGING_SORT, randoms, lengths).testStructured(512); + new Sorting(SortingHelper.HEAP_SORT, randoms, lengths).testBase(); + new Sorting(SortingHelper.RADIX_SORT, randoms, lengths).testCore(); new Sorting(SortingHelper.DUAL_PIVOT_QUICKSORT, randoms, lengths).testCore(); new Sorting(SortingHelper.PARALLEL_SORT, randoms, lengths).testCore(); - new Sorting(SortingHelper.HEAP_SORT, randoms, lengths).testBasic(); new Sorting(SortingHelper.ARRAYS_SORT, randoms, lengths).testAll(); new Sorting(SortingHelper.ARRAYS_PARALLEL_SORT, randoms, lengths).testAll(); long end = System.currentTimeMillis(); - out.format("PASSED in %d sec.\n", (end - start) / 1000); + out.format("PASSED in %d sec.\n", (end - start) / 1_000); + } + + private Sorting(SortingHelper sortingHelper, TestRandom[] randoms) { + this(sortingHelper, randoms, SHORT_RUN_LENGTHS, true); } private Sorting(SortingHelper sortingHelper, TestRandom[] randoms, int[] lengths) { + this(sortingHelper, randoms, lengths, false); + } + + private Sorting(SortingHelper sortingHelper, TestRandom[] randoms, int[] lengths, boolean fix) { this.sortingHelper = sortingHelper; this.randoms = randoms; this.lengths = lengths; + this.fix = fix; } - private void testBasic() { + private void testBase() { + testStructured(0); testEmptyArray(); for (int length : lengths) { createData(length); - testBasic(length); - } - } + testSubArray(length); - private void testBasic(int length) { - for (TestRandom random : randoms) { - testWithInsertionSort(length, random); - testWithCheckSum(length, random); - testWithScrambling(length, random); + for (TestRandom random : randoms) { + testWithCheckSum(length, random); + testWithScrambling(length, random); + testWithInsertionSort(length, random); + } } } private void testCore() { + testBase(); + for (int length : lengths) { createData(length); - testCore(length); - } - } - - private void testCore(int length) { - testBasic(length); - for (TestRandom random : randoms) { - testMergingSort(length, random); - testSubArray(length, random); - testNegativeZero(length, random); - testFloatingPointSorting(length, random); + for (TestRandom random : randoms) { + testNegativeZero(length, random); + testFloatingPointSorting(length, random); + } } } private void testAll() { + testCore(); + for (int length : lengths) { createData(length); - testAll(length); + testRange(length); } } - private void testAll(int length) { - testCore(length); - - for (TestRandom random : randoms) { - testRange(length, random); - testStability(length, random); + private void testStructured(int min) { + for (int length : lengths) { + createData(length); + testStructured(length, min); } } private void testEmptyArray() { - testEmptyAndNullIntArray(); - testEmptyAndNullLongArray(); - testEmptyAndNullByteArray(); - testEmptyAndNullCharArray(); - testEmptyAndNullShortArray(); - testEmptyAndNullFloatArray(); - testEmptyAndNullDoubleArray(); - } - - private void testStability(int length, TestRandom random) { - printTestName("Test stability", random, length); - - Pair[] a = build(length, random); - sortingHelper.sort(a); - checkSorted(a); - checkStable(a); - - a = build(length, random); - sortingHelper.sort(a, pairComparator); - checkSorted(a); - checkStable(a); - - out.println(); - } - - private void testEmptyAndNullIntArray() { sortingHelper.sort(new int[] {}); sortingHelper.sort(new int[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(int[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(int[]) shouldn't catch null array"); - } - - private void testEmptyAndNullLongArray() { sortingHelper.sort(new long[] {}); sortingHelper.sort(new long[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(long[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(long[]) shouldn't catch null array"); - } - - private void testEmptyAndNullByteArray() { sortingHelper.sort(new byte[] {}); sortingHelper.sort(new byte[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(byte[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(byte[]) shouldn't catch null array"); - } - - private void testEmptyAndNullCharArray() { sortingHelper.sort(new char[] {}); sortingHelper.sort(new char[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(char[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(char[]) shouldn't catch null array"); - } - - private void testEmptyAndNullShortArray() { sortingHelper.sort(new short[] {}); sortingHelper.sort(new short[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(short[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(short[]) shouldn't catch null array"); - } - - private void testEmptyAndNullFloatArray() { sortingHelper.sort(new float[] {}); sortingHelper.sort(new float[] {}, 0, 0); - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(float[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(float[]) shouldn't catch null array"); - } - - private void testEmptyAndNullDoubleArray() { sortingHelper.sort(new double[] {}); sortingHelper.sort(new double[] {}, 0, 0); - - try { - sortingHelper.sort(null); - } catch (NullPointerException expected) { - try { - sortingHelper.sort(null, 0, 0); - } catch (NullPointerException expected2) { - return; - } - fail(sortingHelper + "(double[],fromIndex,toIndex) shouldn't " + - "catch null array"); - } - fail(sortingHelper + "(double[]) shouldn't catch null array"); } - private void testSubArray(int length, TestRandom random) { - if (length < 4) { + private void testSubArray(int length) { + if (fix || length < 4) { return; } for (int m = 1; m < length / 2; m <<= 1) { - int fromIndex = m; int toIndex = length - m; - prepareSubArray((int[]) gold[0], fromIndex, toIndex); + prepareSubArray((int[]) gold[0], m, toIndex); convertData(length); - for (int i = 0; i < test.length; i++) { - printTestName("Test subarray", random, length, - ", m = " + m + ", " + getType(i)); - sortingHelper.sort(test[i], fromIndex, toIndex); - checkSubArray(test[i], fromIndex, toIndex); + for (int i = 0; i < test.length; ++i) { + printTestName("Test subarray", length, + ", m = " + m + ", " + getType(i)); + sortingHelper.sort(test[i], m, toIndex); + checkSubArray(test[i], m, toIndex); } } out.println(); } - private void testRange(int length, TestRandom random) { - if (length < 2) { - return; - } + private void testRange(int length) { for (int m = 1; m < length; m <<= 1) { - for (int i = 1; i <= length; i++) { - ((int[]) gold[0]) [i - 1] = i % m + m % i; + for (int i = 1; i <= length; ++i) { + ((int[]) gold[0])[i - 1] = i % m + m % i; } convertData(length); - for (int i = 0; i < test.length; i++) { - printTestName("Test range check", random, length, - ", m = " + m + ", " + getType(i)); + for (int i = 0; i < test.length; ++i) { + printTestName("Test range check", length, + ", m = " + m + ", " + getType(i)); checkRange(test[i], m); } } out.println(); } - private void checkSorted(Pair[] a) { - for (int i = 0; i < a.length - 1; i++) { - if (a[i].getKey() > a[i + 1].getKey()) { - fail("Array is not sorted at " + i + "-th position: " + - a[i].getKey() + " and " + a[i + 1].getKey()); - } - } - } - - private void checkStable(Pair[] a) { - for (int i = 0; i < a.length / 4; ) { - int key1 = a[i].getKey(); - int value1 = a[i++].getValue(); - int key2 = a[i].getKey(); - int value2 = a[i++].getValue(); - int key3 = a[i].getKey(); - int value3 = a[i++].getValue(); - int key4 = a[i].getKey(); - int value4 = a[i++].getValue(); - - if (!(key1 == key2 && key2 == key3 && key3 == key4)) { - fail("Keys are different " + key1 + ", " + key2 + ", " + - key3 + ", " + key4 + " at position " + i); - } - if (!(value1 < value2 && value2 < value3 && value3 < value4)) { - fail("Sorting is not stable at position " + i + - ". Second values have been changed: " + value1 + ", " + - value2 + ", " + value3 + ", " + value4); - } - } - } - - private Pair[] build(int length, Random random) { - Pair[] a = new Pair[length * 4]; - - for (int i = 0; i < a.length; ) { - int key = random.nextInt(); - a[i++] = new Pair(key, 1); - a[i++] = new Pair(key, 2); - a[i++] = new Pair(key, 3); - a[i++] = new Pair(key, 4); - } - return a; - } - private void testWithInsertionSort(int length, TestRandom random) { - if (length > 1000) { + if (length > 1_000) { return; } for (int m = 1; m <= length; m <<= 1) { @@ -391,11 +222,12 @@ private void testWithInsertionSort(int length, TestRandom random) { builder.build((int[]) gold[0], m, random); convertData(length); - for (int i = 0; i < test.length; i++) { + for (int i = 0; i < test.length; ++i) { printTestName("Test with insertion sort", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); + ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); sortByInsertionSort(gold[i]); + checkSorted(gold[i]); compare(test[i], gold[i]); } } @@ -403,20 +235,18 @@ private void testWithInsertionSort(int length, TestRandom random) { out.println(); } - private void testMergingSort(int length, TestRandom random) { - if (length < (4 << 10)) { // DualPivotQuicksort.MIN_TRY_MERGE_SIZE + private void testStructured(int length, int min) { + if (length < min) { return; } - final int PERIOD = 50; - - for (int m = PERIOD - 2; m <= PERIOD + 2; m++) { - for (MergingBuilder builder : MergingBuilder.values()) { + for (int m = 1; m < 8; ++m) { + for (StructuredBuilder builder : StructuredBuilder.values()) { builder.build((int[]) gold[0], m); convertData(length); - for (int i = 0; i < test.length; i++) { - printTestName("Test merging sort", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); + for (int i = 0; i < test.length; ++i) { + printTestName("Test structured", length, + ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); checkSorted(test[i]); } @@ -426,14 +256,17 @@ private void testMergingSort(int length, TestRandom random) { } private void testWithCheckSum(int length, TestRandom random) { + if (length > 1_000) { + return; + } for (int m = 1; m <= length; m <<= 1) { for (UnsortedBuilder builder : UnsortedBuilder.values()) { builder.build((int[]) gold[0], m, random); convertData(length); - for (int i = 0; i < test.length; i++) { + for (int i = 0; i < test.length; ++i) { printTestName("Test with check sum", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); + ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); checkWithCheckSum(test[i], gold[i]); } @@ -443,14 +276,17 @@ private void testWithCheckSum(int length, TestRandom random) { } private void testWithScrambling(int length, TestRandom random) { + if (fix) { + return; + } for (int m = 1; m <= length; m <<= 1) { for (SortedBuilder builder : SortedBuilder.values()) { builder.build((int[]) gold[0], m); convertData(length); - for (int i = 0; i < test.length; i++) { + for (int i = 0; i < test.length; ++i) { printTestName("Test with scrambling", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); + ", m = " + m + ", " + getType(i) + " " + builder); scramble(test[i], random); sortingHelper.sort(test[i]); compare(test[i], gold[i]); @@ -461,10 +297,10 @@ private void testWithScrambling(int length, TestRandom random) { } private void testNegativeZero(int length, TestRandom random) { - for (int i = 5; i < test.length; i++) { + for (int i = 5; i < test.length; ++i) { printTestName("Test negative zero -0.0", random, length, " " + getType(i)); - NegativeZeroBuilder builder = NegativeZeroBuilder.values() [i - 5]; + NegativeZeroBuilder builder = NegativeZeroBuilder.values()[i - 5]; builder.build(test[i], random); sortingHelper.sort(test[i]); @@ -474,72 +310,71 @@ private void testNegativeZero(int length, TestRandom random) { } private void testFloatingPointSorting(int length, TestRandom random) { - if (length < 2) { + if (length < 6) { return; } - final int MAX = 13; + final int MAX = 14; + int s = 4; - for (int a = 0; a < MAX; a++) { - for (int g = 0; g < MAX; g++) { - for (int z = 0; z < MAX; z++) { - for (int n = 0; n < MAX; n++) { - for (int p = 0; p < MAX; p++) { - if (a + g + z + n + p != length) { + for (int a = 0; a < MAX; ++a) { + for (int g = 0; g < MAX; ++g) { + for (int z = 0; z < MAX; ++z) { + for (int n = 0; n < MAX; ++n) { + for (int p = 0; p < MAX; ++p) { + if (a + g + z + n + p + s != length) { continue; } - for (int i = 5; i < test.length; i++) { + for (int i = 5; i < test.length; ++i) { printTestName("Test float-pointing sorting", random, length, - ", a = " + a + ", g = " + g + ", z = " + z + - ", n = " + n + ", p = " + p + ", " + getType(i)); + ", a = " + a + ", g = " + g + ", z = " + z + + ", n = " + n + ", p = " + p + ", " + getType(i)); FloatingPointBuilder builder = FloatingPointBuilder.values()[i - 5]; builder.build(gold[i], a, g, z, n, p, random); copy(test[i], gold[i]); scramble(test[i], random); sortingHelper.sort(test[i]); - compare(test[i], gold[i], a, n, g); + compare(test[i], gold[i], a, n + 2, g); } } } } } } + for (int m = MAX; m > 4; --m) { + int g = length / m; + int a = length - g - g - g - g - s; - for (int m = 13; m > 4; m--) { - int t = length / m; - int g = t, z = t, n = t, p = t; - int a = length - g - z - n - p; - - for (int i = 5; i < test.length; i++) { + for (int i = 5; i < test.length; ++i) { printTestName("Test float-pointing sorting", random, length, - ", a = " + a + ", g = " + g + ", z = " + z + - ", n = " + n + ", p = " + p + ", " + getType(i)); - FloatingPointBuilder builder = FloatingPointBuilder.values() [i - 5]; - builder.build(gold[i], a, g, z, n, p, random); + ", a = " + a + ", g = " + g + ", z = " + g + + ", n = " + g + ", p = " + g + ", " + getType(i)); + FloatingPointBuilder builder = FloatingPointBuilder.values()[i - 5]; + builder.build(gold[i], a, g, g, g, g, random); copy(test[i], gold[i]); scramble(test[i], random); sortingHelper.sort(test[i]); - compare(test[i], gold[i], a, n, g); + compare(test[i], gold[i], a, g + 2, g); } } out.println(); } private void prepareSubArray(int[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { a[i] = A380; } int middle = (fromIndex + toIndex) >>> 1; int k = 0; - for (int i = fromIndex; i < middle; i++) { + for (int i = fromIndex; i < middle; ++i) { a[i] = k++; } - for (int i = middle; i < toIndex; i++) { + for (int i = middle; i < toIndex; ++i) { a[i] = k--; } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { a[i] = B747; } } @@ -560,48 +395,48 @@ private void scramble(Object a, Random random) { } else if (a instanceof double[]) { scramble((double[]) a, random); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void scramble(int[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(long[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(byte[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(char[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(short[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(float[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } private void scramble(double[] a, Random random) { - for (int i = 0; i < a.length * 7; i++) { + for (int i = 0; i < a.length * 7; ++i) { swap(a, random.nextInt(a.length), random.nextInt(a.length)); } } @@ -639,6 +474,10 @@ private void checkWithCheckSum(Object test, Object gold) { checkCheckSum(test, gold); } + private void fail(Object object) { + fail("Unknown type of array: " + object.getClass().getName()); + } + private void fail(String message) { err.format("\n*** TEST FAILED ***\n\n%s\n\n", message); throw new RuntimeException("Test failed"); @@ -650,12 +489,12 @@ private void checkNegativeZero(Object a) { } else if (a instanceof double[]) { checkNegativeZero((double[]) a); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void checkNegativeZero(float[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (Float.floatToRawIntBits(a[i]) == 0 && Float.floatToRawIntBits(a[i + 1]) < 0) { fail(a[i] + " before " + a[i + 1] + " at position " + i); } @@ -663,7 +502,7 @@ private void checkNegativeZero(float[] a) { } private void checkNegativeZero(double[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (Double.doubleToRawLongBits(a[i]) == 0 && Double.doubleToRawLongBits(a[i + 1]) < 0) { fail(a[i] + " before " + a[i + 1] + " at position " + i); } @@ -676,25 +515,25 @@ private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) } else if (a instanceof double[]) { compare((double[]) a, (double[]) b, numNaN, numNeg, numNegZero); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZero) { - for (int i = a.length - numNaN; i < a.length; i++) { + for (int i = a.length - numNaN; i < a.length; ++i) { if (a[i] == a[i]) { fail("There must be NaN instead of " + a[i] + " at position " + i); } } final int NEGATIVE_ZERO = Float.floatToIntBits(-0.0f); - for (int i = numNeg; i < numNeg + numNegZero; i++) { + for (int i = numNeg; i < numNeg + numNegZero; ++i) { if (NEGATIVE_ZERO != Float.floatToIntBits(a[i])) { fail("There must be -0.0 instead of " + a[i] + " at position " + i); } } - for (int i = 0; i < a.length - numNaN; i++) { + for (int i = 0; i < a.length - numNaN; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -702,20 +541,20 @@ private void compare(float[] a, float[] b, int numNaN, int numNeg, int numNegZer } private void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) { - for (int i = a.length - numNaN; i < a.length; i++) { + for (int i = a.length - numNaN; i < a.length; ++i) { if (a[i] == a[i]) { fail("There must be NaN instead of " + a[i] + " at position " + i); } } final long NEGATIVE_ZERO = Double.doubleToLongBits(-0.0d); - for (int i = numNeg; i < numNeg + numNegZero; i++) { + for (int i = numNeg; i < numNeg + numNegZero; ++i) { if (NEGATIVE_ZERO != Double.doubleToLongBits(a[i])) { fail("There must be -0.0 instead of " + a[i] + " at position " + i); } } - for (int i = 0; i < a.length - numNaN; i++) { + for (int i = 0; i < a.length - numNaN; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -738,12 +577,12 @@ private void compare(Object a, Object b) { } else if (a instanceof double[]) { compare((double[]) a, (double[]) b); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void compare(int[] a, int[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -751,7 +590,7 @@ private void compare(int[] a, int[] b) { } private void compare(long[] a, long[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -759,7 +598,7 @@ private void compare(long[] a, long[] b) { } private void compare(byte[] a, byte[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -767,7 +606,7 @@ private void compare(byte[] a, byte[] b) { } private void compare(char[] a, char[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -775,7 +614,7 @@ private void compare(char[] a, char[] b) { } private void compare(short[] a, short[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -783,7 +622,7 @@ private void compare(short[] a, short[] b) { } private void compare(float[] a, float[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -791,7 +630,7 @@ private void compare(float[] a, float[] b) { } private void compare(double[] a, double[] b) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { if (a[i] != b[i]) { fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); } @@ -822,7 +661,7 @@ private String getType(int i) { if (a instanceof double[]) { return "DOUBLE"; } - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); return null; } @@ -842,12 +681,12 @@ private void checkSorted(Object a) { } else if (a instanceof double[]) { checkSorted((double[]) a); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void checkSorted(int[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -855,7 +694,7 @@ private void checkSorted(int[] a) { } private void checkSorted(long[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -863,7 +702,7 @@ private void checkSorted(long[] a) { } private void checkSorted(byte[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -871,7 +710,7 @@ private void checkSorted(byte[] a) { } private void checkSorted(char[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -879,7 +718,7 @@ private void checkSorted(char[] a) { } private void checkSorted(short[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -887,7 +726,7 @@ private void checkSorted(short[] a) { } private void checkSorted(float[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -895,7 +734,7 @@ private void checkSorted(float[] a) { } private void checkSorted(double[] a) { - for (int i = 0; i < a.length - 1; i++) { + for (int i = 0; i < a.length - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } @@ -933,7 +772,7 @@ private int checkSumXor(Object a) { if (a instanceof double[]) { return checkSumXor((double[]) a); } - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); return -1; } @@ -961,7 +800,7 @@ private int checkSumXor(byte[] a) { for (byte e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(char[] a) { @@ -970,7 +809,7 @@ private int checkSumXor(char[] a) { for (char e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(short[] a) { @@ -979,7 +818,7 @@ private int checkSumXor(short[] a) { for (short e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(float[] a) { @@ -1022,7 +861,7 @@ private int checkSumPlus(Object a) { if (a instanceof double[]) { return checkSumPlus((double[]) a); } - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); return -1; } @@ -1050,7 +889,7 @@ private int checkSumPlus(byte[] a) { for (byte e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(char[] a) { @@ -1059,7 +898,7 @@ private int checkSumPlus(char[] a) { for (char e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(short[] a) { @@ -1068,7 +907,7 @@ private int checkSumPlus(short[] a) { for (short e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(float[] a) { @@ -1090,100 +929,7 @@ private int checkSumPlus(double[] a) { } private void sortByInsertionSort(Object a) { - if (a instanceof int[]) { - sortByInsertionSort((int[]) a); - } else if (a instanceof long[]) { - sortByInsertionSort((long[]) a); - } else if (a instanceof byte[]) { - sortByInsertionSort((byte[]) a); - } else if (a instanceof char[]) { - sortByInsertionSort((char[]) a); - } else if (a instanceof short[]) { - sortByInsertionSort((short[]) a); - } else if (a instanceof float[]) { - sortByInsertionSort((float[]) a); - } else if (a instanceof double[]) { - sortByInsertionSort((double[]) a); - } else { - fail("Unknown type of array: " + a.getClass().getName()); - } - } - - private void sortByInsertionSort(int[] a) { - for (int j, i = 1; i < a.length; i++) { - int ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(long[] a) { - for (int j, i = 1; i < a.length; i++) { - long ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(byte[] a) { - for (int j, i = 1; i < a.length; i++) { - byte ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(char[] a) { - for (int j, i = 1; i < a.length; i++) { - char ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(short[] a) { - for (int j, i = 1; i < a.length; i++) { - short ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(float[] a) { - for (int j, i = 1; i < a.length; i++) { - float ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } - } - - private void sortByInsertionSort(double[] a) { - for (int j, i = 1; i < a.length; i++) { - double ai = a[i]; - - for (j = i - 1; j >= 0 && ai < a[j]; j--) { - a[j + 1] = a[j]; - } - a[j + 1] = ai; - } + SortingHelper.INSERTION_SORT.sort(a); } private void checkSubArray(Object a, int fromIndex, int toIndex) { @@ -1202,24 +948,24 @@ private void checkSubArray(Object a, int fromIndex, int toIndex) { } else if (a instanceof double[]) { checkSubArray((double[]) a, fromIndex, toIndex); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } private void checkSubArray(int[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != A380) { fail("Range sort changes left element at position " + i + hex(a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != B747) { fail("Range sort changes right element at position " + i + hex(a[i], B747)); } @@ -1227,19 +973,19 @@ private void checkSubArray(int[] a, int fromIndex, int toIndex) { } private void checkSubArray(long[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (long) A380) { fail("Range sort changes left element at position " + i + hex(a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (long) B747) { fail("Range sort changes right element at position " + i + hex(a[i], B747)); } @@ -1247,19 +993,19 @@ private void checkSubArray(long[] a, int fromIndex, int toIndex) { } private void checkSubArray(byte[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (byte) A380) { fail("Range sort changes left element at position " + i + hex(a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (byte) B747) { fail("Range sort changes right element at position " + i + hex(a[i], B747)); } @@ -1267,19 +1013,19 @@ private void checkSubArray(byte[] a, int fromIndex, int toIndex) { } private void checkSubArray(char[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (char) A380) { fail("Range sort changes left element at position " + i + hex(a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (char) B747) { fail("Range sort changes right element at position " + i + hex(a[i], B747)); } @@ -1287,19 +1033,19 @@ private void checkSubArray(char[] a, int fromIndex, int toIndex) { } private void checkSubArray(short[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (short) A380) { fail("Range sort changes left element at position " + i + hex(a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (short) B747) { fail("Range sort changes right element at position " + i + hex(a[i], B747)); } @@ -1307,19 +1053,19 @@ private void checkSubArray(short[] a, int fromIndex, int toIndex) { } private void checkSubArray(float[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (float) A380) { fail("Range sort changes left element at position " + i + hex((long) a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (float) B747) { fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); } @@ -1327,19 +1073,19 @@ private void checkSubArray(float[] a, int fromIndex, int toIndex) { } private void checkSubArray(double[] a, int fromIndex, int toIndex) { - for (int i = 0; i < fromIndex; i++) { + for (int i = 0; i < fromIndex; ++i) { if (a[i] != (double) A380) { fail("Range sort changes left element at position " + i + hex((long) a[i], A380)); } } - for (int i = fromIndex; i < toIndex - 1; i++) { + for (int i = fromIndex; i < toIndex - 1; ++i) { if (a[i] > a[i + 1]) { fail("Array is not sorted at " + i + "-th position: " + a[i] + " and " + a[i + 1]); } } - for (int i = toIndex; i < a.length; i++) { + for (int i = toIndex; i < a.length; ++i) { if (a[i] != (double) B747) { fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); } @@ -1362,7 +1108,7 @@ private void checkRange(Object a, int m) { } else if (a instanceof double[]) { checkRange((double[]) a, m); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } @@ -1370,17 +1116,17 @@ private void checkRange(int[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1390,17 +1136,17 @@ private void checkRange(long[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1410,17 +1156,17 @@ private void checkRange(byte[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1430,17 +1176,17 @@ private void checkRange(char[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1450,17 +1196,17 @@ private void checkRange(short[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1470,17 +1216,17 @@ private void checkRange(float[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1490,17 +1236,17 @@ private void checkRange(double[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + - "as expected: fromIndex = " + (m + 1) + " toIndex = " + m); + "as expected: fromIndex = " + (m + 1) + ", toIndex = " + m); } catch (IllegalArgumentException iae) { try { sortingHelper.sort(a, -m, a.length); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: fromIndex = " + (-m)); + "as expected: fromIndex = " + (-m)); } catch (ArrayIndexOutOfBoundsException aoe) { try { sortingHelper.sort(a, 0, a.length + m); fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "as expected: toIndex = " + (a.length + m)); + "as expected: toIndex = " + (a.length + m)); } catch (ArrayIndexOutOfBoundsException expected) {} } } @@ -1512,7 +1258,7 @@ private void copy(Object dst, Object src) { } else if (src instanceof double[]) { copy((double[]) dst, (double[]) src); } else { - fail("Unknown type of array: " + src.getClass().getName()); + fail(src); } } @@ -1524,31 +1270,27 @@ private void copy(double[] dst, double[] src) { System.arraycopy(src, 0, dst, 0, src.length); } - private void printTestName(String test, TestRandom random, int length) { - printTestName(test, random, length, ""); - } - private void createData(int length) { gold = new Object[] { - new int[length], new long[length], - new byte[length], new char[length], new short[length], - new float[length], new double[length] + new int[length], new long[length], + new byte[length], new char[length], new short[length], + new float[length], new double[length] }; test = new Object[] { - new int[length], new long[length], - new byte[length], new char[length], new short[length], - new float[length], new double[length] + new int[length], new long[length], + new byte[length], new char[length], new short[length], + new float[length], new double[length] }; } private void convertData(int length) { - for (int i = 1; i < gold.length; i++) { - TypeConverter converter = TypeConverter.values()[i - 1]; - converter.convert((int[])gold[0], gold[i]); + for (int i = 0; i < gold.length; ++i) { + TypeConverter converter = TypeConverter.values()[i]; + converter.convert((int[]) gold[0], gold[i], fix); } - for (int i = 0; i < gold.length; i++) { + for (int i = 0; i < gold.length; ++i) { System.arraycopy(gold[i], 0, test[i], 0, length); } } @@ -1557,83 +1299,123 @@ private String hex(long a, int b) { return ": " + Long.toHexString(a) + ", must be " + Integer.toHexString(b); } + private void printTestName(String test, int length, String message) { + out.println("[" + sortingHelper + "] '" + test + "' length = " + length + message); + } + private void printTestName(String test, TestRandom random, int length, String message) { - out.println( "[" + sortingHelper + "] '" + test + - "' length = " + length + ", random = " + random + message); + out.println("[" + sortingHelper + "] '" + test + + "' length = " + length + ", random = " + random + message); } - private static enum TypeConverter { + private enum TypeConverter { + + INT { + @Override + void convert(int[] src, Object dst, boolean fix) { + if (fix) { + src[0] = Integer.MIN_VALUE; + } + } + }, + LONG { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { long[] b = (long[]) dst; - for (int i = 0; i < src.length; i++) { - b[i] = (long) src[i]; + for (int i = 0; i < src.length; ++i) { + b[i] = src[i]; + } + if (fix) { + b[0] = Long.MIN_VALUE; } } }, BYTE { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { byte[] b = (byte[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (byte) src[i]; } + if (fix) { + b[0] = Byte.MIN_VALUE; + } } }, CHAR { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { char[] b = (char[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (char) src[i]; } + if (fix) { + b[0] = Character.MIN_VALUE; + } } }, SHORT { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { short[] b = (short[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (short) src[i]; } + if (fix) { + b[0] = Short.MIN_VALUE; + } } }, FLOAT { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { float[] b = (float[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (float) src[i]; } + if (fix) { + b[0] = Float.NEGATIVE_INFINITY; + } } }, DOUBLE { - void convert(int[] src, Object dst) { + @Override + void convert(int[] src, Object dst, boolean fix) { double[] b = (double[]) dst; - for (int i = 0; i < src.length; i++) { - b[i] = (double) src[i]; + for (int i = 0; i < src.length; ++i) { + b[i] = src[i]; + } + if (fix) { + b[0] = Double.NEGATIVE_INFINITY; } } }; - abstract void convert(int[] src, Object dst); + abstract void convert(int[] src, Object dst, boolean fix); } - private static enum SortedBuilder { + private enum SortedBuilder { + STEPS { + @Override void build(int[] a, int m) { - for (int i = 0; i < m; i++) { + for (int i = 0; i < m; ++i) { a[i] = 0; } - for (int i = m; i < a.length; i++) { + for (int i = m; i < a.length; ++i) { a[i] = 1; } } @@ -1642,40 +1424,63 @@ void build(int[] a, int m) { abstract void build(int[] a, int m); } - private static enum UnsortedBuilder { + private enum UnsortedBuilder { + RANDOM { + @Override void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = random.nextInt(); } } }, - ASCENDING { + PERMUTATION { + @Override void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { - a[i] = m + i; + int mask = ~(0x000000FF << (random.nextInt(4) * 2)); + + for (int i = 0; i < a.length; ++i) { + a[i] = i & mask; + } + for (int i = a.length; i > 1; --i) { + int k = random.nextInt(i); + int t = a[i - 1]; a[i - 1] = a[k]; a[k] = t; } } }, - DESCENDING { + UNIFORM { + @Override void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { - a[i] = a.length - m - i; + int mask = (m << 15) - 1; + + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt() & mask; } } }, - EQUAL { + REPEATED { + @Override void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { - a[i] = m; + for (int i = 0; i < a.length; ++i) { + a[i] = i % m; + } + } + }, + + DUPLICATED { + @Override + void build(int[] a, int m, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt(m); } } }, - SAW { + SAWTOOTH { + @Override void build(int[] a, int m, Random random) { int incCount = 1; int decCount = a.length; @@ -1683,7 +1488,7 @@ void build(int[] a, int m, Random random) { int period = m--; while (true) { - for (int k = 1; k <= period; k++) { + for (int k = 1; k <= period; ++k) { if (i >= a.length) { return; } @@ -1691,7 +1496,7 @@ void build(int[] a, int m, Random random) { } period += m; - for (int k = 1; k <= period; k++) { + for (int k = 1; k <= period; ++k) { if (i >= a.length) { return; } @@ -1702,161 +1507,145 @@ void build(int[] a, int m, Random random) { } }, - REPEATED { + SHUFFLE { + @Override void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { - a[i] = i % m; + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = random.nextInt(m) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(int[] a, int m, Random random); + } + + private enum StructuredBuilder { + + ASCENDING { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { + a[i] = m + i; } } }, - DUPLICATED { - void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { - a[i] = random.nextInt(m); + DESCENDING { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { + a[i] = a.length - m - i; + } + } + }, + + EQUAL { + @Override + void build(int[] a, int m) { + Arrays.fill(a, m); + } + }, + + MASKED { + @Override + void build(int[] a, int m) { + int mask = (m << 15) - 1; + + for (int i = 0; i < a.length; ++i) { + a[i] = (i ^ 0xFF) & mask; } } }, ORGAN_PIPES { - void build(int[] a, int m, Random random) { + @Override + void build(int[] a, int m) { int middle = a.length / (m + 1); - for (int i = 0; i < middle; i++) { + for (int i = 0; i < middle; ++i) { a[i] = i; } - for (int i = middle; i < a.length; i++) { + for (int i = middle; i < a.length; ++i) { a[i] = a.length - i - 1; } } }, STAGGER { - void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { a[i] = (i * m + i) % a.length; } } }, PLATEAU { - void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; i++) { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { a[i] = Math.min(i, m); } } }, - SHUFFLE { - void build(int[] a, int m, Random random) { - int x = 0, y = 0; - - for (int i = 0; i < a.length; i++) { - a[i] = random.nextBoolean() ? (x += 2) : (y += 2); - } - } - }, - LATCH { - void build(int[] a, int m, Random random) { + @Override + void build(int[] a, int m) { int max = a.length / m; - max = max < 2 ? 2 : max; + max = Math.max(max, 2); - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = i % max; } } - }; - - abstract void build(int[] a, int m, Random random); - } - - private static enum MergingBuilder { - ASCENDING { - void build(int[] a, int m) { - int period = a.length / m; - int v = 1, i = 0; - - for (int k = 0; k < m; k++) { - v = 1; - - for (int p = 0; p < period; p++) { - a[i++] = v++; - } - } - - for (int j = i; j < a.length - 1; j++) { - a[j] = v++; - } - - a[a.length - 1] = 0; - } - }, - - DESCENDING { - void build(int[] a, int m) { - int period = a.length / m; - int v = -1, i = 0; - - for (int k = 0; k < m; k++) { - v = -1; - - for (int p = 0; p < period; p++) { - a[i++] = v--; - } - } - - for (int j = i; j < a.length - 1; j++) { - a[j] = v--; - } - - a[a.length - 1] = 0; - } }, POINT { + @Override void build(int[] a, int m) { - for (int i = 0; i < a.length; i++) { - a[i] = 0; - } + Arrays.fill(a, 0); a[a.length / 2] = m; } }, LINE { + @Override void build(int[] a, int m) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = i; } - reverse(a, 0, a.length - 1); + reverse(a, m, a.length - 1); } }, PEARL { + @Override void build(int[] a, int m) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = i; } - reverse(a, 0, 2); + reverse(a, 0, Math.min(m, a.length)); } }, RING { + @Override void build(int[] a, int m) { int k1 = a.length / 3; int k2 = a.length / 3 * 2; int level = a.length / 3; - for (int i = 0, k = level; i < k1; i++) { + for (int i = 0, k = level; i < k1; ++i) { a[i] = k--; } - for (int i = k1; i < k2; i++) { + for (int i = k1; i < k2; ++i) { a[i] = 0; } - for (int i = k2, k = level; i < a.length; i++) { + for (int i = k2, k = level; i < a.length; ++i) { a[i] = k--; } } @@ -1873,22 +1662,25 @@ private static void reverse(int[] a, int lo, int hi) { } } - private static enum NegativeZeroBuilder { + private enum NegativeZeroBuilder { + FLOAT { + @Override void build(Object o, Random random) { float[] a = (float[]) o; - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = random.nextBoolean() ? -0.0f : 0.0f; } } }, DOUBLE { + @Override void build(Object o, Random random) { double[] a = (double[]) o; - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = random.nextBoolean() ? -0.0d : 0.0d; } } @@ -1897,109 +1689,99 @@ void build(Object o, Random random) { abstract void build(Object o, Random random); } - private static enum FloatingPointBuilder { + private enum FloatingPointBuilder { + FLOAT { + @Override void build(Object o, int a, int g, int z, int n, int p, Random random) { float negativeValue = -random.nextFloat(); float positiveValue = random.nextFloat(); - float[] x = (float[]) o; + float[] data = (float[]) o; int fromIndex = 0; - writeValue(x, negativeValue, fromIndex, n); + fillWithValue(data, Float.NEGATIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, -Float.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(x, -0.0f, fromIndex, g); + fillWithValue(data, -0.0f, fromIndex, g); fromIndex += g; - writeValue(x, 0.0f, fromIndex, z); + fillWithValue(data, 0.0f, fromIndex, z); fromIndex += z; - writeValue(x, positiveValue, fromIndex, p); + fillWithValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(x, Float.NaN, fromIndex, a); + fillWithValue(data, Float.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, Float.POSITIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, Float.NaN, fromIndex, a); } }, DOUBLE { + @Override void build(Object o, int a, int g, int z, int n, int p, Random random) { double negativeValue = -random.nextFloat(); double positiveValue = random.nextFloat(); - double[] x = (double[]) o; + double[] data = (double[]) o; int fromIndex = 0; - writeValue(x, negativeValue, fromIndex, n); + fillWithValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); + fromIndex++; + + fillWithValue(data, -Double.MAX_VALUE, fromIndex, 1); + fromIndex++; + + fillWithValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(x, -0.0d, fromIndex, g); + fillWithValue(data, -0.0d, fromIndex, g); fromIndex += g; - writeValue(x, 0.0d, fromIndex, z); + fillWithValue(data, 0.0d, fromIndex, z); fromIndex += z; - writeValue(x, positiveValue, fromIndex, p); + fillWithValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(x, Double.NaN, fromIndex, a); + fillWithValue(data, Double.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, Double.POSITIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + fillWithValue(data, Double.NaN, fromIndex, a); } }; abstract void build(Object o, int a, int g, int z, int n, int p, Random random); - private static void writeValue(float[] a, float value, int fromIndex, int count) { - for (int i = fromIndex; i < fromIndex + count; i++) { + private static void fillWithValue(float[] a, float value, int fromIndex, int count) { + for (int i = fromIndex; i < fromIndex + count; ++i) { a[i] = value; } } - private static void writeValue(double[] a, double value, int fromIndex, int count) { - for (int i = fromIndex; i < fromIndex + count; i++) { + private static void fillWithValue(double[] a, double value, int fromIndex, int count) { + for (int i = fromIndex; i < fromIndex + count; ++i) { a[i] = value; } } } - private static Comparator pairComparator = new Comparator() { - - @Override - public int compare(Pair p1, Pair p2) { - return p1.compareTo(p2); - } - }; - - private static class Pair implements Comparable { - - private Pair(int key, int value) { - this.key = key; - this.value = value; - } - - int getKey() { - return key; - } - - int getValue() { - return value; - } - - @Override - public int compareTo(Pair pair) { - return Integer.compare(key, pair.key); - } - - @Override - public String toString() { - return "(" + key + ", " + value + ")"; - } - - private int key; - private int value; - } - private static class TestRandom extends Random { - private static final TestRandom BABA = new TestRandom(0xBABA); private static final TestRandom DEDA = new TestRandom(0xDEDA); + private static final TestRandom BABA = new TestRandom(0xBABA); private static final TestRandom C0FFEE = new TestRandom(0xC0FFEE); private TestRandom(long seed) { @@ -2012,6 +1794,6 @@ public String toString() { return seed; } - private String seed; + private final String seed; } } diff --git a/test/jdk/java/util/Arrays/java.base/java/util/SortingHelper.java b/test/jdk/java/util/Arrays/java.base/java/util/SortingHelper.java index a8318b6e3747d..1594ffe96ae73 100644 --- a/test/jdk/java/util/Arrays/java.base/java/util/SortingHelper.java +++ b/test/jdk/java/util/Arrays/java.base/java/util/SortingHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,179 +29,142 @@ * * @author Vladimir Yaroslavskiy * - * @version 2019.09.19 + * @version 2022.06.14 * - * @since 14 + * @since 14 ^ 20 */ public enum SortingHelper { DUAL_PIVOT_QUICKSORT("Dual-Pivot Quicksort") { + @Override + public void sort(Object a, int low, int high) { + sort(a, SEQUENTIAL, low, high); + } + }, + PARALLEL_SORT("Parallel sort") { @Override - public void sort(Object a) { - if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, SEQUENTIAL, 0, ((int[]) a).length); - } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, SEQUENTIAL, 0, ((long[]) a).length); - } else if (a instanceof byte[]) { - DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length); - } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, SEQUENTIAL, 0, ((char[]) a).length); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, SEQUENTIAL, 0, ((short[]) a).length); - } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, SEQUENTIAL, 0, ((float[]) a).length); - } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, SEQUENTIAL, 0, ((double[]) a).length); - } else { - fail(a); - } + public void sort(Object a, int low, int high) { + sort(a, PARALLEL, low, high); } + }, + MIXED_INSERTION_SORT("Mixed insertion sort") { @Override public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.mixedInsertionSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.mixedInsertionSort((long[]) a, low, high); } else if (a instanceof byte[]) { DualPivotQuicksort.sort((byte[]) a, low, high); } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.mixedInsertionSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, SEQUENTIAL, low, high); + DualPivotQuicksort.mixedInsertionSort((double[]) a, low, high); } else { fail(a); } } - - @Override - public void sort(Object[] a) { - fail(a); - } - - @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); - } }, - PARALLEL_SORT("Parallel sort") { - + INSERTION_SORT("Insertion sort") { @Override - public void sort(Object a) { + public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, PARALLEL, 0, ((int[]) a).length); + DualPivotQuicksort.insertionSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, PARALLEL, 0, ((long[]) a).length); + DualPivotQuicksort.insertionSort((long[]) a, low, high); } else if (a instanceof byte[]) { - DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length); + DualPivotQuicksort.insertionSort((byte[]) a, low, high); } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, PARALLEL, 0, ((char[]) a).length); + DualPivotQuicksort.insertionSort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, PARALLEL, 0, ((short[]) a).length); + DualPivotQuicksort.insertionSort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, PARALLEL, 0, ((float[]) a).length); + DualPivotQuicksort.insertionSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, PARALLEL, 0, ((double[]) a).length); + DualPivotQuicksort.insertionSort((double[]) a, low, high); } else { fail(a); } } + }, + MERGING_SORT("Merging sort") { @Override public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, PARALLEL, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (int[]) a, low, high - low)); } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, PARALLEL, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (long[]) a, low, high - low)); } else if (a instanceof byte[]) { DualPivotQuicksort.sort((byte[]) a, low, high); } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, PARALLEL, low, high); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, PARALLEL, low, high); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, PARALLEL, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (float[]) a, low, high - low)); } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, PARALLEL, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (double[]) a, low, high - low)); } else { fail(a); } } - - @Override - public void sort(Object[] a) { - fail(a); - } - - @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); - } }, - HEAP_SORT("Heap sort") { - + RADIX_SORT("Radix sort") { @Override - public void sort(Object a) { + public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.sort(null, (int[]) a, BIG_DEPTH, 0, ((int[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (int[]) a, low, high)); } else if (a instanceof long[]) { - DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, 0, ((long[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (long[]) a, low, high)); } else if (a instanceof byte[]) { - DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length); + DualPivotQuicksort.sort((byte[]) a, low, high); } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, BIG_DEPTH, 0, ((char[]) a).length); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, BIG_DEPTH, 0, ((short[]) a).length); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort(null, (float[]) a, BIG_DEPTH, 0, ((float[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high)); } else if (a instanceof double[]) { - DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, 0, ((double[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (double[]) a, low, high)); } else { fail(a); } } + }, + HEAP_SORT("Heap sort") { @Override public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.sort(null, (int[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.heapSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.heapSort((long[]) a, low, high); } else if (a instanceof byte[]) { DualPivotQuicksort.sort((byte[]) a, low, high); } else if (a instanceof char[]) { - DualPivotQuicksort.sort((char[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort(null, (float[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.heapSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, low, high); + DualPivotQuicksort.heapSort((double[]) a, low, high); } else { fail(a); } } - - @Override - public void sort(Object[] a) { - fail(a); - } - - @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); - } }, ARRAYS_SORT("Arrays.sort") { - @Override public void sort(Object a) { if (a instanceof int[]) { @@ -243,21 +206,9 @@ public void sort(Object a, int low, int high) { fail(a); } } - - @Override - public void sort(Object[] a) { - Arrays.sort(a); - } - - @Override - @SuppressWarnings("unchecked") - public void sort(Object[] a, Comparator comparator) { - Arrays.sort(a, comparator); - } }, ARRAYS_PARALLEL_SORT("Arrays.parallelSort") { - @Override public void sort(Object a) { if (a instanceof int[]) { @@ -299,29 +250,31 @@ public void sort(Object a, int low, int high) { fail(a); } } - - @Override - @SuppressWarnings("unchecked") - public void sort(Object[] a) { - Arrays.parallelSort((Comparable[]) a); - } - - @Override - @SuppressWarnings("unchecked") - public void sort(Object[] a, Comparator comparator) { - Arrays.parallelSort(a, comparator); - } }; - abstract public void sort(Object a); - abstract public void sort(Object a, int low, int high); - abstract public void sort(Object[] a); - - abstract public void sort(Object[] a, Comparator comparator); + public void sort(Object a) { + if (a instanceof int[]) { + sort(a, 0, ((int[]) a).length); + } else if (a instanceof long[]) { + sort(a, 0, ((long[]) a).length); + } else if (a instanceof byte[]) { + sort(a, 0, ((byte[]) a).length); + } else if (a instanceof char[]) { + sort(a, 0, ((char[]) a).length); + } else if (a instanceof short[]) { + sort(a, 0, ((short[]) a).length); + } else if (a instanceof float[]) { + sort(a, 0, ((float[]) a).length); + } else if (a instanceof double[]) { + sort(a, 0, ((double[]) a).length); + } else { + fail(a); + } + } - private SortingHelper(String name) { + SortingHelper(String name) { this.name = name; } @@ -330,21 +283,49 @@ public String toString() { return name; } + static void sort(Object a, int parallelism, int low, int high) { + if (a instanceof int[]) { + DualPivotQuicksort.sort((int[]) a, parallelism, low, high); + } else if (a instanceof long[]) { + DualPivotQuicksort.sort((long[]) a, parallelism, low, high); + } else if (a instanceof byte[]) { + DualPivotQuicksort.sort((byte[]) a, low, high); + } else if (a instanceof char[]) { + DualPivotQuicksort.sort((char[]) a, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, low, high); + } else if (a instanceof float[]) { + DualPivotQuicksort.sort((float[]) a, parallelism, low, high); + } else if (a instanceof double[]) { + DualPivotQuicksort.sort((double[]) a, parallelism, low, high); + } else { + fail(a); + } + } + + private static void check(String name, boolean result) { + if (!result) { + fail(name + " sort must return true"); + } + } + private static void fail(Object a) { - throw new RuntimeException("Unexpected type of array: " + a.getClass().getName()); + fail("Unknown array: " + a.getClass().getName()); + } + + private static void fail(String message) { + throw new RuntimeException(message); } - private String name; + private final String name; /** - * Parallelism level for sequential and parallel sorting. + * Parallelism level for sequential sorting. */ private static final int SEQUENTIAL = 0; - private static final int PARALLEL = 87; /** - * Heap sort will be invoked, if recursion depth is too big. - * Value is taken from DualPivotQuicksort.MAX_RECURSION_DEPTH. + * Parallelism level for parallel sorting. */ - private static final int BIG_DEPTH = 64 * (3 << 1); + private static final int PARALLEL = 88; } diff --git a/test/micro/org/openjdk/bench/java/util/ArraysSort.java b/test/micro/org/openjdk/bench/java/util/ArraysSort.java index 4cd45d79412c1..9edc0bfc06790 100644 --- a/test/micro/org/openjdk/bench/java/util/ArraysSort.java +++ b/test/micro/org/openjdk/bench/java/util/ArraysSort.java @@ -20,144 +20,292 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -package org.openjdk.bench.java.lang; + +package org.openjdk.bench.java.util; + +import java.util.Arrays; +import java.util.Random; +import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OperationsPerInvocation; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Param; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.Level; import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; -import java.util.Arrays; -import java.util.Random; -import java.util.concurrent.TimeUnit; -import java.io.UnsupportedEncodingException; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.reflect.Method; /** - * Performance test of Arrays.sort() methods + * Microbenchmark for Arrays.sort() and Arrays.parallelSort(). + * + * @author Vladimir Yaroslavskiy + * + * @version 2022.06.14 + * + * @since 22 */ -@Fork(value=1, jvmArgsAppend={"-XX:CompileThreshold=1", "-XX:-TieredCompilation"}) +@State(Scope.Benchmark) @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.MICROSECONDS) -@State(Scope.Thread) -@Warmup(iterations = 3, time=5) -@Measurement(iterations = 3, time=3) +@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 4, time = 3, timeUnit = TimeUnit.SECONDS) +@Fork(value=1, jvmArgsAppend={"-XX:CompileThreshold=1", "-XX:-TieredCompilation"}) public class ArraysSort { - @Param({"10","25","50","75","100", "1000", "10000", "100000", "1000000"}) - private int size; - - private int[] ints_unsorted; - private long[] longs_unsorted; - private float[] floats_unsorted; - private double[] doubles_unsorted; - - private int[] ints_sorted; - private long[] longs_sorted; - private float[] floats_sorted; - private double[] doubles_sorted; - - - public void initialize() { - Random rnd = new Random(42); - - ints_unsorted = new int[size]; - longs_unsorted = new long[size]; - floats_unsorted = new float[size]; - doubles_unsorted = new double[size]; - - int[] intSpecialCases = {Integer.MIN_VALUE, Integer.MAX_VALUE}; - long[] longSpecialCases = {Long.MIN_VALUE, Long.MAX_VALUE}; - float[] floatSpecialCases = {+0.0f, -0.0f, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN}; - double[] doubleSpecialCases = {+0.0, -0.0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN}; - - for (int i = 0; i < size; i++) { - ints_unsorted[i] = rnd.nextInt(); - longs_unsorted[i] = rnd.nextLong(); - if (i % 10 != 0) { - ints_unsorted[i] = rnd.nextInt(); - longs_unsorted[i] = rnd.nextLong(); - floats_unsorted[i] = rnd.nextFloat(); - doubles_unsorted[i] = rnd.nextDouble(); - } else { - ints_unsorted[i] = intSpecialCases[rnd.nextInt(intSpecialCases.length)]; - longs_unsorted[i] = longSpecialCases[rnd.nextInt(longSpecialCases.length)]; - floats_unsorted[i] = floatSpecialCases[rnd.nextInt(floatSpecialCases.length)]; - doubles_unsorted[i] = doubleSpecialCases[rnd.nextInt(doubleSpecialCases.length)]; - } - } - } + @Param({ "600", "9000", "20000", "400000", "3000000" }) + int size; + + @Param + Builder builder; + + int[] b; @Setup - public void setup() throws UnsupportedEncodingException, ClassNotFoundException, NoSuchMethodException, Throwable { - initialize(); + public void init() { + b = new int[size]; } - @Setup(Level.Invocation) - public void clear() { - ints_sorted = ints_unsorted.clone(); - longs_sorted = longs_unsorted.clone(); - floats_sorted = floats_unsorted.clone(); - doubles_sorted = doubles_unsorted.clone(); - } + public enum Builder { - @Benchmark - public int[] intSort() throws Throwable { - Arrays.sort(ints_sorted); - return ints_sorted; - } + RANDOM { + @Override + void build(int[] b) { + Random random = new Random(0x888); + + for (int i = 0; i < b.length; ++i) { + b[i] = random.nextInt(); + } + } + }, + + REPEATED { + @Override + void build(int[] b) { + Random random = new Random(0x555); + + for (int i = 0; i < b.length; ++i) { + b[i] = random.nextInt(5); + } + } + }, - @Benchmark - public int[] intParallelSort() throws Throwable { - Arrays.parallelSort(ints_sorted); - return ints_sorted; + STAGGER { + @Override + void build(int[] b) { + for (int i = 0; i < b.length; ++i) { + b[i] = (i * 7) % b.length; + } + } + }, + + SHUFFLE { + @Override + void build(int[] b) { + Random random = new Random(0x555); + + for (int i = 0, j = 0, k = 1; i < b.length; ++i) { + b[i] = random.nextInt(8) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(int[] b); } - @Benchmark - public long[] longSort() throws Throwable { - Arrays.sort(longs_sorted); - return longs_sorted; + public static class Int extends ArraysSort { + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + } + + @Benchmark + public void testSort() { + Arrays.sort(b); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(b); + } } - @Benchmark - public long[] longParallelSort() throws Throwable { - Arrays.parallelSort(longs_sorted); - return longs_sorted; + public static class Long extends ArraysSort { + + long[] a; + + @Setup + public void setup() { + a = new long[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } } - @Benchmark - public float[] floatSort() throws Throwable { - Arrays.sort(floats_sorted); - return floats_sorted; + public static class Byte extends ArraysSort { + + byte[] a; + + @Setup + public void setup() { + a = new byte[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (byte) b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } } - @Benchmark - public float[] floatParallelSort() throws Throwable { - Arrays.parallelSort(floats_sorted); - return floats_sorted; + public static class Char extends ArraysSort { + + char[] a; + + @Setup + public void setup() { + a = new char[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (char) b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } } - @Benchmark - public double[] doubleSort() throws Throwable { - Arrays.sort(doubles_sorted); - return doubles_sorted; + public static class Short extends ArraysSort { + + short[] a; + + @Setup + public void setup() { + a = new short[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (short) b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } } - @Benchmark - public double[] doubleParallelSort() throws Throwable { - Arrays.parallelSort(doubles_sorted); - return doubles_sorted; + public static class Float extends ArraysSort { + + float[] a; + + @Setup + public void setup() { + a = new float[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } } + public static class Double extends ArraysSort { + + double[] a; + + @Setup + public void setup() { + a = new double[size]; + } + + @Setup(Level.Invocation) + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } + } + + @Benchmark + public void testSort() { + Arrays.sort(a); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(a); + } + } }