From 087b1ea68a3badae3cfb151a70797d9f8ce0dfb8 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Sat, 8 May 2021 23:50:42 +0300 Subject: [PATCH 01/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Sorting: - adopt radix sort for sequential and parallel sorts on int/long/float/double arrays (almost random and length > 6K) - fix tryMergeRuns() to better handle case when the last run is a single element - minor javadoc and comment changes Testing: - add new data inputs in tests for sorting - add min/max/infinity values to float/double testing - add tests for radix sort --- .../classes/java/util/DualPivotQuicksort.java | 713 ++++++++++++++++-- test/jdk/java/util/Arrays/Sorting.java | 168 +++-- .../java.base/java/util/SortingHelper.java | 85 ++- 3 files changed, 811 insertions(+), 155 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index ac96b34c5fb32..89b6d46e03898 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2021, 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 @@ -37,16 +37,16 @@ * * 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. + * sort, radix sort, counting sort and parallel merge sort. * * @author Vladimir Yaroslavskiy * @author Jon Bentley * @author Josh Bloch * @author Doug Lea * - * @version 2018.08.18 + * @version 2020.06.14 * - * @since 1.7 * 14 + * @since 1.7 * 14 & 17 */ final class DualPivotQuicksort { @@ -111,14 +111,24 @@ private DualPivotQuicksort() {} private static final int MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE = 1750; /** - * Threshold of mixed insertion sort is incremented by this value. + * Min array size to use radix sort. */ - private static final int DELTA = 3 << 1; + private static final int MIN_RADIX_SORT_SIZE = 6 << 10; + + /** + * Threshold of mixed insertion sort is increased by this value. + */ + private static final int DEPTH = 3 << 1; + + /** + * Min depth to invoke radix sort. + */ + private static final int MIN_RADIX_SORT_DEPTH = DEPTH << 2; /** * Max recursive partitioning depth before using heap sort. */ - private static final int MAX_RECURSION_DEPTH = 64 * DELTA; + private static final int MAX_RECURSION_DEPTH = 64 * DEPTH; /** * Calculates the double depth of parallel merging. @@ -157,7 +167,7 @@ static void sort(int[] a, int parallelism, int low, int high) { if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { int depth = getDepth(parallelism, size >> 12); - int[] b = depth == 0 ? null : new int[size]; + int[] b = depth == 0 ? null : (int[]) tryAllocate(a, size); new Sorter(null, a, b, low, size, low, depth).invoke(); } else { sort(null, a, 0, low, high); @@ -166,7 +176,7 @@ static void sort(int[] 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. + * other sorts in special cases, possibly with parallel partitions. * * @param sorter parallel context * @param a the array to be sorted @@ -205,10 +215,10 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Switch to heap sort if execution + * Switch to heap sort, if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); return; } @@ -269,10 +279,18 @@ && tryMergeRuns(sorter, a, low, size)) { 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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { + /* + * Invoke radix sort on large array. + */ + if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE + && radixSort(sorter, a, low, high)) { + return; + } + /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -618,6 +636,126 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { a[p] = value; } + /** + * Sorts the specified range of the array using 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 true if finally sorted, false otherwise + */ + static boolean radixSort(Sorter sorter, int[] a, int low, int high) { + int[] b; int offset = low; + + if (sorter == null || (b = (int[]) sorter.b) == null) { + b = (int[]) tryAllocate(a, high - low); + + if (b == null) { + return false; + } + } else { + offset = sorter.offset; + } + + int start = low - offset; + int last = high - offset; + + int[] count1 = new int[256]; + int[] count2 = new int[256]; + int[] count3 = new int[256]; + int[] count4 = new int[256]; + + for (int i = low; i < high; ++i) { + count1[ a[i] & 0xFF]--; + count2[(a[i] >>> 8) & 0xFF]--; + count3[(a[i] >>> 16) & 0xFF]--; + count4[(a[i] >>> 24) ^ 0x80]--; + } + + boolean passLevel4 = passLevel(count4, 255, low - high, high); + boolean passLevel3 = passLevel(count3, 255, low - high, high); + boolean passLevel2 = passLevel(count2, 255, low - high, high); + boolean passLevel1 = passLevel(count1, 255, low - high, high); + + if (passLevel1) { + for (int i = low; i < high; ++i) { + b[count1[a[i] & 0xFF]++ - offset] = a[i]; + } + } + + if (passLevel2) { + if (passLevel1) { + for (int i = start; i < last; ++i) { + a[count2[(b[i] >>> 8) & 0xFF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(a[i] >>> 8) & 0xFF]++ - offset] = a[i]; + } + } + } + + if (passLevel3) { + if (passLevel1 ^ passLevel2) { + for (int i = start; i < last; ++i) { + a[count3[(b[i] >>> 16) & 0xFF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count3[(a[i] >>> 16) & 0xFF]++ - offset] = a[i]; + } + } + } + + if (passLevel4) { + if (passLevel1 ^ passLevel2 ^ passLevel3) { + for (int i = start; i < last; ++i) { + a[count4[(b[i] >>> 24) ^ 0x80]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count4[(a[i] >>> 24) ^ 0x80]++ - offset] = a[i]; + } + } + } + + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + System.arraycopy(b, low - offset, a, low, high - low); + } + return true; + } + + /** + * Scans count array and creates histogram. + * + * @param count the count array + * @param last the index of the last count + * @param size the array size + * @param high the index of the last element, exclusive + * @return false if the level can be skipped, true otherwise + */ + private static boolean passLevel(int[] count, int last, int size, int high) { + for (int c : count) { + if (c == 0) { + continue; + } + if (c == size) { // All elements are equal + return false; + } + break; + } + + /* + * Compute histogram. + */ + count[last] += high; + + for (int i = last; i > 0; --i) { + count[i - 1] += count[i]; + } + return true; + } + /** * Tries to sort the specified range of the array. * @@ -673,7 +811,6 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { */ if (run == null) { if (k == high) { - /* * The array is monotonous sequence, * and therefore already sorted. @@ -682,7 +819,6 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { } if (k - low < MIN_FIRST_RUN_SIZE) { - /* * The first run is too small * to proceed with scanning. @@ -690,13 +826,13 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { return false; } + // min 127, max 1023, ext 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; - } else if (a[last - 1] > a[last]) { + } else if (a[last - 1] > a[last]) { // Start new run if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* * The first runs are not long * enough to continue scanning. @@ -705,7 +841,6 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { } if (++count == MAX_RUN_CAPACITY) { - /* * Array is not highly structured. */ @@ -713,7 +848,6 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { } if (count == run.length) { - /* * Increase capacity of index array. */ @@ -721,6 +855,13 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { } } run[count] = (last = k); + + if (++k == high) { + /* + * There is a single-element run at the end. + */ + --k; + } } /* @@ -730,7 +871,11 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { int[] b; int offset = low; if (sorter == null || (b = (int[]) sorter.b) == null) { - b = new int[size]; + b = (int[]) tryAllocate(a, size); + + if (b == null) { + return false; + } } else { offset = sorter.offset; } @@ -911,7 +1056,7 @@ static void sort(long[] a, int parallelism, int low, int high) { if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { int depth = getDepth(parallelism, size >> 12); - long[] b = depth == 0 ? null : new long[size]; + long[] b = depth == 0 ? null : (long[]) tryAllocate(a, size); new Sorter(null, a, b, low, size, low, depth).invoke(); } else { sort(null, a, 0, low, high); @@ -920,7 +1065,7 @@ static void sort(long[] 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. + * other sorts in special cases, possibly with parallel partitions. * * @param sorter parallel context * @param a the array to be sorted @@ -959,10 +1104,10 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Switch to heap sort if execution + * Switch to heap sort, if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); return; } @@ -1023,10 +1168,18 @@ && tryMergeRuns(sorter, a, low, size)) { 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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { + /* + * Invoke radix sort on large array. + */ + if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE + && radixSort(sorter, a, low, high)) { + return; + } + /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -1372,6 +1525,125 @@ private static void pushDown(long[] a, int p, long value, int low, int high) { a[p] = value; } + /** + * Sorts the specified range of the array using 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 true if finally sorted, false otherwise + */ + static boolean radixSort(Sorter sorter, long[] a, int low, int high) { + long[] b; int offset = low; + + if (sorter == null || (b = (long[]) sorter.b) == null) { + b = (long[]) tryAllocate(a, high - low); + + if (b == null) { + return false; + } + } else { + offset = sorter.offset; + } + + int start = low - offset; + int last = high - offset; + + 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)]--; + } + + boolean passLevel6 = passLevel(count6, 1023, low - high, high); + boolean passLevel5 = passLevel(count5, 2047, low - high, high); + boolean passLevel4 = passLevel(count4, 2047, low - high, high); + boolean passLevel3 = passLevel(count3, 2047, low - high, high); + boolean passLevel2 = passLevel(count2, 2047, low - high, high); + boolean passLevel1 = passLevel(count1, 1023, low - high, high); + + if (passLevel1) { + for (int i = low; i < high; ++i) { + b[count1[(int) (a[i] & 0x3FF)]++ - offset] = a[i]; + } + } + + if (passLevel2) { + if (passLevel1) { + for (int i = start; i < last; ++i) { + a[count2[(int) ((b[i] >>> 10) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(int) ((a[i] >>> 10) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel3) { + if (passLevel1 ^ passLevel2) { + for (int i = start; i < last; ++i) { + a[count3[(int) ((b[i] >>> 21) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count3[(int) ((a[i] >>> 21) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel4) { + if (passLevel1 ^ passLevel2 ^ passLevel3) { + for (int i = start; i < last; ++i) { + a[count4[(int) ((b[i] >>> 32) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count4[(int) ((a[i] >>> 32) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel5) { + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + for (int i = start; i < last; ++i) { + a[count5[(int) ((b[i] >>> 43) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count5[(int) ((a[i] >>> 43) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel6) { + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5) { + for (int i = start; i < last; ++i) { + a[count6[(int) ((b[i] >>> 54) ^ 0x200)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count6[(int) ((a[i] >>> 54) ^ 0x200)]++ - offset] = a[i]; + } + } + } + + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { + System.arraycopy(b, low - offset, a, low, high - low); + } + return true; + } + /** * Tries to sort the specified range of the array. * @@ -1427,7 +1699,6 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) */ if (run == null) { if (k == high) { - /* * The array is monotonous sequence, * and therefore already sorted. @@ -1436,7 +1707,6 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) } if (k - low < MIN_FIRST_RUN_SIZE) { - /* * The first run is too small * to proceed with scanning. @@ -1444,13 +1714,13 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) return false; } + // min 127, max 1023, ext 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; - } else if (a[last - 1] > a[last]) { + } else if (a[last - 1] > a[last]) { // Start new run if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* * The first runs are not long * enough to continue scanning. @@ -1459,7 +1729,6 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) } if (++count == MAX_RUN_CAPACITY) { - /* * Array is not highly structured. */ @@ -1467,7 +1736,6 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) } if (count == run.length) { - /* * Increase capacity of index array. */ @@ -1475,6 +1743,13 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) } } run[count] = (last = k); + + if (++k == high) { + /* + * There is a single-element run at the end. + */ + --k; + } } /* @@ -1484,7 +1759,11 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) long[] b; int offset = low; if (sorter == null || (b = (long[]) sorter.b) == null) { - b = new long[size]; + b = (long[]) tryAllocate(a, size); + + if (b == null) { + return false; + } } else { offset = sorter.offset; } @@ -1702,7 +1981,7 @@ 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 histogram for all values. */ for (int i = high; i > low; ++count[a[--i] & 0xFF]); @@ -1751,7 +2030,7 @@ 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. + * other sorts in special cases, possibly with parallel partitions. * * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where @@ -1775,7 +2054,7 @@ static void sort(char[] a, int bits, int low, int high) { * Switch to counting sort if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } @@ -1836,7 +2115,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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2025,7 +2304,7 @@ private static void countingSort(char[] a, int low, int high) { int[] count = new int[NUM_CHAR_VALUES]; /* - * Compute a histogram with the number of each values. + * Compute histogram for all values. */ for (int i = high; i > low; ++count[a[--i]]); @@ -2070,7 +2349,7 @@ static void sort(short[] 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. + * other sorts in special cases, possibly with parallel partitions. * * @param a the array to be sorted * @param bits the combination of recursion depth and bit flag, where @@ -2094,7 +2373,7 @@ static void sort(short[] a, int bits, int low, int high) { * Switch to counting sort if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } @@ -2155,7 +2434,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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2349,7 +2628,7 @@ 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 histogram for all values. */ for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); @@ -2423,7 +2702,7 @@ static void sort(float[] a, int parallelism, int low, int high) { if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { int depth = getDepth(parallelism, size >> 12); - float[] b = depth == 0 ? null : new float[size]; + float[] b = depth == 0 ? null : (float[]) tryAllocate(a, size); new Sorter(null, a, b, low, size, low, depth).invoke(); } else { sort(null, a, 0, low, high); @@ -2461,7 +2740,7 @@ 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. + * other sorts in special cases, possibly with parallel partitions. * * @param sorter parallel context * @param a the array to be sorted @@ -2500,10 +2779,10 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Switch to heap sort if execution + * Switch to heap sort, if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); return; } @@ -2564,10 +2843,18 @@ && tryMergeRuns(sorter, a, low, size)) { 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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { + /* + * Invoke radix sort on large array. + */ + if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE + && radixSort(sorter, a, low, high)) { + return; + } + /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -2913,6 +3200,106 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { a[p] = value; } + /** + * Sorts the specified range of the array using 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 true if finally sorted, false otherwise + */ + static boolean radixSort(Sorter sorter, float[] a, int low, int high) { + float[] b; int offset = low; + + if (sorter == null || (b = (float[]) sorter.b) == null) { + b = (float[]) tryAllocate(a, high - low); + + if (b == null) { + return false; + } + } else { + offset = sorter.offset; + } + + int start = low - offset; + int last = high - offset; + + int[] count1 = new int[256]; + int[] count2 = new int[256]; + int[] count3 = new int[256]; + int[] count4 = new int[256]; + + for (int i = low; i < high; ++i) { + count1[ fti(a[i]) & 0xFF]--; + count2[(fti(a[i]) >>> 8) & 0xFF]--; + count3[(fti(a[i]) >>> 16) & 0xFF]--; + count4[(fti(a[i]) >>> 24) & 0xFF]--; + } + + boolean passLevel4 = passLevel(count4, 255, low - high, high); + boolean passLevel3 = passLevel(count3, 255, low - high, high); + boolean passLevel2 = passLevel(count2, 255, low - high, high); + boolean passLevel1 = passLevel(count1, 255, low - high, high); + + if (passLevel1) { + for (int i = low; i < high; ++i) { + b[count1[fti(a[i]) & 0xFF]++ - offset] = a[i]; + } + } + + if (passLevel2) { + if (passLevel1) { + for (int i = start; i < last; ++i) { + a[count2[(fti(b[i]) >>> 8) & 0xFF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(fti(a[i]) >>> 8) & 0xFF]++ - offset] = a[i]; + } + } + } + + if (passLevel3) { + if (passLevel1 ^ passLevel2) { + for (int i = start; i < last; ++i) { + a[count3[(fti(b[i]) >>> 16) & 0xFF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count3[(fti(a[i]) >>> 16) & 0xFF]++ - offset] = a[i]; + } + } + } + + if (passLevel4) { + if (passLevel1 ^ passLevel2 ^ passLevel3) { + for (int i = start; i < last; ++i) { + a[count4[(fti(b[i]) >>> 24) & 0xFF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count4[(fti(a[i]) >>> 24) & 0xFF]++ - offset] = a[i]; + } + } + } + + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + System.arraycopy(b, low - offset, a, low, high - low); + } + return true; + } + + /** + * Returns masked bits that represent the float number. + * + * @param f the given number + * @return masked bits + */ + private static int fti(float f) { + int x = Float.floatToRawIntBits(f); + return x ^ ((x >> 31) | 0x80000000); + } + /** * Tries to sort the specified range of the array. * @@ -2968,7 +3355,6 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) */ if (run == null) { if (k == high) { - /* * The array is monotonous sequence, * and therefore already sorted. @@ -2977,7 +3363,6 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) } if (k - low < MIN_FIRST_RUN_SIZE) { - /* * The first run is too small * to proceed with scanning. @@ -2985,13 +3370,13 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) return false; } + // min 127, max 1023, ext 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; - } else if (a[last - 1] > a[last]) { + } else if (a[last - 1] > a[last]) { // Start new run if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* * The first runs are not long * enough to continue scanning. @@ -3000,7 +3385,6 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) } if (++count == MAX_RUN_CAPACITY) { - /* * Array is not highly structured. */ @@ -3008,7 +3392,6 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) } if (count == run.length) { - /* * Increase capacity of index array. */ @@ -3016,6 +3399,13 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) } } run[count] = (last = k); + + if (++k == high) { + /* + * There is a single-element run at the end. + */ + --k; + } } /* @@ -3025,7 +3415,11 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) float[] b; int offset = low; if (sorter == null || (b = (float[]) sorter.b) == null) { - b = new float[size]; + b = (float[]) tryAllocate(a, size); + + if (b == null) { + return false; + } } else { offset = sorter.offset; } @@ -3229,7 +3623,7 @@ static void sort(double[] a, int parallelism, int low, int high) { if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { int depth = getDepth(parallelism, size >> 12); - double[] b = depth == 0 ? null : new double[size]; + double[] b = depth == 0 ? null : (double[]) tryAllocate(a, size); new Sorter(null, a, b, low, size, low, depth).invoke(); } else { sort(null, a, 0, low, high); @@ -3267,7 +3661,7 @@ 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. + * other sorts in special cases, possibly with parallel partitions. * * @param sorter parallel context * @param a the array to be sorted @@ -3306,10 +3700,10 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Switch to heap sort if execution + * Switch to heap sort, if execution * time is becoming quadratic. */ - if ((bits += DELTA) > MAX_RECURSION_DEPTH) { + if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); return; } @@ -3370,10 +3764,18 @@ && tryMergeRuns(sorter, a, low, size)) { 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 in case of different elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { + /* + * Invoke radix sort on large array. + */ + if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE + && radixSort(sorter, a, low, high)) { + return; + } + /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -3719,6 +4121,136 @@ private static void pushDown(double[] a, int p, double value, int low, int high) a[p] = value; } + /** + * Sorts the specified range of the array using 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 true if finally sorted, false otherwise + */ + static boolean radixSort(Sorter sorter, double[] a, int low, int high) { + double[] b; int offset = low; + + if (sorter == null || (b = (double[]) sorter.b) == null) { + b = (double[]) tryAllocate(a, high - low); + + if (b == null) { + return false; + } + } else { + offset = sorter.offset; + } + + int start = low - offset; + int last = high - offset; + + 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)]--; + } + + boolean passLevel6 = passLevel(count6, 1023, low - high, high); + boolean passLevel5 = passLevel(count5, 2047, low - high, high); + boolean passLevel4 = passLevel(count4, 2047, low - high, high); + boolean passLevel3 = passLevel(count3, 2047, low - high, high); + boolean passLevel2 = passLevel(count2, 2047, low - high, high); + boolean passLevel1 = passLevel(count1, 1023, low - high, high); + + if (passLevel1) { + for (int i = low; i < high; ++i) { + b[count1[(int) (dtl(a[i]) & 0x3FF)]++ - offset] = a[i]; + } + } + + if (passLevel2) { + if (passLevel1) { + for (int i = start; i < last; ++i) { + a[count2[(int) ((dtl(b[i]) >>> 10) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(int) ((dtl(a[i]) >>> 10) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel3) { + if (passLevel1 ^ passLevel2) { + for (int i = start; i < last; ++i) { + a[count3[(int) ((dtl(b[i]) >>> 21) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count3[(int) ((dtl(a[i]) >>> 21) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel4) { + if (passLevel1 ^ passLevel2 ^ passLevel3) { + for (int i = start; i < last; ++i) { + a[count4[(int) ((dtl(b[i]) >>> 32) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count4[(int) ((dtl(a[i]) >>> 32) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel5) { + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + for (int i = start; i < last; ++i) { + a[count5[(int) ((dtl(b[i]) >>> 43) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count5[(int) ((dtl(a[i]) >>> 43) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel6) { + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5) { + for (int i = start; i < last; ++i) { + a[count6[(int) ((dtl(b[i]) >>> 54) & 0x3FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count6[(int) ((dtl(a[i]) >>> 54) & 0x3FF)]++ - offset] = a[i]; + } + } + } + + if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { + System.arraycopy(b, low - offset, a, low, high - low); + } + return true; + } + + /** + * Returns masked bits that represent the double number. + * + * @param f the given number + * @return masked bits + */ + private static long dtl(double d) { + long x = Double.doubleToRawLongBits(d); + return x ^ ((x >> 63) | 0x8000000000000000L); + } + /** * Tries to sort the specified range of the array. * @@ -3774,7 +4306,6 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size */ if (run == null) { if (k == high) { - /* * The array is monotonous sequence, * and therefore already sorted. @@ -3783,7 +4314,6 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size } if (k - low < MIN_FIRST_RUN_SIZE) { - /* * The first run is too small * to proceed with scanning. @@ -3791,13 +4321,13 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size return false; } + // min 127, max 1023, ext 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; - } else if (a[last - 1] > a[last]) { + } else if (a[last - 1] > a[last]) { // Start new run if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* * The first runs are not long * enough to continue scanning. @@ -3806,7 +4336,6 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size } if (++count == MAX_RUN_CAPACITY) { - /* * Array is not highly structured. */ @@ -3814,7 +4343,6 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size } if (count == run.length) { - /* * Increase capacity of index array. */ @@ -3822,6 +4350,13 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size } } run[count] = (last = k); + + if (++k == high) { + /* + * There is a single-element run at the end. + */ + --k; + } } /* @@ -3831,7 +4366,11 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size double[] b; int offset = low; if (sorter == null || (b = (double[]) sorter.b) == null) { - b = new double[size]; + b = (double[]) tryAllocate(a, size); + + if (b == null) { + return false; + } } else { offset = sorter.offset; } @@ -3996,7 +4535,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, * This class implements parallel sorting. */ private static final class Sorter extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 31415926L; private final Object a, b; private final int low, size, offset, depth; @@ -4008,7 +4547,7 @@ private Sorter(CountedCompleter parent, this.low = low; this.size = size; this.offset = offset; - this.depth = depth; + this.depth = b == null ? 0 : depth; } @Override @@ -4028,8 +4567,7 @@ public final void compute() { } else if (a instanceof double[]) { sort(this, (double[]) a, depth, low, low + size); } else { - throw new IllegalArgumentException( - "Unknown type of array: " + a.getClass().getName()); + throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); } } tryComplete(); @@ -4056,7 +4594,7 @@ public final void onCompletion(CountedCompleter caller) { private void forkSorter(int depth, int low, int high) { addToPendingCount(1); - Object a = this.a; // Use local variable for performance + Object a = this.a; // Use local variable for better performance new Sorter(this, a, b, low, high - low, offset, depth).fork(); } } @@ -4065,7 +4603,7 @@ private void forkSorter(int depth, int low, int high) { * This class implements parallel merging. */ private static final class Merger extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 31415926L; private final Object dst, a1, a2; private final int k, lo1, hi1, lo2, hi2; @@ -4097,8 +4635,7 @@ public final void compute() { mergeParts(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 type: " + dst.getClass().getName()); } propagateCompletion(); } @@ -4114,7 +4651,7 @@ private void forkMerger(Object dst, int k, * This class implements parallel merging of runs. */ private static final class RunMerger extends RecursiveTask { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 31415926L; private final Object a, b; private final int[] run; private final int offset, aim, lo, hi; @@ -4144,8 +4681,7 @@ protected final Object compute() { 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()); + throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); } private RunMerger forkMe() { @@ -4158,4 +4694,31 @@ private Object getDestination() { return getRawResult(); } } + + /** + * Tries to allocate memory for new array. + * + * @param a the array of given type + * @param size the new array size + * @return null if there is not enough memory, created array otherwise + */ + private static Object tryAllocate(Object a, int size) { + try { + if (a instanceof int[]) { + return new int[size]; + } + if (a instanceof long[]) { + return new long[size]; + } + if (a instanceof float[]) { + return new float[size]; + } + if (a instanceof double[]) { + return new double[size]; + } + throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); + } catch (OutOfMemoryError e) { + return null; + } + } } diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index e89496bb2e532..ca5875d07980f 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2021, 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 @@ -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 Sorting -shortrun * @summary Exercise Arrays.sort, Arrays.parallelSort @@ -32,6 +32,10 @@ * @author Vladimir Yaroslavskiy * @author Jon Bentley * @author Josh Bloch + * + * @version 2020.06.14 + * + * @since 1.7 * 14 & 17 */ import java.io.PrintStream; @@ -44,24 +48,26 @@ 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) + // Lengths of arrays for long run (default) private static final int[] LONG_RUN_LENGTHS = { 1, 3, 8, 21, 55, 100, 1_000, 10_000, 100_000 }; - // Array lengths used in a short run + // Lengths of arrays for short run private static final int[] SHORT_RUN_LENGTHS = { 1, 8, 55, 100, 10_000 }; - // Random initial values used in a long run (default) + // Initial random values for long run (default) private static final TestRandom[] LONG_RUN_RANDOMS = { TestRandom.BABA, TestRandom.DEDA, TestRandom.C0FFEE }; - // Random initial values used in a short run + // Initial random values for short run private static final TestRandom[] SHORT_RUN_RANDOMS = { 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; @@ -80,6 +86,7 @@ public static void main(String[] args) { 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.RADIX_SORT, randoms, lengths).testCore(); new Sorting(SortingHelper.ARRAYS_SORT, randoms, lengths).testAll(); new Sorting(SortingHelper.ARRAYS_PARALLEL_SORT, randoms, lengths).testAll(); @@ -121,8 +128,8 @@ private void testCore(int length) { testBasic(length); for (TestRandom random : randoms) { - testMergingSort(length, random); testSubArray(length, random); + testMergingSort(length, random); testNegativeZero(length, random); testFloatingPointSorting(length, random); } @@ -139,8 +146,8 @@ private void testAll(int length) { testCore(length); for (TestRandom random : randoms) { - testRange(length, random); testStability(length, random); + testOutOfBounds(length, random); } } @@ -317,7 +324,7 @@ private void testSubArray(int length, TestRandom random) { out.println(); } - private void testRange(int length, TestRandom random) { + private void testOutOfBounds(int length, TestRandom random) { if (length < 2) { return; } @@ -330,7 +337,7 @@ private void testRange(int length, TestRandom random) { for (int i = 0; i < test.length; i++) { printTestName("Test range check", random, length, ", m = " + m + ", " + getType(i)); - checkRange(test[i], m); + checkOutOfBounds(test[i], m); } } out.println(); @@ -473,17 +480,18 @@ 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) { + if (a + g + z + n + p + s != length) { continue; } for (int i = 5; i < test.length; i++) { @@ -495,18 +503,17 @@ private void testFloatingPointSorting(int length, TestRandom 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 = 13; m > 4; m--) { + for (int m = MAX; m > 4; m--) { int t = length / m; int g = t, z = t, n = t, p = t; - int a = length - g - z - n - p; + int a = length - g - z - n - p - s; for (int i = 5; i < test.length; i++) { printTestName("Test float-pointing sorting", random, length, @@ -517,7 +524,7 @@ private void testFloatingPointSorting(int length, TestRandom 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); } } out.println(); @@ -1345,27 +1352,27 @@ private void checkSubArray(double[] a, int fromIndex, int toIndex) { } } - private void checkRange(Object a, int m) { + private void checkOutOfBounds(Object a, int m) { if (a instanceof int[]) { - checkRange((int[]) a, m); + checkOutOfBounds((int[]) a, m); } else if (a instanceof long[]) { - checkRange((long[]) a, m); + checkOutOfBounds((long[]) a, m); } else if (a instanceof byte[]) { - checkRange((byte[]) a, m); + checkOutOfBounds((byte[]) a, m); } else if (a instanceof char[]) { - checkRange((char[]) a, m); + checkOutOfBounds((char[]) a, m); } else if (a instanceof short[]) { - checkRange((short[]) a, m); + checkOutOfBounds((short[]) a, m); } else if (a instanceof float[]) { - checkRange((float[]) a, m); + checkOutOfBounds((float[]) a, m); } else if (a instanceof double[]) { - checkRange((double[]) a, m); + checkOutOfBounds((double[]) a, m); } else { fail("Unknown type of array: " + a.getClass().getName()); } } - private void checkRange(int[] a, int m) { + private void checkOutOfBounds(int[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1385,7 +1392,7 @@ private void checkRange(int[] a, int m) { } } - private void checkRange(long[] a, int m) { + private void checkOutOfBounds(long[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1405,7 +1412,7 @@ private void checkRange(long[] a, int m) { } } - private void checkRange(byte[] a, int m) { + private void checkOutOfBounds(byte[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1425,7 +1432,7 @@ private void checkRange(byte[] a, int m) { } } - private void checkRange(char[] a, int m) { + private void checkOutOfBounds(char[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1445,7 +1452,7 @@ private void checkRange(char[] a, int m) { } } - private void checkRange(short[] a, int m) { + private void checkOutOfBounds(short[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1465,7 +1472,7 @@ private void checkRange(short[] a, int m) { } } - private void checkRange(float[] a, int m) { + private void checkOutOfBounds(float[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1485,7 +1492,7 @@ private void checkRange(float[] a, int m) { } } - private void checkRange(double[] a, int m) { + private void checkOutOfBounds(double[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1562,6 +1569,7 @@ private void printTestName(String test, TestRandom random, int length, String me } private static enum TypeConverter { + LONG { void convert(int[] src, Object dst) { long[] b = (long[]) dst; @@ -1642,6 +1650,7 @@ void build(int[] a, int m) { } private static enum UnsortedBuilder { + RANDOM { void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; i++) { @@ -1650,6 +1659,20 @@ void build(int[] a, int m, Random random) { } }, + PERMUTATION { + void build(int[] a, int m, Random random) { + 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; + } + } + }, + ASCENDING { void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; i++) { @@ -1674,7 +1697,27 @@ void build(int[] a, int m, Random random) { } }, - SAW { + UNIFORM { + void build(int[] a, int m, Random random) { + int mask = (m << 15) - 1; + + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt() & mask; + } + } + }, + + MASKED { + void build(int[] a, int m, Random random) { + int mask = (m << 15) - 1; + + for (int i = 0; i < a.length; ++i) { + a[i] = (i ^ 0xFF) & mask; + } + } + }, + + SAWTOOTH { void build(int[] a, int m, Random random) { int incCount = 1; int decCount = a.length; @@ -1749,10 +1792,10 @@ void build(int[] a, int m, Random random) { SHUFFLE { void build(int[] a, int m, Random random) { - int x = 0, y = 0; + int k = 0, j = 0; for (int i = 0; i < a.length; i++) { - a[i] = random.nextBoolean() ? (x += 2) : (y += 2); + a[i] = random.nextBoolean() ? (k += 2) : (j += 2); } } }, @@ -1772,6 +1815,7 @@ void build(int[] a, int m, Random random) { } private static enum MergingBuilder { + ASCENDING { void build(int[] a, int m) { int period = a.length / m; @@ -1873,6 +1917,7 @@ private static void reverse(int[] a, int lo, int hi) { } private static enum NegativeZeroBuilder { + FLOAT { void build(Object o, Random random) { float[] a = (float[]) o; @@ -1897,26 +1942,39 @@ void build(Object o, Random random) { } private static enum FloatingPointBuilder { + FLOAT { 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); + writeValue(data, Float.NEGATIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + writeValue(data, -Float.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + writeValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(x, -0.0f, fromIndex, g); + writeValue(data, -0.0f, fromIndex, g); fromIndex += g; - writeValue(x, 0.0f, fromIndex, z); + writeValue(data, 0.0f, fromIndex, z); fromIndex += z; - writeValue(x, positiveValue, fromIndex, p); + writeValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(x, Float.NaN, fromIndex, a); + writeValue(data, Float.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + writeValue(data, Float.POSITIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + writeValue(data, Float.NaN, fromIndex, a); } }, @@ -1924,22 +1982,34 @@ void build(Object o, int a, int g, int z, int n, int p, Random random) { 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); + writeValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); + fromIndex++; + + writeValue(data, -Double.MAX_VALUE, fromIndex, 1); + fromIndex++; + + writeValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(x, -0.0d, fromIndex, g); + writeValue(data, -0.0d, fromIndex, g); fromIndex += g; - writeValue(x, 0.0d, fromIndex, z); + writeValue(data, 0.0d, fromIndex, z); fromIndex += z; - writeValue(x, positiveValue, fromIndex, p); + writeValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(x, Double.NaN, fromIndex, a); + writeValue(data, Double.MAX_VALUE, fromIndex, 1); + fromIndex += 1; + + writeValue(data, Double.POSITIVE_INFINITY, fromIndex, 1); + fromIndex += 1; + + writeValue(data, Double.NaN, fromIndex, a); } }; 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 3357aa959ee64..dfef1cec786a6 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) 2021, 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 @@ -31,9 +31,9 @@ * * @author Vladimir Yaroslavskiy * - * @version 2019.09.19 + * @version 2020.06.14 * - * @since 14 + * @since 14 & 17 */ public enum SortingHelper { @@ -80,16 +80,6 @@ public void sort(Object a, int low, int high) { fail(a); } } - - @Override - public void sort(Object[] a) { - fail(a); - } - - @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); - } }, PARALLEL_SORT("Parallel sort") { @@ -135,16 +125,6 @@ public void sort(Object a, int low, int high) { fail(a); } } - - @Override - public void sort(Object[] a) { - fail(a); - } - - @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); - } }, HEAP_SORT("Heap sort") { @@ -190,15 +170,50 @@ public void sort(Object a, int low, int high) { fail(a); } } + }, + + RADIX_SORT("Radix sort") { @Override - public void sort(Object[] a) { - fail(a); + public void sort(Object a) { + if (a instanceof int[]) { + DualPivotQuicksort.radixSort(null, (int[]) a, 0, ((int[]) a).length); + } else if (a instanceof long[]) { + DualPivotQuicksort.radixSort(null, (long[]) a, 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, 0, ((char[]) a).length); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, 0, ((short[]) a).length); + } else if (a instanceof float[]) { + DualPivotQuicksort.radixSort(null, (float[]) a, 0, ((float[]) a).length); + } else if (a instanceof double[]) { + DualPivotQuicksort.radixSort(null, (double[]) a, 0, ((double[]) a).length); + } else { + fail(a); + } } @Override - public void sort(Object[] a, Comparator comparator) { - fail(a); + public void sort(Object a, int low, int high) { + if (a instanceof int[]) { + DualPivotQuicksort.radixSort(null, (int[]) a, low, high); + } else if (a instanceof long[]) { + DualPivotQuicksort.radixSort(null, (long[]) a, low, high); + } else if (a instanceof byte[]) { + DualPivotQuicksort.sort((byte[]) a, low, high); + } else if (a instanceof char[]) { + DualPivotQuicksort.sort((char[]) a, 0, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, 0, low, high); + } else if (a instanceof float[]) { + DualPivotQuicksort.radixSort(null, (float[]) a, low, high); + } else if (a instanceof double[]) { + DualPivotQuicksort.radixSort(null, (double[]) a, low, high); + } else { + fail(a); + } } }, @@ -319,9 +334,13 @@ public void sort(Object[] a, Comparator comparator) { abstract public void sort(Object a, int low, int high); - abstract public void sort(Object[] a); + public void sort(Object[] a) { + fail(a); + } - abstract public void sort(Object[] a, Comparator comparator); + public void sort(Object[] a, Comparator comparator) { + fail(a); + } private SortingHelper(String name) { this.name = name; @@ -339,10 +358,14 @@ private static void fail(Object a) { private 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; + + /** + * Parallelism level for parallel sorting. + */ + private static final int PARALLEL = 88; /** * Heap sort will be invoked, if recursion depth is too big. From 6afd60a25bbe7a74c09b56673ef722631ed81f90 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Mon, 17 May 2021 23:15:41 +0300 Subject: [PATCH 02/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Sorting: - move radix sort out from quicksort partitioning - rename radixSort to tryRadixSort - minor javadoc and comment changes Testing: - rename radixSort to tryRadixSort in helper --- .../classes/java/util/DualPivotQuicksort.java | 218 ++++++++++-------- .../java.base/java/util/SortingHelper.java | 16 +- 2 files changed, 127 insertions(+), 107 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 89b6d46e03898..c40e906bb9661 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -260,6 +260,19 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } + /* + * Tries radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Insert the last element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -279,18 +292,10 @@ && tryMergeRuns(sorter, a, low, size)) { int upper = end; // The index of the first element of the right part /* - * Partitioning with two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Invoke radix sort on large array. - */ - if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE - && radixSort(sorter, a, low, high)) { - return; - } - /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -496,10 +501,10 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { for (int i, p = high; ++low < end; ) { int ai = a[i = low]; - if (ai < a[i - 1]) { // Small element + if (ai < a[i - 1]) { // Element smaller than pin /* - * Insert small element into sorted part. + * Insert this element into sorted part. */ a[i] = a[--i]; @@ -508,7 +513,7 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { } a[i + 1] = ai; - } else if (p > i && ai > pin) { // Large element + } else if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -524,7 +529,7 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { } /* - * Insert small element into sorted part. + * Insert smaller element into sorted part. */ while (ai < a[--i]) { a[i + 1] = a[i]; @@ -637,18 +642,18 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { } /** - * Sorts the specified range of the array using radix sort. + * Tries to sort the specified range of the array using 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 true if finally sorted, false otherwise */ - static boolean radixSort(Sorter sorter, int[] a, int low, int high) { - int[] b; int offset = low; + static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { + int[] b; int offset = low, size = high - low; if (sorter == null || (b = (int[]) sorter.b) == null) { - b = (int[]) tryAllocate(a, high - low); + b = (int[]) tryAllocate(a, size); if (b == null) { return false; @@ -672,10 +677,10 @@ static boolean radixSort(Sorter sorter, int[] a, int low, int high) { count4[(a[i] >>> 24) ^ 0x80]--; } - boolean passLevel4 = passLevel(count4, 255, low - high, high); - boolean passLevel3 = passLevel(count3, 255, low - high, high); - boolean passLevel2 = passLevel(count2, 255, low - high, high); - boolean passLevel1 = passLevel(count1, 255, low - high, high); + boolean passLevel1 = passLevel(count1, 255, -size, high); + boolean passLevel2 = passLevel(count2, 255, -size, high); + boolean passLevel3 = passLevel(count3, 255, -size, high); + boolean passLevel4 = passLevel(count4, 255, -size, high); if (passLevel1) { for (int i = low; i < high; ++i) { @@ -720,7 +725,7 @@ static boolean radixSort(Sorter sorter, int[] a, int low, int high) { } if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { - System.arraycopy(b, low - offset, a, low, high - low); + System.arraycopy(b, low - offset, a, low, size); } return true; } @@ -729,24 +734,24 @@ static boolean radixSort(Sorter sorter, int[] a, int low, int high) { * Scans count array and creates histogram. * * @param count the count array - * @param last the index of the last count - * @param size the array size + * @param last the last index of count + * @param total the total number of elements * @param high the index of the last element, exclusive * @return false if the level can be skipped, true otherwise */ - private static boolean passLevel(int[] count, int last, int size, int high) { + private static boolean passLevel(int[] count, int last, int total, int high) { for (int c : count) { if (c == 0) { continue; } - if (c == size) { // All elements are equal + if (c == total) { // All elements are equal return false; } break; } /* - * Compute histogram. + * Compute the histogram. */ count[last] += high; @@ -1149,6 +1154,19 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } + /* + * Tries radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Insert the last element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -1168,18 +1186,10 @@ && tryMergeRuns(sorter, a, low, size)) { int upper = end; // The index of the first element of the right part /* - * Partitioning with two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Invoke radix sort on large array. - */ - if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE - && radixSort(sorter, a, low, high)) { - return; - } - /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -1385,10 +1395,10 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { for (int i, p = high; ++low < end; ) { long ai = a[i = low]; - if (ai < a[i - 1]) { // Small element + if (ai < a[i - 1]) { // Element smaller than pin /* - * Insert small element into sorted part. + * Insert this element into sorted part. */ a[i] = a[--i]; @@ -1397,7 +1407,7 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { } a[i + 1] = ai; - } else if (p > i && ai > pin) { // Large element + } else if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -1413,7 +1423,7 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { } /* - * Insert small element into sorted part. + * Insert smaller element into sorted part. */ while (ai < a[--i]) { a[i + 1] = a[i]; @@ -1526,18 +1536,18 @@ private static void pushDown(long[] a, int p, long value, int low, int high) { } /** - * Sorts the specified range of the array using radix sort. + * Tries to sort the specified range of the array using 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 true if finally sorted, false otherwise */ - static boolean radixSort(Sorter sorter, long[] a, int low, int high) { - long[] b; int offset = low; + static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { + long[] b; int offset = low, size = high - low; if (sorter == null || (b = (long[]) sorter.b) == null) { - b = (long[]) tryAllocate(a, high - low); + b = (long[]) tryAllocate(a, size); if (b == null) { return false; @@ -1565,12 +1575,12 @@ static boolean radixSort(Sorter sorter, long[] a, int low, int high) { count6[(int) ((a[i] >>> 54) ^ 0x200)]--; } - boolean passLevel6 = passLevel(count6, 1023, low - high, high); - boolean passLevel5 = passLevel(count5, 2047, low - high, high); - boolean passLevel4 = passLevel(count4, 2047, low - high, high); - boolean passLevel3 = passLevel(count3, 2047, low - high, high); - boolean passLevel2 = passLevel(count2, 2047, low - high, high); - boolean passLevel1 = passLevel(count1, 1023, low - high, high); + boolean passLevel1 = passLevel(count1, 1023, -size, high); + boolean passLevel2 = passLevel(count2, 2047, -size, high); + boolean passLevel3 = passLevel(count3, 2047, -size, high); + boolean passLevel4 = passLevel(count4, 2047, -size, high); + boolean passLevel5 = passLevel(count5, 2047, -size, high); + boolean passLevel6 = passLevel(count6, 1023, -size, high); if (passLevel1) { for (int i = low; i < high; ++i) { @@ -1639,7 +1649,7 @@ static boolean radixSort(Sorter sorter, long[] a, int low, int high) { } if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { - System.arraycopy(b, low - offset, a, low, high - low); + System.arraycopy(b, low - offset, a, low, size); } return true; } @@ -2115,7 +2125,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 two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2434,7 +2444,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 two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { @@ -2824,6 +2834,19 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } + /* + * Tries radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Insert the last element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2843,18 +2866,10 @@ && tryMergeRuns(sorter, a, low, size)) { int upper = end; // The index of the first element of the right part /* - * Partitioning with two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Invoke radix sort on large array. - */ - if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE - && radixSort(sorter, a, low, high)) { - return; - } - /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -3060,10 +3075,10 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { for (int i, p = high; ++low < end; ) { float ai = a[i = low]; - if (ai < a[i - 1]) { // Small element + if (ai < a[i - 1]) { // Element smaller than pin /* - * Insert small element into sorted part. + * Insert this element into sorted part. */ a[i] = a[--i]; @@ -3072,7 +3087,7 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { } a[i + 1] = ai; - } else if (p > i && ai > pin) { // Large element + } else if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -3088,7 +3103,7 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { } /* - * Insert small element into sorted part. + * Insert smaller element into sorted part. */ while (ai < a[--i]) { a[i + 1] = a[i]; @@ -3201,18 +3216,18 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { } /** - * Sorts the specified range of the array using radix sort. + * Tries to sort the specified range of the array using 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 true if finally sorted, false otherwise */ - static boolean radixSort(Sorter sorter, float[] a, int low, int high) { - float[] b; int offset = low; + static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { + float[] b; int offset = low, size = high - low; if (sorter == null || (b = (float[]) sorter.b) == null) { - b = (float[]) tryAllocate(a, high - low); + b = (float[]) tryAllocate(a, size); if (b == null) { return false; @@ -3236,10 +3251,10 @@ static boolean radixSort(Sorter sorter, float[] a, int low, int high) { count4[(fti(a[i]) >>> 24) & 0xFF]--; } - boolean passLevel4 = passLevel(count4, 255, low - high, high); - boolean passLevel3 = passLevel(count3, 255, low - high, high); - boolean passLevel2 = passLevel(count2, 255, low - high, high); - boolean passLevel1 = passLevel(count1, 255, low - high, high); + boolean passLevel1 = passLevel(count1, 255, -size, high); + boolean passLevel2 = passLevel(count2, 255, -size, high); + boolean passLevel3 = passLevel(count3, 255, -size, high); + boolean passLevel4 = passLevel(count4, 255, -size, high); if (passLevel1) { for (int i = low; i < high; ++i) { @@ -3284,7 +3299,7 @@ static boolean radixSort(Sorter sorter, float[] a, int low, int high) { } if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { - System.arraycopy(b, low - offset, a, low, high - low); + System.arraycopy(b, low - offset, a, low, size); } return true; } @@ -3745,6 +3760,19 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } + /* + * Tries radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + + /* + * Insert the last element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -3764,18 +3792,10 @@ && tryMergeRuns(sorter, a, low, size)) { int upper = end; // The index of the first element of the right part /* - * Partitioning with two pivots in case of different elements. + * Partitioning with two pivots on array of random elements. */ if (a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]) { - /* - * Invoke radix sort on large array. - */ - if ((bits > MIN_RADIX_SORT_DEPTH || sorter == null) && size > MIN_RADIX_SORT_SIZE - && radixSort(sorter, a, low, high)) { - return; - } - /* * Use the first and fifth of the five sorted elements as * the pivots. These values are inexpensive approximation @@ -3981,10 +4001,10 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { for (int i, p = high; ++low < end; ) { double ai = a[i = low]; - if (ai < a[i - 1]) { // Small element + if (ai < a[i - 1]) { // Element smaller than pin /* - * Insert small element into sorted part. + * Insert this element into sorted part. */ a[i] = a[--i]; @@ -3993,7 +4013,7 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { } a[i + 1] = ai; - } else if (p > i && ai > pin) { // Large element + } else if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -4009,7 +4029,7 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { } /* - * Insert small element into sorted part. + * Insert smaller element into sorted part. */ while (ai < a[--i]) { a[i + 1] = a[i]; @@ -4122,18 +4142,18 @@ private static void pushDown(double[] a, int p, double value, int low, int high) } /** - * Sorts the specified range of the array using radix sort. + * Tries to sort the specified range of the array using 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 true if finally sorted, false otherwise */ - static boolean radixSort(Sorter sorter, double[] a, int low, int high) { - double[] b; int offset = low; + static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { + double[] b; int offset = low, size = high - low; if (sorter == null || (b = (double[]) sorter.b) == null) { - b = (double[]) tryAllocate(a, high - low); + b = (double[]) tryAllocate(a, size); if (b == null) { return false; @@ -4161,12 +4181,12 @@ static boolean radixSort(Sorter sorter, double[] a, int low, int high) { count6[(int) ((dtl(a[i]) >>> 54) & 0x3FF)]--; } - boolean passLevel6 = passLevel(count6, 1023, low - high, high); - boolean passLevel5 = passLevel(count5, 2047, low - high, high); - boolean passLevel4 = passLevel(count4, 2047, low - high, high); - boolean passLevel3 = passLevel(count3, 2047, low - high, high); - boolean passLevel2 = passLevel(count2, 2047, low - high, high); - boolean passLevel1 = passLevel(count1, 1023, low - high, high); + boolean passLevel1 = passLevel(count1, 1023, -size, high); + boolean passLevel2 = passLevel(count2, 2047, -size, high); + boolean passLevel3 = passLevel(count3, 2047, -size, high); + boolean passLevel4 = passLevel(count4, 2047, -size, high); + boolean passLevel5 = passLevel(count5, 2047, -size, high); + boolean passLevel6 = passLevel(count6, 1023, -size, high); if (passLevel1) { for (int i = low; i < high; ++i) { @@ -4235,7 +4255,7 @@ static boolean radixSort(Sorter sorter, double[] a, int low, int high) { } if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { - System.arraycopy(b, low - offset, a, low, high - low); + System.arraycopy(b, low - offset, a, low, size); } return true; } 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 dfef1cec786a6..54b7c7a74ec24 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 @@ -177,9 +177,9 @@ public void sort(Object a, int low, int high) { @Override public void sort(Object a) { if (a instanceof int[]) { - DualPivotQuicksort.radixSort(null, (int[]) a, 0, ((int[]) a).length); + DualPivotQuicksort.tryRadixSort(null, (int[]) a, 0, ((int[]) a).length); } else if (a instanceof long[]) { - DualPivotQuicksort.radixSort(null, (long[]) a, 0, ((long[]) a).length); + DualPivotQuicksort.tryRadixSort(null, (long[]) a, 0, ((long[]) a).length); } else if (a instanceof byte[]) { DualPivotQuicksort.sort((byte[]) a, 0, ((byte[]) a).length); } else if (a instanceof char[]) { @@ -187,9 +187,9 @@ public void sort(Object a) { } else if (a instanceof short[]) { DualPivotQuicksort.sort((short[]) a, 0, ((short[]) a).length); } else if (a instanceof float[]) { - DualPivotQuicksort.radixSort(null, (float[]) a, 0, ((float[]) a).length); + DualPivotQuicksort.tryRadixSort(null, (float[]) a, 0, ((float[]) a).length); } else if (a instanceof double[]) { - DualPivotQuicksort.radixSort(null, (double[]) a, 0, ((double[]) a).length); + DualPivotQuicksort.tryRadixSort(null, (double[]) a, 0, ((double[]) a).length); } else { fail(a); } @@ -198,9 +198,9 @@ public void sort(Object a) { @Override public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.radixSort(null, (int[]) a, low, high); + DualPivotQuicksort.tryRadixSort(null, (int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.radixSort(null, (long[]) a, low, high); + DualPivotQuicksort.tryRadixSort(null, (long[]) a, low, high); } else if (a instanceof byte[]) { DualPivotQuicksort.sort((byte[]) a, low, high); } else if (a instanceof char[]) { @@ -208,9 +208,9 @@ public void sort(Object a, int low, int high) { } else if (a instanceof short[]) { DualPivotQuicksort.sort((short[]) a, 0, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.radixSort(null, (float[]) a, low, high); + DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.radixSort(null, (double[]) a, low, high); + DualPivotQuicksort.tryRadixSort(null, (double[]) a, low, high); } else { fail(a); } From 2d9728870c4708a8900027b88358dec2c03faab1 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 18 May 2021 18:26:07 +0300 Subject: [PATCH 03/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Testing: - remove @since and @date, otherwise jtreg tag parser fails --- test/jdk/java/util/Arrays/Sorting.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index ca5875d07980f..d0ecc5adc2ad2 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -32,10 +32,6 @@ * @author Vladimir Yaroslavskiy * @author Jon Bentley * @author Josh Bloch - * - * @version 2020.06.14 - * - * @since 1.7 * 14 & 17 */ import java.io.PrintStream; From 189f547aa10a412598a22ee3d2da541c32de7e3c Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 14 Sep 2021 22:13:55 +0300 Subject: [PATCH 04/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Update target version --- src/java.base/share/classes/java/util/DualPivotQuicksort.java | 2 +- .../java/util/Arrays/java.base/java/util/SortingHelper.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index c40e906bb9661..396b28ee8582d 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -46,7 +46,7 @@ * * @version 2020.06.14 * - * @since 1.7 * 14 & 17 + * @since 1.7 * 14 & 18 */ final class DualPivotQuicksort { 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 54b7c7a74ec24..d58da59168a84 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) 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2021, 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 @@ -33,7 +33,7 @@ * * @version 2020.06.14 * - * @since 14 & 17 + * @since 14 & 18 */ public enum SortingHelper { From adcc6942be90f5cf2d70da9e1f417f200e388852 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Mon, 27 Sep 2021 19:10:49 +0300 Subject: [PATCH 05/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Simplified mixed insertion sort --- .../classes/java/util/DualPivotQuicksort.java | 260 ++++++++---------- 1 file changed, 108 insertions(+), 152 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 396b28ee8582d..82b8516e74019 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -260,16 +260,6 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } - /* - * Tries radix sort on large random data. - */ - if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] - && tryRadixSort(sorter, a, low, high)) { - return; - } - /* * Insert the last element. */ @@ -287,6 +277,16 @@ && tryRadixSort(sorter, a, low, high)) { } } + /* + * Try radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -477,7 +477,7 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { /* * Invoke simple insertion sort on tiny array. */ - for (int i; ++low < end; ) { + for (int i; ++low < high; ) { int ai = a[i = low]; while (ai < a[--i]) { @@ -485,35 +485,24 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { } a[i + 1] = ai; } + } else { /* * 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. */ int pin = a[end]; for (int i, p = high; ++low < end; ) { int ai = a[i = low]; - if (ai < a[i - 1]) { // Element smaller than pin - - /* - * Insert this 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) { // Element larger than pin + /* + * Put elements larger than an element called pin + * to the end of array (the proper area for them). + * It avoids expensive movements of these elements + * through the whole array. + */ + if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -521,21 +510,21 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { while (a[--p] > pin); /* - * Swap it with large element. + * Swap it with larger element. */ if (p > i) { ai = a[p]; a[p] = a[i]; } + } - /* - * Insert smaller 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]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; } /* @@ -1154,16 +1143,6 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } - /* - * Tries radix sort on large random data. - */ - if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] - && tryRadixSort(sorter, a, low, high)) { - return; - } - /* * Insert the last element. */ @@ -1181,6 +1160,16 @@ && tryRadixSort(sorter, a, low, high)) { } } + /* + * Try radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -1371,7 +1360,7 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { /* * Invoke simple insertion sort on tiny array. */ - for (int i; ++low < end; ) { + for (int i; ++low < high; ) { long ai = a[i = low]; while (ai < a[--i]) { @@ -1379,35 +1368,24 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { } a[i + 1] = ai; } + } else { /* * 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. */ long pin = a[end]; for (int i, p = high; ++low < end; ) { long ai = a[i = low]; - if (ai < a[i - 1]) { // Element smaller than pin - - /* - * Insert this 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) { // Element larger than pin + /* + * Put elements larger than an element called pin + * to the end of array (the proper area for them). + * It avoids expensive movements of these elements + * through the whole array. + */ + if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -1415,21 +1393,21 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { while (a[--p] > pin); /* - * Swap it with large element. + * Swap it with larger element. */ if (p > i) { ai = a[p]; a[p] = a[i]; } + } - /* - * Insert smaller 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]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; } /* @@ -2834,16 +2812,6 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } - /* - * Tries radix sort on large random data. - */ - if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] - && tryRadixSort(sorter, a, low, high)) { - return; - } - /* * Insert the last element. */ @@ -2861,6 +2829,16 @@ && tryRadixSort(sorter, a, low, high)) { } } + /* + * Try radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -3051,7 +3029,7 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { /* * Invoke simple insertion sort on tiny array. */ - for (int i; ++low < end; ) { + for (int i; ++low < high; ) { float ai = a[i = low]; while (ai < a[--i]) { @@ -3059,35 +3037,24 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { } a[i + 1] = ai; } + } else { /* * 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. */ float pin = a[end]; for (int i, p = high; ++low < end; ) { float ai = a[i = low]; - if (ai < a[i - 1]) { // Element smaller than pin - - /* - * Insert this 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) { // Element larger than pin + /* + * Put elements larger than an element called pin + * to the end of array (the proper area for them). + * It avoids expensive movements of these elements + * through the whole array. + */ + if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -3095,21 +3062,21 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { while (a[--p] > pin); /* - * Swap it with large element. + * Swap it with larger element. */ if (p > i) { ai = a[p]; a[p] = a[i]; } + } - /* - * Insert smaller 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]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; } /* @@ -3760,16 +3727,6 @@ && tryMergeRuns(sorter, a, low, size)) { 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; } - /* - * Tries radix sort on large random data. - */ - if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] - && tryRadixSort(sorter, a, low, high)) { - return; - } - /* * Insert the last element. */ @@ -3787,6 +3744,16 @@ && tryRadixSort(sorter, a, low, high)) { } } + /* + * Try radix sort on large random data. + */ + if (size > MIN_RADIX_SORT_SIZE + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && tryRadixSort(sorter, a, low, high)) { + return; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -3977,7 +3944,7 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { /* * Invoke simple insertion sort on tiny array. */ - for (int i; ++low < end; ) { + for (int i; ++low < high; ) { double ai = a[i = low]; while (ai < a[--i]) { @@ -3985,35 +3952,24 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { } a[i + 1] = ai; } + } else { /* * 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. */ double pin = a[end]; for (int i, p = high; ++low < end; ) { double ai = a[i = low]; - if (ai < a[i - 1]) { // Element smaller than pin - - /* - * Insert this 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) { // Element larger than pin + /* + * Put elements larger than an element called pin + * to the end of array (the proper area for them). + * It avoids expensive movements of these elements + * through the whole array. + */ + if (p > i && ai > pin) { // Element larger than pin /* * Find element smaller than pin. @@ -4021,21 +3977,21 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { while (a[--p] > pin); /* - * Swap it with large element. + * Swap it with larger element. */ if (p > i) { ai = a[p]; a[p] = a[i]; } + } - /* - * Insert smaller 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]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; } /* From 90c08aed7f7261d56324c1da78548256aebc41af Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 5 Oct 2021 16:15:02 +0300 Subject: [PATCH 06/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Better naming of methods --- test/jdk/java/util/Arrays/Sorting.java | 294 ++++++++++++------------- 1 file changed, 147 insertions(+), 147 deletions(-) diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index d0ecc5adc2ad2..844444aa126c9 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -310,7 +310,7 @@ private void testSubArray(int length, TestRandom random) { prepareSubArray((int[]) gold[0], fromIndex, toIndex); convertData(length); - for (int i = 0; i < test.length; i++) { + for (int i = 0; i < test.length; ++i) { printTestName("Test subarray", random, length, ", m = " + m + ", " + getType(i)); sortingHelper.sort(test[i], fromIndex, toIndex); @@ -325,12 +325,12 @@ private void testOutOfBounds(int length, TestRandom random) { return; } for (int m = 1; m < length; m <<= 1) { - for (int i = 1; i <= length; 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++) { + for (int i = 0; i < test.length; ++i) { printTestName("Test range check", random, length, ", m = " + m + ", " + getType(i)); checkOutOfBounds(test[i], m); @@ -340,7 +340,7 @@ private void testOutOfBounds(int length, TestRandom random) { } private void checkSorted(Pair[] a) { - for (int i = 0; i < a.length - 1; i++) { + 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()); @@ -393,7 +393,7 @@ 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); sortingHelper.sort(test[i]); @@ -411,12 +411,12 @@ private void testMergingSort(int length, TestRandom random) { } final int PERIOD = 50; - for (int m = PERIOD - 2; m <= PERIOD + 2; m++) { + for (int m = PERIOD - 2; m <= PERIOD + 2; ++m) { for (MergingBuilder builder : MergingBuilder.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 merging sort", random, length, ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); @@ -433,7 +433,7 @@ private void testWithCheckSum(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 check sum", random, length, ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); @@ -450,7 +450,7 @@ private void testWithScrambling(int length, TestRandom random) { 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); scramble(test[i], random); @@ -463,7 +463,7 @@ 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]; @@ -482,15 +482,15 @@ private void testFloatingPointSorting(int length, TestRandom random) { 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++) { + 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)); @@ -506,12 +506,12 @@ private void testFloatingPointSorting(int length, TestRandom random) { } } } - for (int m = MAX; m > 4; m--) { + for (int m = MAX; m > 4; --m) { int t = length / m; int g = t, z = t, n = t, p = t; int a = length - g - z - n - p - s; - 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)); @@ -527,21 +527,21 @@ private void testFloatingPointSorting(int length, TestRandom random) { } 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; } } @@ -567,43 +567,43 @@ private void scramble(Object a, Random random) { } 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)); } } @@ -657,7 +657,7 @@ private void checkNegativeZero(Object 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); } @@ -665,7 +665,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); } @@ -683,20 +683,20 @@ private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) } 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); } @@ -704,20 +704,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); } @@ -745,7 +745,7 @@ private void compare(Object a, Object b) { } 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); } @@ -753,7 +753,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); } @@ -761,7 +761,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); } @@ -769,7 +769,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); } @@ -777,7 +777,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); } @@ -785,7 +785,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); } @@ -793,7 +793,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); } @@ -849,7 +849,7 @@ private void checkSorted(Object 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]); } @@ -857,7 +857,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]); } @@ -865,7 +865,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]); } @@ -873,7 +873,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]); } @@ -881,7 +881,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]); } @@ -889,7 +889,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]); } @@ -897,7 +897,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]); } @@ -1112,10 +1112,10 @@ private void sortByInsertionSort(Object a) { } private void sortByInsertionSort(int[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { int ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1123,10 +1123,10 @@ private void sortByInsertionSort(int[] a) { } private void sortByInsertionSort(long[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { long ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1134,10 +1134,10 @@ private void sortByInsertionSort(long[] a) { } private void sortByInsertionSort(byte[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { byte ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1145,10 +1145,10 @@ private void sortByInsertionSort(byte[] a) { } private void sortByInsertionSort(char[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { char ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1156,10 +1156,10 @@ private void sortByInsertionSort(char[] a) { } private void sortByInsertionSort(short[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { short ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1167,10 +1167,10 @@ private void sortByInsertionSort(short[] a) { } private void sortByInsertionSort(float[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { float ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1178,10 +1178,10 @@ private void sortByInsertionSort(float[] a) { } private void sortByInsertionSort(double[] a) { - for (int j, i = 1; i < a.length; i++) { + for (int j, i = 1; i < a.length; ++i) { double ai = a[i]; - for (j = i - 1; j >= 0 && ai < a[j]; j--) { + for (j = i - 1; j >= 0 && ai < a[j]; --j) { a[j + 1] = a[j]; } a[j + 1] = ai; @@ -1209,19 +1209,19 @@ private void checkSubArray(Object a, int fromIndex, int toIndex) { } 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)); } @@ -1229,19 +1229,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)); } @@ -1249,19 +1249,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)); } @@ -1269,19 +1269,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)); } @@ -1289,19 +1289,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)); } @@ -1309,19 +1309,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)); } @@ -1329,19 +1329,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)); } @@ -1545,12 +1545,12 @@ private void createData(int length) { } private void convertData(int length) { - for (int i = 1; i < gold.length; i++) { + 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++) { + for (int i = 0; i < gold.length; ++i) { System.arraycopy(gold[i], 0, test[i], 0, length); } } @@ -1570,7 +1570,7 @@ private static enum TypeConverter { void convert(int[] src, Object dst) { long[] b = (long[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (long) src[i]; } } @@ -1580,7 +1580,7 @@ void convert(int[] src, Object dst) { void convert(int[] src, Object dst) { 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]; } } @@ -1590,7 +1590,7 @@ void convert(int[] src, Object dst) { void convert(int[] src, Object dst) { 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]; } } @@ -1600,7 +1600,7 @@ void convert(int[] src, Object dst) { void convert(int[] src, Object dst) { 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]; } } @@ -1610,7 +1610,7 @@ void convert(int[] src, Object dst) { void convert(int[] src, Object dst) { 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]; } } @@ -1620,7 +1620,7 @@ void convert(int[] src, Object dst) { void convert(int[] src, Object dst) { double[] b = (double[]) dst; - for (int i = 0; i < src.length; i++) { + for (int i = 0; i < src.length; ++i) { b[i] = (double) src[i]; } } @@ -1632,11 +1632,11 @@ void convert(int[] src, Object dst) { private static enum SortedBuilder { STEPS { 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; } } @@ -1649,7 +1649,7 @@ private static enum UnsortedBuilder { RANDOM { 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(); } } @@ -1662,7 +1662,7 @@ void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { a[i] = i & mask; } - for (int i = a.length; i > 1; i--) { + 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; } @@ -1671,7 +1671,7 @@ void build(int[] a, int m, Random random) { ASCENDING { 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] = m + i; } } @@ -1679,7 +1679,7 @@ void build(int[] a, int m, Random random) { DESCENDING { 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] = a.length - m - i; } } @@ -1687,7 +1687,7 @@ void build(int[] a, int m, Random random) { EQUAL { 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] = m; } } @@ -1721,7 +1721,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; } @@ -1729,7 +1729,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; } @@ -1742,7 +1742,7 @@ void build(int[] a, int m, Random random) { REPEATED { 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] = i % m; } } @@ -1750,7 +1750,7 @@ void build(int[] a, int m, Random random) { DUPLICATED { 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(m); } } @@ -1760,11 +1760,11 @@ void build(int[] a, int m, Random random) { void build(int[] a, int m, Random random) { 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; } } @@ -1772,7 +1772,7 @@ void build(int[] a, int m, Random random) { STAGGER { 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] = (i * m + i) % a.length; } } @@ -1780,7 +1780,7 @@ void build(int[] a, int m, Random random) { PLATEAU { 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] = Math.min(i, m); } } @@ -1790,7 +1790,7 @@ void build(int[] a, int m, Random random) { void build(int[] a, int m, Random random) { int k = 0, j = 0; - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = random.nextBoolean() ? (k += 2) : (j += 2); } } @@ -1801,7 +1801,7 @@ void build(int[] a, int m, Random random) { int max = a.length / m; max = max < 2 ? 2 : max; - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = i % max; } } @@ -1817,15 +1817,15 @@ void build(int[] a, int m) { int period = a.length / m; int v = 1, i = 0; - for (int k = 0; k < m; k++) { + for (int k = 0; k < m; ++k) { v = 1; - for (int p = 0; p < period; p++) { + for (int p = 0; p < period; ++p) { a[i++] = v++; } } - for (int j = i; j < a.length - 1; j++) { + for (int j = i; j < a.length - 1; ++j) { a[j] = v++; } @@ -1838,15 +1838,15 @@ void build(int[] a, int m) { int period = a.length / m; int v = -1, i = 0; - for (int k = 0; k < m; k++) { + for (int k = 0; k < m; ++k) { v = -1; - for (int p = 0; p < period; p++) { + for (int p = 0; p < period; ++p) { a[i++] = v--; } } - for (int j = i; j < a.length - 1; j++) { + for (int j = i; j < a.length - 1; ++j) { a[j] = v--; } @@ -1856,7 +1856,7 @@ void build(int[] a, int m) { POINT { void build(int[] a, int m) { - for (int i = 0; i < a.length; i++) { + for (int i = 0; i < a.length; ++i) { a[i] = 0; } a[a.length / 2] = m; @@ -1865,7 +1865,7 @@ void build(int[] a, int m) { LINE { 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); @@ -1874,7 +1874,7 @@ void build(int[] a, int m) { PEARL { 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); @@ -1887,15 +1887,15 @@ void build(int[] a, int m) { 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--; } } @@ -1918,7 +1918,7 @@ private static enum NegativeZeroBuilder { 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; } } @@ -1928,7 +1928,7 @@ void build(Object o, Random random) { 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; } } @@ -1946,31 +1946,31 @@ void build(Object o, int a, int g, int z, int n, int p, Random random) { float[] data = (float[]) o; int fromIndex = 0; - writeValue(data, Float.NEGATIVE_INFINITY, fromIndex, 1); + fillWithValue(data, Float.NEGATIVE_INFINITY, fromIndex, 1); fromIndex += 1; - writeValue(data, -Float.MAX_VALUE, fromIndex, 1); + fillWithValue(data, -Float.MAX_VALUE, fromIndex, 1); fromIndex += 1; - writeValue(data, negativeValue, fromIndex, n); + fillWithValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(data, -0.0f, fromIndex, g); + fillWithValue(data, -0.0f, fromIndex, g); fromIndex += g; - writeValue(data, 0.0f, fromIndex, z); + fillWithValue(data, 0.0f, fromIndex, z); fromIndex += z; - writeValue(data, positiveValue, fromIndex, p); + fillWithValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(data, Float.MAX_VALUE, fromIndex, 1); + fillWithValue(data, Float.MAX_VALUE, fromIndex, 1); fromIndex += 1; - writeValue(data, Float.POSITIVE_INFINITY, fromIndex, 1); + fillWithValue(data, Float.POSITIVE_INFINITY, fromIndex, 1); fromIndex += 1; - writeValue(data, Float.NaN, fromIndex, a); + fillWithValue(data, Float.NaN, fromIndex, a); } }, @@ -1981,44 +1981,44 @@ void build(Object o, int a, int g, int z, int n, int p, Random random) { double[] data = (double[]) o; int fromIndex = 0; - writeValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); + fillWithValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); fromIndex++; - writeValue(data, -Double.MAX_VALUE, fromIndex, 1); + fillWithValue(data, -Double.MAX_VALUE, fromIndex, 1); fromIndex++; - writeValue(data, negativeValue, fromIndex, n); + fillWithValue(data, negativeValue, fromIndex, n); fromIndex += n; - writeValue(data, -0.0d, fromIndex, g); + fillWithValue(data, -0.0d, fromIndex, g); fromIndex += g; - writeValue(data, 0.0d, fromIndex, z); + fillWithValue(data, 0.0d, fromIndex, z); fromIndex += z; - writeValue(data, positiveValue, fromIndex, p); + fillWithValue(data, positiveValue, fromIndex, p); fromIndex += p; - writeValue(data, Double.MAX_VALUE, fromIndex, 1); + fillWithValue(data, Double.MAX_VALUE, fromIndex, 1); fromIndex += 1; - writeValue(data, Double.POSITIVE_INFINITY, fromIndex, 1); + fillWithValue(data, Double.POSITIVE_INFINITY, fromIndex, 1); fromIndex += 1; - writeValue(data, Double.NaN, fromIndex, a); + 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; } } From 9989de5ba1911c1c24a37aee0643c2edb798631a Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Thu, 7 Oct 2021 00:17:04 +0300 Subject: [PATCH 07/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Added more comments --- .../classes/java/util/DualPivotQuicksort.java | 762 ++++++++++-------- 1 file changed, 442 insertions(+), 320 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 82b8516e74019..a631a10799d6d 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -36,8 +36,8 @@ * 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, radix sort, counting sort and parallel merge sort. + * Quicksort such as mixed insertion sort, merging sort and counting + * sort, heap sort and LSD Radix sort, parallel merge sort. * * @author Vladimir Yaroslavskiy * @author Jon Bentley @@ -71,9 +71,9 @@ private DualPivotQuicksort() {} private static final int MIN_PARALLEL_SORT_SIZE = 4 << 10; /** - * 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 = 4 << 10; /** * Min size of the first run to continue with scanning. @@ -111,7 +111,7 @@ private DualPivotQuicksort() {} private static final int MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE = 1750; /** - * Min array size to use radix sort. + * Min array size to use Radix sort. */ private static final int MIN_RADIX_SORT_SIZE = 6 << 10; @@ -121,7 +121,7 @@ private DualPivotQuicksort() {} private static final int DEPTH = 3 << 1; /** - * Min depth to invoke radix sort. + * Min depth to invoke Radix sort. */ private static final int MIN_RADIX_SORT_DEPTH = DEPTH << 2; @@ -209,8 +209,8 @@ static void sort(Sorter sorter, int[] a, int bits, int low, int high) { * 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)) { + if ((bits == 0 || size > MIN_MERGING_SORT_SIZE && (bits & 1) > 0) + && tryMergingSort(sorter, a, low, size)) { return; } @@ -261,7 +261,7 @@ && tryMergeRuns(sorter, a, low, size)) { if (a[e4] < a[e2]) { int t = a[e4]; a[e4] = a[e2]; a[e2] = t; } /* - * Insert the last element. + * Insert the third element. */ if (a3 < a[e2]) { if (a3 < a[e1]) { @@ -278,7 +278,7 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Try radix sort on large random data. + * Try Radix sort on large random data. */ if (size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) @@ -379,7 +379,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -502,10 +502,10 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { * It avoids expensive movements of these elements * through the whole array. */ - if (p > i && ai > pin) { // Element larger than pin + if (p > i && ai > pin) { // Element, larger than pin /* - * Find element smaller than pin. + * Find element, smaller than pin. */ while (a[--p] > pin); @@ -587,60 +587,20 @@ private static void insertionSort(int[] a, int low, int high) { } /** - * 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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - - /** - * Tries to sort the specified range of the array using radix sort. + * Tries to sort the specified range of the array + * using LSD (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 true if finally sorted, false otherwise + * @return true if the array is finally sorted, false otherwise */ 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 = (int[]) sorter.b) == null) { b = (int[]) tryAllocate(a, size); @@ -654,6 +614,9 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { int start = low - offset; int last = high - offset; + /* + * Count the number of all digits. + */ int[] count1 = new int[256]; int[] count2 = new int[256]; int[] count3 = new int[256]; @@ -663,22 +626,31 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { count1[ a[i] & 0xFF]--; count2[(a[i] >>> 8) & 0xFF]--; count3[(a[i] >>> 16) & 0xFF]--; - count4[(a[i] >>> 24) ^ 0x80]--; + count4[(a[i] >>> 24) ^ 0x80]--; // Reverse the sign bit } - boolean passLevel1 = passLevel(count1, 255, -size, high); - boolean passLevel2 = passLevel(count2, 255, -size, high); - boolean passLevel3 = passLevel(count3, 255, -size, high); - boolean passLevel4 = passLevel(count4, 255, -size, high); + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, 255, -size, high); + boolean processDigit2 = processDigit(count2, 255, -size, high); + boolean processDigit3 = processDigit(count3, 255, -size, high); + boolean processDigit4 = processDigit(count4, 255, -size, high); - if (passLevel1) { + /* + * Process the 1-st digit. + */ + if (processDigit1) { for (int i = low; i < high; ++i) { b[count1[a[i] & 0xFF]++ - offset] = a[i]; } } - if (passLevel2) { - if (passLevel1) { + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { for (int i = start; i < last; ++i) { a[count2[(b[i] >>> 8) & 0xFF]++] = b[i]; } @@ -689,8 +661,11 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { } } - if (passLevel3) { - if (passLevel1 ^ passLevel2) { + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { a[count3[(b[i] >>> 16) & 0xFF]++] = b[i]; } @@ -701,8 +676,11 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { } } - if (passLevel4) { - if (passLevel1 ^ passLevel2 ^ passLevel3) { + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { for (int i = start; i < last; ++i) { a[count4[(b[i] >>> 24) ^ 0x80]++] = b[i]; } @@ -713,27 +691,34 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { } } - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { System.arraycopy(b, low - offset, a, low, size); } return true; } /** - * Scans count array and creates histogram. + * Checks the count array and then creates histogram. * * @param count the count array - * @param last the last index of count + * @param last the last index of count array * @param total the total number of elements * @param high the index of the last element, exclusive - * @return false if the level can be skipped, true otherwise + * @return false if the digit can be skipped, true otherwise */ - private static boolean passLevel(int[] count, int last, int total, int high) { + private static boolean processDigit(int[] count, int last, int total, int high) { + + /* + * Check if we can skip given digit. + */ for (int c : count) { if (c == 0) { continue; } - if (c == total) { // All elements are equal + if (c == total) { return false; } break; @@ -751,15 +736,59 @@ private static boolean passLevel(int[] count, int last, int total, int high) { } /** - * Tries to sort the specified range of the array. + * 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 + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + + /** + * 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 + * @return true if the array is finally sorted, false otherwise */ - private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { + private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { /* * The run array is constructed only if initial runs are @@ -820,7 +849,7 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { return false; } - // min 127, max 1023, ext 5120 + // Min 127, max 1023, extend to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -852,7 +881,7 @@ private static boolean tryMergeRuns(Sorter sorter, int[] a, int low, int size) { if (++k == high) { /* - * There is a single-element run at the end. + * This is single-element run at the end. */ --k; } @@ -1028,7 +1057,7 @@ private static void mergeParts(Merger merger, int[] dst, int k, } } -// [long] +// #[long] /** * Sorts the specified range of the array using parallel merge @@ -1092,8 +1121,8 @@ static void sort(Sorter sorter, long[] a, int bits, int low, int high) { * 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)) { + if ((bits == 0 || size > MIN_MERGING_SORT_SIZE && (bits & 1) > 0) + && tryMergingSort(sorter, a, low, size)) { return; } @@ -1144,7 +1173,7 @@ && tryMergeRuns(sorter, a, low, size)) { if (a[e4] < a[e2]) { long t = a[e4]; a[e4] = a[e2]; a[e2] = t; } /* - * Insert the last element. + * Insert the third element. */ if (a3 < a[e2]) { if (a3 < a[e1]) { @@ -1161,7 +1190,7 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Try radix sort on large random data. + * Try Radix sort on large random data. */ if (size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) @@ -1262,7 +1291,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -1385,10 +1414,10 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { * It avoids expensive movements of these elements * through the whole array. */ - if (p > i && ai > pin) { // Element larger than pin + if (p > i && ai > pin) { // Element, larger than pin /* - * Find element smaller than pin. + * Find element, smaller than pin. */ while (a[--p] > pin); @@ -1470,60 +1499,20 @@ private static void insertionSort(long[] a, int low, int high) { } /** - * 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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - - /** - * Tries to sort the specified range of the array using radix sort. + * Tries to sort the specified range of the array + * using LSD (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 true if finally sorted, false otherwise + * @return true if the array is finally sorted, false otherwise */ 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 = (long[]) sorter.b) == null) { b = (long[]) tryAllocate(a, size); @@ -1537,6 +1526,9 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { 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]; @@ -1550,24 +1542,33 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { count3[(int) ((a[i] >>> 21) & 0x7FF)]--; count4[(int) ((a[i] >>> 32) & 0x7FF)]--; count5[(int) ((a[i] >>> 43) & 0x7FF)]--; - count6[(int) ((a[i] >>> 54) ^ 0x200)]--; + count6[(int) ((a[i] >>> 54) ^ 0x200)]--; // Reverse the sign bit } - boolean passLevel1 = passLevel(count1, 1023, -size, high); - boolean passLevel2 = passLevel(count2, 2047, -size, high); - boolean passLevel3 = passLevel(count3, 2047, -size, high); - boolean passLevel4 = passLevel(count4, 2047, -size, high); - boolean passLevel5 = passLevel(count5, 2047, -size, high); - boolean passLevel6 = passLevel(count6, 1023, -size, high); + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + boolean processDigit4 = processDigit(count4, 2047, -size, high); + boolean processDigit5 = processDigit(count5, 2047, -size, high); + boolean processDigit6 = processDigit(count6, 1023, -size, high); - if (passLevel1) { + /* + * Process the 1-st digit. + */ + if (processDigit1) { for (int i = low; i < high; ++i) { b[count1[(int) (a[i] & 0x3FF)]++ - offset] = a[i]; } } - if (passLevel2) { - if (passLevel1) { + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { for (int i = start; i < last; ++i) { a[count2[(int) ((b[i] >>> 10) & 0x7FF)]++] = b[i]; } @@ -1578,8 +1579,11 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { } } - if (passLevel3) { - if (passLevel1 ^ passLevel2) { + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { a[count3[(int) ((b[i] >>> 21) & 0x7FF)]++] = b[i]; } @@ -1590,8 +1594,11 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { } } - if (passLevel4) { - if (passLevel1 ^ passLevel2 ^ passLevel3) { + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { for (int i = start; i < last; ++i) { a[count4[(int) ((b[i] >>> 32) & 0x7FF)]++] = b[i]; } @@ -1602,8 +1609,11 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { } } - if (passLevel5) { - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + /* + * Process the 5-th digit. + */ + if (processDigit5) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { for (int i = start; i < last; ++i) { a[count5[(int) ((b[i] >>> 43) & 0x7FF)]++] = b[i]; } @@ -1614,8 +1624,11 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { } } - if (passLevel6) { - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5) { + /* + * Process the 6-th digit. + */ + if (processDigit6) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5) { for (int i = start; i < last; ++i) { a[count6[(int) ((b[i] >>> 54) ^ 0x200)]++] = b[i]; } @@ -1626,22 +1639,69 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { } } - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { + /* + * 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; } /** - * Tries to sort the specified range of the array. + * 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 + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + + /** + * 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 + * @return true if the array is finally sorted, false otherwise */ - private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) { + private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { /* * The run array is constructed only if initial runs are @@ -1702,7 +1762,7 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) return false; } - // min 127, max 1023, ext 5120 + // Min 127, max 1023, extend to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -1734,7 +1794,7 @@ private static boolean tryMergeRuns(Sorter sorter, long[] a, int low, int size) if (++k == high) { /* - * There is a single-element run at the end. + * This is single-element run at the end. */ --k; } @@ -1910,7 +1970,7 @@ private static void mergeParts(Merger merger, long[] dst, int k, } } -// [byte] +// #[byte] /** * Sorts the specified range of the array using @@ -1969,12 +2029,12 @@ private static void countingSort(byte[] a, int low, int high) { int[] count = new int[NUM_BYTE_VALUES]; /* - * Compute histogram for all values. + * Compute the histogram for all values. */ 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; ) { @@ -1998,7 +2058,7 @@ private static void countingSort(byte[] a, int low, int high) { } } -// [char] +// #[char] /** * Sorts the specified range of the array using @@ -2084,6 +2144,9 @@ static void sort(char[] a, int bits, int low, int high) { 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; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2185,7 +2248,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 for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -2292,12 +2355,12 @@ private static void countingSort(char[] a, int low, int high) { int[] count = new int[NUM_CHAR_VALUES]; /* - * Compute histogram for all values. + * Compute the histogram for all values. */ for (int i = high; i > low; ++count[a[--i]]); /* - * Place values on their final positions. + * Put values on their final positions. */ if (high - low > NUM_CHAR_VALUES) { for (int i = NUM_CHAR_VALUES; i > 0; ) { @@ -2317,7 +2380,7 @@ private static void countingSort(char[] a, int low, int high) { } } -// [short] +// #[short] /** * Sorts the specified range of the array using @@ -2403,6 +2466,9 @@ static void sort(short[] a, int bits, int low, int high) { 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; } + /* + * Insert the third element. + */ if (a3 < a[e2]) { if (a3 < a[e1]) { a[e3] = a[e2]; a[e2] = a[e1]; a[e1] = a3; @@ -2504,7 +2570,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 for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -2616,12 +2682,12 @@ private static void countingSort(short[] a, int low, int high) { int[] count = new int[NUM_SHORT_VALUES]; /* - * Compute histogram for all values. + * Compute the histogram for all values. */ for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); /* - * Place values on their final positions. + * Put values on their final positions. */ if (high - low > NUM_SHORT_VALUES) { for (int i = MAX_SHORT_INDEX; --i > Short.MAX_VALUE; ) { @@ -2645,7 +2711,7 @@ private static void countingSort(short[] a, int low, int high) { } } -// [float] +// #[float] /** * Sorts the specified range of the array using parallel merge @@ -2761,8 +2827,8 @@ static void sort(Sorter sorter, float[] a, int bits, int low, int high) { * 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)) { + if ((bits == 0 || size > MIN_MERGING_SORT_SIZE && (bits & 1) > 0) + && tryMergingSort(sorter, a, low, size)) { return; } @@ -2813,7 +2879,7 @@ && tryMergeRuns(sorter, a, low, size)) { if (a[e4] < a[e2]) { float t = a[e4]; a[e4] = a[e2]; a[e2] = t; } /* - * Insert the last element. + * Insert the third element. */ if (a3 < a[e2]) { if (a3 < a[e1]) { @@ -2830,7 +2896,7 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Try radix sort on large random data. + * Try Radix sort on large random data. */ if (size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) @@ -2931,7 +2997,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -3054,10 +3120,10 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { * It avoids expensive movements of these elements * through the whole array. */ - if (p > i && ai > pin) { // Element larger than pin + if (p > i && ai > pin) { // Element, larger than pin /* - * Find element smaller than pin. + * Find element, smaller than pin. */ while (a[--p] > pin); @@ -3139,60 +3205,20 @@ private static void insertionSort(float[] a, int low, int high) { } /** - * 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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - - /** - * Tries to sort the specified range of the array using radix sort. + * Tries to sort the specified range of the array + * using LSD (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 true if finally sorted, false otherwise + * @return true if the array is finally sorted, false otherwise */ 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 = (float[]) sorter.b) == null) { b = (float[]) tryAllocate(a, size); @@ -3206,6 +3232,9 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { int start = low - offset; int last = high - offset; + /* + * Count the number of all digits. + */ int[] count1 = new int[256]; int[] count2 = new int[256]; int[] count3 = new int[256]; @@ -3218,19 +3247,28 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { count4[(fti(a[i]) >>> 24) & 0xFF]--; } - boolean passLevel1 = passLevel(count1, 255, -size, high); - boolean passLevel2 = passLevel(count2, 255, -size, high); - boolean passLevel3 = passLevel(count3, 255, -size, high); - boolean passLevel4 = passLevel(count4, 255, -size, high); + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, 255, -size, high); + boolean processDigit2 = processDigit(count2, 255, -size, high); + boolean processDigit3 = processDigit(count3, 255, -size, high); + boolean processDigit4 = processDigit(count4, 255, -size, high); - if (passLevel1) { + /* + * Process the 1-st digit. + */ + if (processDigit1) { for (int i = low; i < high; ++i) { b[count1[fti(a[i]) & 0xFF]++ - offset] = a[i]; } } - if (passLevel2) { - if (passLevel1) { + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { for (int i = start; i < last; ++i) { a[count2[(fti(b[i]) >>> 8) & 0xFF]++] = b[i]; } @@ -3241,8 +3279,11 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { } } - if (passLevel3) { - if (passLevel1 ^ passLevel2) { + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { a[count3[(fti(b[i]) >>> 16) & 0xFF]++] = b[i]; } @@ -3253,8 +3294,11 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { } } - if (passLevel4) { - if (passLevel1 ^ passLevel2 ^ passLevel3) { + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { for (int i = start; i < last; ++i) { a[count4[(fti(b[i]) >>> 24) & 0xFF]++] = b[i]; } @@ -3265,7 +3309,10 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { } } - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + /* + * Copy the buffer to original array, if we process ood number of digits. + */ + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { System.arraycopy(b, low - offset, a, low, size); } return true; @@ -3283,15 +3330,59 @@ private static int fti(float f) { } /** - * Tries to sort the specified range of the array. + * 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 + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + + /** + * 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 + * @return true if the array is finally sorted, false otherwise */ - private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) { + private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { /* * The run array is constructed only if initial runs are @@ -3352,7 +3443,7 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) return false; } - // min 127, max 1023, ext 5120 + // Min 127, max 1023, extend to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -3384,7 +3475,7 @@ private static boolean tryMergeRuns(Sorter sorter, float[] a, int low, int size) if (++k == high) { /* - * There is a single-element run at the end. + * This is single-element run at the end. */ --k; } @@ -3560,7 +3651,7 @@ private static void mergeParts(Merger merger, float[] dst, int k, } } -// [double] +// #[double] /** * Sorts the specified range of the array using parallel merge @@ -3676,8 +3767,8 @@ static void sort(Sorter sorter, double[] a, int bits, int low, int high) { * 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)) { + if ((bits == 0 || size > MIN_MERGING_SORT_SIZE && (bits & 1) > 0) + && tryMergingSort(sorter, a, low, size)) { return; } @@ -3728,7 +3819,7 @@ && tryMergeRuns(sorter, a, low, size)) { if (a[e4] < a[e2]) { double t = a[e4]; a[e4] = a[e2]; a[e2] = t; } /* - * Insert the last element. + * Insert the third element. */ if (a3 < a[e2]) { if (a3 < a[e1]) { @@ -3745,7 +3836,7 @@ && tryMergeRuns(sorter, a, low, size)) { } /* - * Try radix sort on large random data. + * Try Radix sort on large random data. */ if (size > MIN_RADIX_SORT_SIZE && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) @@ -3846,7 +3937,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Use single pivot in case of many equal elements + } else { // Partitioning with one pivot for repeated data /* * Use the third of the five sorted elements as the pivot. @@ -3969,10 +4060,10 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { * It avoids expensive movements of these elements * through the whole array. */ - if (p > i && ai > pin) { // Element larger than pin + if (p > i && ai > pin) { // Element, larger than pin /* - * Find element smaller than pin. + * Find element, smaller than pin. */ while (a[--p] > pin); @@ -4054,60 +4145,20 @@ private static void insertionSort(double[] a, int low, int high) { } /** - * Sorts the specified range of the array using heap sort. + * Tries to sort the specified range of the array + * using LSD (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 - */ - 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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - - /** - * Tries to sort the specified range of the array using 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 true if finally sorted, false otherwise + * @return true if the array is finally sorted, false otherwise */ 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 = (double[]) sorter.b) == null) { b = (double[]) tryAllocate(a, size); @@ -4121,6 +4172,9 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { 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]; @@ -4137,21 +4191,30 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { count6[(int) ((dtl(a[i]) >>> 54) & 0x3FF)]--; } - boolean passLevel1 = passLevel(count1, 1023, -size, high); - boolean passLevel2 = passLevel(count2, 2047, -size, high); - boolean passLevel3 = passLevel(count3, 2047, -size, high); - boolean passLevel4 = passLevel(count4, 2047, -size, high); - boolean passLevel5 = passLevel(count5, 2047, -size, high); - boolean passLevel6 = passLevel(count6, 1023, -size, high); + /* + * Detect digits to be processed. + */ + boolean processDigit1 = processDigit(count1, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + boolean processDigit4 = processDigit(count4, 2047, -size, high); + boolean processDigit5 = processDigit(count5, 2047, -size, high); + boolean processDigit6 = processDigit(count6, 1023, -size, high); - if (passLevel1) { + /* + * Process the 1-st digit. + */ + if (processDigit1) { for (int i = low; i < high; ++i) { b[count1[(int) (dtl(a[i]) & 0x3FF)]++ - offset] = a[i]; } } - if (passLevel2) { - if (passLevel1) { + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { for (int i = start; i < last; ++i) { a[count2[(int) ((dtl(b[i]) >>> 10) & 0x7FF)]++] = b[i]; } @@ -4162,8 +4225,11 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { } } - if (passLevel3) { - if (passLevel1 ^ passLevel2) { + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { a[count3[(int) ((dtl(b[i]) >>> 21) & 0x7FF)]++] = b[i]; } @@ -4174,8 +4240,11 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { } } - if (passLevel4) { - if (passLevel1 ^ passLevel2 ^ passLevel3) { + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { for (int i = start; i < last; ++i) { a[count4[(int) ((dtl(b[i]) >>> 32) & 0x7FF)]++] = b[i]; } @@ -4186,8 +4255,11 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { } } - if (passLevel5) { - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4) { + /* + * Process the 5-th digit. + */ + if (processDigit5) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { for (int i = start; i < last; ++i) { a[count5[(int) ((dtl(b[i]) >>> 43) & 0x7FF)]++] = b[i]; } @@ -4198,8 +4270,11 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { } } - if (passLevel6) { - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5) { + /* + * Process the 6-th digit. + */ + if (processDigit6) { + if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4 ^ processDigit5) { for (int i = start; i < last; ++i) { a[count6[(int) ((dtl(b[i]) >>> 54) & 0x3FF)]++] = b[i]; } @@ -4210,7 +4285,10 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { } } - if (passLevel1 ^ passLevel2 ^ passLevel3 ^ passLevel4 ^ passLevel5 ^ passLevel6) { + /* + * 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; @@ -4228,15 +4306,59 @@ private static long dtl(double d) { } /** - * Tries to sort the specified range of the array. + * 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 + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + + /** + * 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 + * @return true if the array is finally sorted, false otherwise */ - private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size) { + private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { /* * The run array is constructed only if initial runs are @@ -4297,7 +4419,7 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size return false; } - // min 127, max 1023, ext 5120 + // Min 127, max 1023, extend to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -4329,7 +4451,7 @@ private static boolean tryMergeRuns(Sorter sorter, double[] a, int low, int size if (++k == high) { /* - * There is a single-element run at the end. + * This is single-element run at the end. */ --k; } @@ -4505,7 +4627,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, } } -// [class] +// #[class] /** * This class implements parallel sorting. From e1b01cfb4f19b5ae536030f0e444964f314582f6 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Fri, 12 Nov 2021 18:38:05 +0300 Subject: [PATCH 08/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) - Set limit for buffer length - Radix sort is invoked on fully random data - Use 3 steps in Radix sort isntead of 4 for int/float - Set better options for parallel sorting --- .../classes/java/util/DualPivotQuicksort.java | 255 +++++++++--------- test/jdk/java/util/Arrays/Sorting.java | 4 +- 2 files changed, 123 insertions(+), 136 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 01438b9da9cc7..4b75141d305f8 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -46,7 +46,7 @@ * * @version 2020.06.14 * - * @since 1.7 * 14 & 18 + * @since 1.7 * 14 ^ 18 */ final class DualPivotQuicksort { @@ -131,21 +131,11 @@ private DualPivotQuicksort() {} private static final int MAX_RECURSION_DEPTH = 64 * DEPTH; /** - * 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 + * Max length of additional buffer, + * limited by max_heap / 64 or 256m elements (2gb max). */ - private static int getDepth(int parallelism, int size) { - int depth = 0; - - while ((parallelism >>= 3) > 0 && (size >>= 2) > 0) { - depth -= 2; - } - return depth; - } + private static final int MAX_BUFFER_LENGTH = + (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, 256L << 20); /** * Sorts the specified range of the array using parallel merge @@ -242,6 +232,9 @@ && tryMergingSort(sorter, a, low, size)) { int e4 = (e3 + e5) >>> 1; int a3 = a[e3]; + boolean isRandom = + a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + /* * Sort these elements in place by the combination * of 4-element sorting network and insertion sort. @@ -278,10 +271,10 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large random data. + * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && tryRadixSort(sorter, a, low, high)) { return; @@ -593,7 +586,7 @@ private static void insertionSort(int[] a, int low, int high) { * @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 true if the array is finally sorted, false otherwise + * @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; @@ -617,32 +610,29 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { /* * Count the number of all digits. */ - int[] count1 = new int[256]; - int[] count2 = new int[256]; - int[] count3 = new int[256]; - int[] count4 = new int[256]; + 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] & 0xFF]--; - count2[(a[i] >>> 8) & 0xFF]--; - count3[(a[i] >>> 16) & 0xFF]--; - count4[(a[i] >>> 24) ^ 0x80]--; // Reverse the sign bit + 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, 255, -size, high); - boolean processDigit2 = processDigit(count2, 255, -size, high); - boolean processDigit3 = processDigit(count3, 255, -size, high); - boolean processDigit4 = processDigit(count4, 255, -size, high); + boolean processDigit1 = processDigit(count1, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); /* * Process the 1-st digit. */ if (processDigit1) { for (int i = low; i < high; ++i) { - b[count1[a[i] & 0xFF]++ - offset] = a[i]; + b[count1[a[i] & 0x3FF]++ - offset] = a[i]; } } @@ -652,11 +642,11 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { if (processDigit2) { if (processDigit1) { for (int i = start; i < last; ++i) { - a[count2[(b[i] >>> 8) & 0xFF]++] = b[i]; + a[count2[(b[i] >>> 10) & 0x7FF]++] = b[i]; } } else { for (int i = low; i < high; ++i) { - b[count2[(a[i] >>> 8) & 0xFF]++ - offset] = a[i]; + b[count2[(a[i] >>> 10) & 0x7FF]++ - offset] = a[i]; } } } @@ -667,26 +657,11 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { if (processDigit3) { if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { - a[count3[(b[i] >>> 16) & 0xFF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count3[(a[i] >>> 16) & 0xFF]++ - offset] = a[i]; - } - } - } - - /* - * Process the 4-th digit. - */ - if (processDigit4) { - if (processDigit1 ^ processDigit2 ^ processDigit3) { - for (int i = start; i < last; ++i) { - a[count4[(b[i] >>> 24) ^ 0x80]++] = b[i]; + a[count3[(b[i] >>> 21) ^ 0x400]++] = b[i]; } } else { for (int i = low; i < high; ++i) { - b[count4[(a[i] >>> 24) ^ 0x80]++ - offset] = a[i]; + b[count3[(a[i] >>> 21) ^ 0x400]++ - offset] = a[i]; } } } @@ -694,7 +669,7 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { /* * Copy the buffer to original array, if we process ood number of digits. */ - if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { System.arraycopy(b, low - offset, a, low, size); } return true; @@ -707,7 +682,7 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { * @param last the last index of count array * @param total the total number of elements * @param high the index of the last element, exclusive - * @return false if the digit can be skipped, true otherwise + * @return {@code true} if the digit must be processed, otherwise {@code false} */ private static boolean processDigit(int[] count, int last, int total, int high) { @@ -715,13 +690,12 @@ private static boolean processDigit(int[] count, int last, int total, int high) * Check if we can skip given digit. */ for (int c : count) { - if (c == 0) { - continue; - } if (c == total) { return false; } - break; + if (c < 0) { + break; + } } /* @@ -786,7 +760,7 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { * @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 the array is finally sorted, false otherwise + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { @@ -849,7 +823,7 @@ private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) return false; } - // Min 127, max 1023, extend to 5120 + // Initial min 127, max 1023, extended to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -927,9 +901,7 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, 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; } @@ -1024,6 +996,9 @@ private static void mergeParts(Merger merger, int[] dst, int k, } } + /* + * Reserve space for the left sub-parts. + */ int d = mi2 - lo2 + mi1 - lo1; /* @@ -1154,6 +1129,9 @@ && tryMergingSort(sorter, a, low, size)) { int e4 = (e3 + e5) >>> 1; long a3 = a[e3]; + boolean isRandom = + a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + /* * Sort these elements in place by the combination * of 4-element sorting network and insertion sort. @@ -1190,10 +1168,10 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large random data. + * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && tryRadixSort(sorter, a, low, high)) { return; @@ -1505,7 +1483,7 @@ private static void insertionSort(long[] a, int low, int high) { * @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 true if the array is finally sorted, false otherwise + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { long[] b; int offset = low, size = high - low; @@ -1699,7 +1677,7 @@ private static void pushDown(long[] a, int p, long value, int low, int high) { * @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 the array is finally sorted, false otherwise + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { @@ -1762,7 +1740,7 @@ private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size return false; } - // Min 127, max 1023, extend to 5120 + // Initial min 127, max 1023, extended to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -1840,9 +1818,7 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, 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; } @@ -1937,6 +1913,9 @@ private static void mergeParts(Merger merger, long[] dst, int k, } } + /* + * Reserve space for the left sub-parts. + */ int d = mi2 - lo2 + mi1 - lo1; /* @@ -2860,6 +2839,9 @@ && tryMergingSort(sorter, a, low, size)) { int e4 = (e3 + e5) >>> 1; float a3 = a[e3]; + boolean isRandom = + a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + /* * Sort these elements in place by the combination * of 4-element sorting network and insertion sort. @@ -2896,10 +2878,10 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large random data. + * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && tryRadixSort(sorter, a, low, high)) { return; @@ -3211,7 +3193,7 @@ private static void insertionSort(float[] a, int low, int high) { * @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 true if the array is finally sorted, false otherwise + * @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; @@ -3235,32 +3217,29 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { /* * Count the number of all digits. */ - int[] count1 = new int[256]; - int[] count2 = new int[256]; - int[] count3 = new int[256]; - int[] count4 = new int[256]; + 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]) & 0xFF]--; - count2[(fti(a[i]) >>> 8) & 0xFF]--; - count3[(fti(a[i]) >>> 16) & 0xFF]--; - count4[(fti(a[i]) >>> 24) & 0xFF]--; + 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, 255, -size, high); - boolean processDigit2 = processDigit(count2, 255, -size, high); - boolean processDigit3 = processDigit(count3, 255, -size, high); - boolean processDigit4 = processDigit(count4, 255, -size, high); + boolean processDigit1 = processDigit(count1, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); /* * Process the 1-st digit. */ if (processDigit1) { for (int i = low; i < high; ++i) { - b[count1[fti(a[i]) & 0xFF]++ - offset] = a[i]; + b[count1[fti(a[i]) & 0x3FF]++ - offset] = a[i]; } } @@ -3270,11 +3249,11 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { if (processDigit2) { if (processDigit1) { for (int i = start; i < last; ++i) { - a[count2[(fti(b[i]) >>> 8) & 0xFF]++] = b[i]; + a[count2[(fti(b[i]) >>> 10) & 0x7FF]++] = b[i]; } } else { for (int i = low; i < high; ++i) { - b[count2[(fti(a[i]) >>> 8) & 0xFF]++ - offset] = a[i]; + b[count2[(fti(a[i]) >>> 10) & 0x7FF]++ - offset] = a[i]; } } } @@ -3285,26 +3264,11 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { if (processDigit3) { if (processDigit1 ^ processDigit2) { for (int i = start; i < last; ++i) { - a[count3[(fti(b[i]) >>> 16) & 0xFF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count3[(fti(a[i]) >>> 16) & 0xFF]++ - offset] = a[i]; - } - } - } - - /* - * Process the 4-th digit. - */ - if (processDigit4) { - if (processDigit1 ^ processDigit2 ^ processDigit3) { - for (int i = start; i < last; ++i) { - a[count4[(fti(b[i]) >>> 24) & 0xFF]++] = b[i]; + a[count3[(fti(b[i]) >>> 21) & 0x7FF]++] = b[i]; } } else { for (int i = low; i < high; ++i) { - b[count4[(fti(a[i]) >>> 24) & 0xFF]++ - offset] = a[i]; + b[count3[(fti(a[i]) >>> 21) & 0x7FF]++ - offset] = a[i]; } } } @@ -3312,7 +3276,7 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { /* * Copy the buffer to original array, if we process ood number of digits. */ - if (processDigit1 ^ processDigit2 ^ processDigit3 ^ processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { System.arraycopy(b, low - offset, a, low, size); } return true; @@ -3380,7 +3344,7 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { * @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 the array is finally sorted, false otherwise + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { @@ -3443,7 +3407,7 @@ private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int siz return false; } - // Min 127, max 1023, extend to 5120 + // Initial min 127, max 1023, extended to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -3521,9 +3485,7 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, 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; } @@ -3618,6 +3580,9 @@ private static void mergeParts(Merger merger, float[] dst, int k, } } + /* + * Reserve space for the left sub-parts. + */ int d = mi2 - lo2 + mi1 - lo1; /* @@ -3800,6 +3765,9 @@ && tryMergingSort(sorter, a, low, size)) { int e4 = (e3 + e5) >>> 1; double a3 = a[e3]; + boolean isRandom = + a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + /* * Sort these elements in place by the combination * of 4-element sorting network and insertion sort. @@ -3836,10 +3804,10 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large random data. + * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && tryRadixSort(sorter, a, low, high)) { return; @@ -4151,7 +4119,7 @@ private static void insertionSort(double[] a, int low, int high) { * @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 true if the array is finally sorted, false otherwise + * @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; @@ -4356,7 +4324,7 @@ private static void pushDown(double[] a, int p, double value, int low, int high) * @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 the array is finally sorted, false otherwise + * @return {@code true} if the array is finally sorted, otherwise {@code false} */ private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { @@ -4419,7 +4387,7 @@ private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int si return false; } - // Min 127, max 1023, extend to 5120 + // Initial min 127, max 1023, extended to 5120 run = new int[((size >> 10) | 0x7F) & 0x3FF]; run[0] = low; @@ -4497,9 +4465,7 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, 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; } @@ -4594,6 +4560,9 @@ private static void mergeParts(Merger merger, double[] dst, int k, } } + /* + * Reserve space for the left sub-parts. + */ int d = mi2 - lo2 + mi1 - lo1; /* @@ -4633,7 +4602,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, * This class implements parallel sorting. */ private static final class Sorter extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 123456789L; @SuppressWarnings("serial") private final Object a, b; @@ -4667,7 +4636,7 @@ public final void compute() { } else if (a instanceof double[]) { sort(this, (double[]) a, depth, low, low + size); } else { - throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); } } tryComplete(); @@ -4694,7 +4663,6 @@ public final void onCompletion(CountedCompleter caller) { private void forkSorter(int depth, int low, int high) { addToPendingCount(1); - Object a = this.a; // Use local variable for better performance new Sorter(this, a, b, low, high - low, offset, depth).fork(); } } @@ -4703,7 +4671,7 @@ private void forkSorter(int depth, int low, int high) { * This class implements parallel merging. */ private static final class Merger extends CountedCompleter { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 123456789L; @SuppressWarnings("serial") private final Object dst, a1, a2; @@ -4737,7 +4705,7 @@ public final void compute() { mergeParts(this, (double[]) dst, k, (double[]) a1, lo1, hi1, (double[]) a2, lo2, hi2); } else { - throw new IllegalArgumentException("Unknown type: " + dst.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + dst.getClass().getName()); } propagateCompletion(); } @@ -4753,7 +4721,7 @@ private void forkMerger(Object dst, int k, * This class implements parallel merging of runs. */ private static final class RunMerger extends RecursiveTask { - private static final long serialVersionUID = 20180818L; + private static final long serialVersionUID = 123456789L; @SuppressWarnings("serial") private final Object a, b; @@ -4785,7 +4753,7 @@ protected final Object compute() { if (a instanceof double[]) { return mergeRuns((double[]) a, (double[]) b, offset, aim, true, run, lo, hi); } - throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); } private RunMerger forkMe() { @@ -4800,27 +4768,46 @@ private Object getDestination() { } /** - * Tries to allocate memory for new array. + * Calculates the negative double depth of parallel merging. + * + * @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 >>= 1) > 0 && (size >>= 2) > 0) { + depth -= 2; + } + return depth; + } + + /** + * Tries to allocate memory for additional buffer. * * @param a the array of given type - * @param size the new array size - * @return null if there is not enough memory, created array otherwise + * @param length the additional buffer length + * @return {@code null} if requested length is too large, otherwise created buffer */ - private static Object tryAllocate(Object a, int size) { + private static Object tryAllocate(Object a, int length) { + if (length > MAX_BUFFER_LENGTH) { + return null; + } try { if (a instanceof int[]) { - return new int[size]; + return new int[length]; } if (a instanceof long[]) { - return new long[size]; + return new long[length]; } if (a instanceof float[]) { - return new float[size]; + return new float[length]; } if (a instanceof double[]) { - return new double[size]; + return new double[length]; } - throw new IllegalArgumentException("Unknown type: " + a.getClass().getName()); + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); } catch (OutOfMemoryError e) { return null; } diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index 844444aa126c9..ef3c5f2dc4b6d 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -54,7 +54,7 @@ public class Sorting { // Initial random values for long run (default) private static final TestRandom[] LONG_RUN_RANDOMS = { - TestRandom.BABA, TestRandom.DEDA, TestRandom.C0FFEE }; + TestRandom.DEDA, TestRandom.BABA, TestRandom.C0FFEE }; // Initial random values for short run private static final TestRandom[] SHORT_RUN_RANDOMS = { @@ -2063,8 +2063,8 @@ public String toString() { 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) { From 4baa9a39ae6fc3c67c059f87531482de60cb2126 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Mon, 15 Nov 2021 19:16:08 +0300 Subject: [PATCH 09/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) Updated comments for partitioning --- .../classes/java/util/DualPivotQuicksort.java | 108 +++++++++--------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 4b75141d305f8..5810b51acb6d6 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -324,11 +324,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -390,7 +390,7 @@ && tryRadixSort(sorter, a, low, high)) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * * left part central part right part * +------------------------------------------------------+ @@ -400,11 +400,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ @@ -1221,11 +1221,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -1287,7 +1287,7 @@ && tryRadixSort(sorter, a, low, high)) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * * left part central part right part * +------------------------------------------------------+ @@ -1297,11 +1297,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ @@ -2184,11 +2184,11 @@ static void sort(char[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -2245,7 +2245,7 @@ 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 * +------------------------------------------------------+ @@ -2255,11 +2255,11 @@ static void sort(char[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ @@ -2506,11 +2506,11 @@ static void sort(short[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -2567,7 +2567,7 @@ 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 * +------------------------------------------------------+ @@ -2577,11 +2577,11 @@ static void sort(short[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ @@ -2931,11 +2931,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -2997,7 +2997,7 @@ && tryRadixSort(sorter, a, low, high)) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * * left part central part right part * +------------------------------------------------------+ @@ -3007,11 +3007,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ @@ -3857,11 +3857,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot1 - * pivot1 <= all in (k, upper) <= pivot2 - * all in [upper, end) > pivot2 + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 * * Pointer k is the last index of ?-part */ @@ -3923,7 +3923,7 @@ && tryRadixSort(sorter, a, low, high)) { a[e3] = a[lower]; /* - * Traditional 3-way (Dutch National Flag) partitioning + * Dutch National Flag partitioning * * left part central part right part * +------------------------------------------------------+ @@ -3933,11 +3933,11 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants: + * Invariants * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot * * Pointer k is the last index of ?-part */ From 41b92f67f4d26be9a9c0e00754e4edcf6cf9e4e9 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 30 Nov 2021 00:10:40 +0300 Subject: [PATCH 10/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Updated javadoc * Optimized insertion sort threshold * Refactored parallel sorting section * Improved step for pivot candidates * Changed condition for Radix sort --- .../classes/java/util/DualPivotQuicksort.java | 178 +++++++----------- test/jdk/java/util/Arrays/Sorting.java | 16 +- 2 files changed, 80 insertions(+), 114 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 5810b51acb6d6..08f12d8f6214a 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -36,8 +36,9 @@ * faster than traditional (one-pivot) Quicksort implementations. * * There are also additional algorithms, invoked from the Dual-Pivot - * Quicksort such as mixed insertion sort, merging sort and counting - * sort, heap sort and LSD Radix sort, 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 @@ -58,7 +59,7 @@ private DualPivotQuicksort() {} /** * 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. @@ -153,12 +154,8 @@ private DualPivotQuicksort() {} * @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 : (int[]) tryAllocate(a, 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); } @@ -217,7 +214,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -274,8 +271,8 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) && tryRadixSort(sorter, a, low, high)) { return; } @@ -594,14 +591,12 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { /* * Allocate additional buffer. */ - if (sorter == null || (b = (int[]) sorter.b) == null) { - b = (int[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (int[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (int[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } int start = low - offset; @@ -867,14 +862,12 @@ private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) if (count > 1) { int[] b; int offset = low; - if (sorter == null || (b = (int[]) sorter.b) == null) { - b = (int[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (int[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (int[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -1050,12 +1043,8 @@ 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 : (long[]) tryAllocate(a, 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); } @@ -1114,7 +1103,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -1171,8 +1160,8 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) && tryRadixSort(sorter, a, low, high)) { return; } @@ -1491,14 +1480,12 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { /* * Allocate additional buffer. */ - if (sorter == null || (b = (long[]) sorter.b) == null) { - b = (long[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (long[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (long[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } int start = low - offset; @@ -1784,14 +1771,12 @@ private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size if (count > 1) { long[] b; int offset = low; - if (sorter == null || (b = (long[]) sorter.b) == null) { - b = (long[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (long[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (long[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -2090,7 +2075,7 @@ static void sort(char[] a, int bits, int low, int high) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -2412,7 +2397,7 @@ static void sort(short[] a, int bits, int low, int high) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -2731,12 +2716,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 : (float[]) tryAllocate(a, 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); } @@ -2824,7 +2805,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -2881,8 +2862,8 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) && tryRadixSort(sorter, a, low, high)) { return; } @@ -3201,14 +3182,12 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { /* * Allocate additional buffer. */ - if (sorter == null || (b = (float[]) sorter.b) == null) { - b = (float[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (float[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (float[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } int start = low - offset; @@ -3451,14 +3430,12 @@ private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int siz if (count > 1) { float[] b; int offset = low; - if (sorter == null || (b = (float[]) sorter.b) == null) { - b = (float[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (float[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (float[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -3657,12 +3634,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 : (double[]) tryAllocate(a, 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); } @@ -3750,7 +3723,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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 >> 8) + 1; /* * Five elements around (and including) the central element @@ -3807,8 +3780,8 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data. */ if (size > MIN_RADIX_SORT_SIZE - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH || isRandom) - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] + && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom + && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) && tryRadixSort(sorter, a, low, high)) { return; } @@ -4127,14 +4100,12 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { /* * Allocate additional buffer. */ - if (sorter == null || (b = (double[]) sorter.b) == null) { - b = (double[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (double[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (double[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } int start = low - offset; @@ -4431,14 +4402,12 @@ private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int si if (count > 1) { double[] b; int offset = low; - if (sorter == null || (b = (double[]) sorter.b) == null) { - b = (double[]) tryAllocate(a, size); - - if (b == null) { + if (sorter != null && (b = (double[]) sorter.b) != null) { + offset = sorter.offset; + } else { + if ((b = (double[]) tryAllocate(a, size)) == null) { return false; } - } else { - offset = sorter.offset; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -4608,6 +4577,19 @@ private static final class Sorter extends CountedCompleter { private final Object a, b; private final int low, size, offset, depth; + private Sorter(Object a, int parallelism, int low, int size, int depth) { + this.a = a; + this.low = low; + this.size = size; + this.offset = low; + + while ((parallelism >>= 1) > 0 && (size >>= 8) > 0) { + depth -= 2; + } + this.b = depth == 0 ? null : tryAllocate(a, this.size); + this.depth = b == null ? 0 : depth; + } + private Sorter(CountedCompleter parent, Object a, Object b, int low, int size, int offset, int depth) { super(parent); @@ -4616,7 +4598,7 @@ private Sorter(CountedCompleter parent, this.low = low; this.size = size; this.offset = offset; - this.depth = b == null ? 0 : depth; + this.depth = depth; } @Override @@ -4767,22 +4749,6 @@ private Object getDestination() { } } - /** - * Calculates the negative double depth of parallel merging. - * - * @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 >>= 1) > 0 && (size >>= 2) > 0) { - depth -= 2; - } - return depth; - } - /** * Tries to allocate memory for additional buffer. * diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index ef3c5f2dc4b6d..582bc745c58fb 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -45,20 +45,20 @@ public class Sorting { private static final PrintStream err = System.err; // Lengths of arrays for long run (default) - private static final int[] LONG_RUN_LENGTHS = { - 1, 3, 8, 21, 55, 100, 1_000, 10_000, 100_000 }; + 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, 8, 55, 100, 10_000 }; + private static final int[] SHORT_RUN_LENGTHS = + { 1, 8, 55, 100, 10_000 }; // Initial random values for long run (default) - private static final TestRandom[] LONG_RUN_RANDOMS = { - TestRandom.DEDA, TestRandom.BABA, TestRandom.C0FFEE }; + private static final TestRandom[] LONG_RUN_RANDOMS = + { TestRandom.DEDA, TestRandom.BABA, TestRandom.C0FFEE }; // Initial random values for short run - private static final TestRandom[] SHORT_RUN_RANDOMS = { - TestRandom.C0FFEE }; + private static final TestRandom[] SHORT_RUN_RANDOMS = + { TestRandom.C0FFEE }; // Constant to fill the left part of array private static final int A380 = 0xA380; From 95f153862e9f2ab839e8eb23ef33822641fc9156 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Wed, 12 Jan 2022 17:17:37 +0300 Subject: [PATCH 11/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Updated javadoc * Improved mixed insertion * Optimized sort networking * Improved pivot partitioning --- .../classes/java/util/DualPivotQuicksort.java | 1140 +++++++++-------- 1 file changed, 572 insertions(+), 568 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 08f12d8f6214a..bf713959e3e76 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -59,12 +59,12 @@ private DualPivotQuicksort() {} /** * Max array size to use mixed insertion sort. */ - private static final int MAX_MIXED_INSERTION_SORT_SIZE = 124; + private static final int MAX_MIXED_INSERTION_SORT_SIZE = 113; /** * Max array size to use insertion sort. */ - private static final int MAX_INSERTION_SORT_SIZE = 44; + private static final int MAX_INSERTION_SORT_SIZE = 26; /** * Min array size to perform sorting in parallel. @@ -162,13 +162,12 @@ static void sort(int[] 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 */ @@ -180,7 +179,7 @@ static void sort(Sorter sorter, int[] a, int bits, int low, int high) { * Run mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high - 3 * ((size >> 5) << 3), high); + mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); return; } @@ -188,7 +187,7 @@ 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) { - insertionSort(a, low, high); + insertionSort(a, low, high, (bits & 1) == 0); return; } @@ -202,8 +201,7 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Switch to heap sort, if execution - * time is becoming quadratic. + * Switch to heap sort, if execution time is quadratic. */ if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); @@ -236,19 +234,19 @@ && tryMergingSort(sorter, a, low, size)) { * 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[e2] > a[e5]) { int t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { int t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { int t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { int t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { int t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -268,7 +266,8 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large fully random data. + * Try Radix sort on large fully random data, + * taking into account parallel context. */ if (size > MIN_RADIX_SORT_SIZE && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom @@ -313,38 +312,40 @@ && tryRadixSort(sorter, a, low, high)) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper + * 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 * - * Invariants + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -369,7 +370,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -389,7 +390,7 @@ && tryRadixSort(sorter, a, low, high)) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -397,13 +398,15 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { int ak = a[k]; @@ -447,8 +450,8 @@ && tryRadixSort(sorter, a, low, high)) { /** * 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 @@ -462,96 +465,81 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ private static void mixedInsertionSort(int[] a, int low, int end, int high) { - if (end == high) { + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end && low < p; ) { + int ai = a[i = low]; /* - * Invoke simple insertion sort on tiny array. + * Find pin element, smaller than the given element. */ - for (int i; ++low < high; ) { - int ai = a[i = low]; + while (ai < a[--p]); - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + /* + * Swap these elements. + */ + ai = a[p]; a[p] = a[i]; + + /* + * Insert element into sorted part. + */ + while (ai < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; + } - } else { + /* + * Continue with simple insertion sort. + */ + for (int i; low < end; ++low) { + int ai = a[i = low]; /* - * Start with pin insertion sort on small part. + * Insert element into sorted part. */ - int pin = a[end]; - - for (int i, p = high; ++low < end; ) { - int ai = a[i = low]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } - /* - * Put elements larger than an element called pin - * to the end of array (the proper area for them). - * It avoids expensive movements of these elements - * through the whole array. - */ - if (p > i && ai > pin) { // Element, larger than pin + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + int a1 = a[i = low], a2 = a[++low]; - /* - * Find element, smaller than pin. - */ - while (a[--p] > pin); + /* + * 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) { - /* - * Swap it with larger element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } + while (a1 < a[--i]) { + a[i + 2] = a[i]; } + a[++i + 1] = a1; - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { + while (a2 < a[--i]) { a[i + 1] = a[i]; } - a[i + 1] = ai; - } - - /* - * Continue with pair insertion sort on remain part. - */ - for (int i; low < high; ++low) { - int a1 = a[i = low], a2 = a[++low]; + a[i + 1] = a2; - /* - * 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 (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; } } } @@ -562,13 +550,25 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { * @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 + * @param leftmost indicates that the range is the leftmost part */ - private static void insertionSort(int[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - int ai = a[i = k]; + private static void insertionSort(int[] a, int low, int high, boolean leftmost) { + if (leftmost) { + for (int i, k = low; ++k < high; ) { + int ai = a[i = k]; - if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { + if (ai < a[i - 1]) { + while (--i >= low && ai < a[i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } + } + } else { + for (int i; ++low < high; ) { + int ai = a[i = low]; + + while (ai < a[--i]) { a[i + 1] = a[i]; } a[i + 1] = ai; @@ -1051,13 +1051,12 @@ static void sort(long[] 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 */ @@ -1069,7 +1068,7 @@ static void sort(Sorter sorter, long[] a, int bits, int low, int high) { * Run mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high - 3 * ((size >> 5) << 3), high); + mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); return; } @@ -1077,7 +1076,7 @@ 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) { - insertionSort(a, low, high); + insertionSort(a, low, high, (bits & 1) == 0); return; } @@ -1091,8 +1090,7 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Switch to heap sort, if execution - * time is becoming quadratic. + * Switch to heap sort, if execution time is quadratic. */ if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); @@ -1125,19 +1123,19 @@ && tryMergingSort(sorter, a, low, size)) { * 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[e2] > a[e5]) { long t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { long t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { long t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { long t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { long t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -1157,7 +1155,8 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large fully random data. + * Try Radix sort on large fully random data, + * taking into account parallel context. */ if (size > MIN_RADIX_SORT_SIZE && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom @@ -1202,38 +1201,40 @@ && tryRadixSort(sorter, a, low, high)) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper + * 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 * - * Invariants + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -1258,7 +1259,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -1278,7 +1279,7 @@ && tryRadixSort(sorter, a, low, high)) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -1286,13 +1287,15 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { long ak = a[k]; @@ -1336,8 +1339,8 @@ && tryRadixSort(sorter, a, low, high)) { /** * 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 @@ -1351,96 +1354,81 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ private static void mixedInsertionSort(long[] a, int low, int end, int high) { - if (end == high) { + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end && low < p; ) { + long ai = a[i = low]; /* - * Invoke simple insertion sort on tiny array. + * Find pin element, smaller than the given element. */ - for (int i; ++low < high; ) { - long ai = a[i = low]; + while (ai < a[--p]); - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + /* + * Swap these elements. + */ + ai = a[p]; a[p] = a[i]; + + /* + * Insert element into sorted part. + */ + while (ai < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; + } - } else { + /* + * Continue with simple insertion sort. + */ + for (int i; low < end; ++low) { + long ai = a[i = low]; /* - * Start with pin insertion sort on small part. + * Insert element into sorted part. */ - long pin = a[end]; - - for (int i, p = high; ++low < end; ) { - long ai = a[i = low]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } - /* - * Put elements larger than an element called pin - * to the end of array (the proper area for them). - * It avoids expensive movements of these elements - * through the whole array. - */ - if (p > i && ai > pin) { // Element, larger than pin + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + long a1 = a[i = low], a2 = a[++low]; - /* - * Find element, smaller than pin. - */ - while (a[--p] > pin); + /* + * 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) { - /* - * Swap it with larger element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } + while (a1 < a[--i]) { + a[i + 2] = a[i]; } + a[++i + 1] = a1; - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { + while (a2 < a[--i]) { a[i + 1] = a[i]; } - a[i + 1] = ai; - } - - /* - * Continue with pair insertion sort on remain part. - */ - for (int i; low < high; ++low) { - long 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) { - - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + 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; } } } @@ -1451,13 +1439,25 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { * @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 + * @param leftmost indicates that the range is the leftmost part */ - private static void insertionSort(long[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - long ai = a[i = k]; + private static void insertionSort(long[] a, int low, int high, boolean leftmost) { + if (leftmost) { + for (int i, k = low; ++k < high; ) { + long ai = a[i = k]; - if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { + if (ai < a[i - 1]) { + while (--i >= low && ai < a[i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } + } + } else { + for (int i; ++low < high; ) { + long ai = a[i = low]; + + while (ai < a[--i]) { a[i + 1] = a[i]; } a[i + 1] = ai; @@ -2041,12 +2041,11 @@ 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. + * 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 */ @@ -2063,8 +2062,7 @@ 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 += DEPTH) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); @@ -2094,19 +2092,19 @@ static void sort(char[] a, int bits, int low, int high) { * 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[e2] > a[e5]) { char t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { char t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { char t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { char t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { char t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -2161,38 +2159,40 @@ 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 + * 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 * - * Invariants + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -2212,7 +2212,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 { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -2232,7 +2232,7 @@ static void sort(char[] a, int bits, int low, int high) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -2240,13 +2240,15 @@ static void sort(char[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { char ak = a[k]; @@ -2363,12 +2365,11 @@ static void sort(short[] 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. + * 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 */ @@ -2385,8 +2386,7 @@ 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 += DEPTH) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); @@ -2416,19 +2416,19 @@ static void sort(short[] a, int bits, int low, int high) { * 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[e2] > a[e5]) { short t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { short t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { short t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { short t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { short t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -2483,38 +2483,40 @@ 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 + * left part central part right part + * +------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | + * +------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -2534,7 +2536,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 { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -2554,7 +2556,7 @@ static void sort(short[] a, int bits, int low, int high) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -2562,13 +2564,15 @@ static void sort(short[] a, int bits, int low, int high) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { short ak = a[k]; @@ -2753,13 +2757,12 @@ 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 */ @@ -2771,7 +2774,7 @@ static void sort(Sorter sorter, float[] a, int bits, int low, int high) { * Run mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high - 3 * ((size >> 5) << 3), high); + mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); return; } @@ -2779,7 +2782,7 @@ 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) { - insertionSort(a, low, high); + insertionSort(a, low, high, (bits & 1) == 0); return; } @@ -2793,8 +2796,7 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Switch to heap sort, if execution - * time is becoming quadratic. + * Switch to heap sort, if execution time is quadratic. */ if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); @@ -2827,19 +2829,19 @@ && tryMergingSort(sorter, a, low, size)) { * 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[e2] > a[e5]) { float t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { float t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { float t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { float t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { float t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -2859,7 +2861,8 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large fully random data. + * Try Radix sort on large fully random data, + * taking into account parallel context. */ if (size > MIN_RADIX_SORT_SIZE && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom @@ -2904,38 +2907,40 @@ && tryRadixSort(sorter, a, low, high)) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper + * left part central part right part + * +------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | + * +------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -2960,7 +2965,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -2980,7 +2985,7 @@ && tryRadixSort(sorter, a, low, high)) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -2988,13 +2993,15 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { float ak = a[k]; @@ -3038,8 +3045,8 @@ && tryRadixSort(sorter, a, low, high)) { /** * 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 @@ -3053,96 +3060,81 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ private static void mixedInsertionSort(float[] a, int low, int end, int high) { - if (end == high) { + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end && low < p; ) { + float ai = a[i = low]; /* - * Invoke simple insertion sort on tiny array. + * Find pin element, smaller than the given element. */ - for (int i; ++low < high; ) { - float ai = a[i = low]; + while (ai < a[--p]); - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + /* + * Swap these elements. + */ + ai = a[p]; a[p] = a[i]; + + /* + * Insert element into sorted part. + */ + while (ai < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; + } - } else { + /* + * Continue with simple insertion sort. + */ + for (int i; low < end; ++low) { + float ai = a[i = low]; /* - * Start with pin insertion sort on small part. + * Insert element into sorted part. */ - float pin = a[end]; - - for (int i, p = high; ++low < end; ) { - float ai = a[i = low]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } - /* - * Put elements larger than an element called pin - * to the end of array (the proper area for them). - * It avoids expensive movements of these elements - * through the whole array. - */ - if (p > i && ai > pin) { // Element, larger than pin + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + float a1 = a[i = low], a2 = a[++low]; - /* - * Find element, smaller than pin. - */ - while (a[--p] > pin); + /* + * 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) { - /* - * Swap it with larger element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } + while (a1 < a[--i]) { + a[i + 2] = a[i]; } + a[++i + 1] = a1; - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { + while (a2 < a[--i]) { a[i + 1] = a[i]; } - a[i + 1] = ai; - } - - /* - * Continue with pair insertion sort on remain part. - */ - for (int i; low < high; ++low) { - float 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) { - - 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; + 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; } } } @@ -3153,13 +3145,25 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { * @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 + * @param leftmost indicates that the range is the leftmost part */ - private static void insertionSort(float[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - float ai = a[i = k]; + private static void insertionSort(float[] a, int low, int high, boolean leftmost) { + if (leftmost) { + for (int i, k = low; ++k < high; ) { + float ai = a[i = k]; - if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { + if (ai < a[i - 1]) { + while (--i >= low && ai < a[i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } + } + } else { + for (int i; ++low < high; ) { + float ai = a[i = low]; + + while (ai < a[--i]) { a[i + 1] = a[i]; } a[i + 1] = ai; @@ -3671,13 +3675,12 @@ 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 */ @@ -3689,7 +3692,7 @@ static void sort(Sorter sorter, double[] a, int bits, int low, int high) { * Run mixed insertion sort on small non-leftmost parts. */ if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high - 3 * ((size >> 5) << 3), high); + mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); return; } @@ -3697,7 +3700,7 @@ 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) { - insertionSort(a, low, high); + insertionSort(a, low, high, (bits & 1) == 0); return; } @@ -3711,8 +3714,7 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Switch to heap sort, if execution - * time is becoming quadratic. + * Switch to heap sort, if execution time is quadratic. */ if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { heapSort(a, low, high); @@ -3745,19 +3747,19 @@ && tryMergingSort(sorter, a, low, size)) { * 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[e2] > a[e5]) { double t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { double t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { double t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { double t = a[e4]; a[e4] = a[e5]; a[e5] = t; } + if (a[e2] > a[e4]) { double t = a[e2]; a[e2] = a[e4]; a[e4] = t; } /* * Insert the third element. @@ -3777,7 +3779,8 @@ && tryMergingSort(sorter, a, low, size)) { } /* - * Try Radix sort on large fully random data. + * Try Radix sort on large fully random data, + * taking into account parallel context. */ if (size > MIN_RADIX_SORT_SIZE && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom @@ -3822,38 +3825,40 @@ && tryRadixSort(sorter, a, low, high)) { /* * Backward 3-interval partitioning * - * left part central part right part - * +------------------------------------------------------------+ - * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | - * +------------------------------------------------------------+ - * ^ ^ ^ - * | | | - * lower k upper + * left part central part right part + * +------------------------------------------------------------------+ + * | < pivot1 | ? | pivot1 <= && <= pivot2 | > pivot2 | + * +------------------------------------------------------------------+ + * ^ ^ ^ + * | | | + * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot1 * all in (k, upper) in [pivot1, pivot2] * all in [upper, end) > pivot2 - * - * Pointer k is the last index of ?-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; + while (a[++lower] < pivot1) { + if (lower == k) { break; } } + 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 side a[k] = a[--upper]; a[upper] = ak; @@ -3878,7 +3883,7 @@ && tryRadixSort(sorter, a, low, high)) { sort(sorter, a, bits | 1, upper + 1, high); } - } else { // Partitioning with one pivot for repeated data + } else { // Partitioning with one pivot /* * Use the third of the five sorted elements as the pivot. @@ -3898,7 +3903,7 @@ && tryRadixSort(sorter, a, low, high)) { /* * Dutch National Flag partitioning * - * left part central part right part + * left part central part right part * +------------------------------------------------------+ * | < pivot | ? | == pivot | > pivot | * +------------------------------------------------------+ @@ -3906,13 +3911,15 @@ && tryRadixSort(sorter, a, low, high)) { * | | | * lower k upper * - * Invariants + * 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 + * + * Invariants: * * all in (low, lower] < pivot * all in (k, upper) == pivot * all in [upper, end] > pivot - * - * Pointer k is the last index of ?-part */ for (int k = ++upper; --k > lower; ) { double ak = a[k]; @@ -3956,8 +3963,8 @@ && tryRadixSort(sorter, a, low, high)) { /** * 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 @@ -3971,96 +3978,81 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ private static void mixedInsertionSort(double[] a, int low, int end, int high) { - if (end == high) { + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end && low < p; ) { + double ai = a[i = low]; /* - * Invoke simple insertion sort on tiny array. + * Find pin element, smaller than the given element. */ - for (int i; ++low < high; ) { - double ai = a[i = low]; + while (ai < a[--p]); - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; + /* + * Swap these elements. + */ + ai = a[p]; a[p] = a[i]; + + /* + * Insert element into sorted part. + */ + while (ai < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = ai; + } - } else { + /* + * Continue with simple insertion sort. + */ + for (int i; low < end; ++low) { + double ai = a[i = low]; /* - * Start with pin insertion sort on small part. + * Insert element into sorted part. */ - double pin = a[end]; - - for (int i, p = high; ++low < end; ) { - double ai = a[i = low]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } - /* - * Put elements larger than an element called pin - * to the end of array (the proper area for them). - * It avoids expensive movements of these elements - * through the whole array. - */ - if (p > i && ai > pin) { // Element, larger than pin + /* + * Finish with pair insertion sort. + */ + for (int i; low < high; ++low) { + double a1 = a[i = low], a2 = a[++low]; - /* - * Find element, smaller than pin. - */ - while (a[--p] > pin); + /* + * 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) { - /* - * Swap it with larger element. - */ - if (p > i) { - ai = a[p]; - a[p] = a[i]; - } + while (a1 < a[--i]) { + a[i + 2] = a[i]; } + a[++i + 1] = a1; - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { + while (a2 < a[--i]) { a[i + 1] = a[i]; } - a[i + 1] = ai; - } - - /* - * Continue with pair insertion sort on remain part. - */ - 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) { - - while (a1 < a[--i]) { - a[i + 2] = a[i]; - } - a[++i + 1] = a1; + 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; } } } @@ -4071,13 +4063,25 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { * @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 + * @param leftmost indicates that the range is the leftmost part */ - private static void insertionSort(double[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - double ai = a[i = k]; + private static void insertionSort(double[] a, int low, int high, boolean leftmost) { + if (leftmost) { + for (int i, k = low; ++k < high; ) { + double ai = a[i = k]; - if (ai < a[i - 1]) { - while (--i >= low && ai < a[i]) { + if (ai < a[i - 1]) { + while (--i >= low && ai < a[i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; + } + } + } else { + for (int i; ++low < high; ) { + double ai = a[i = low]; + + while (ai < a[--i]) { a[i + 1] = a[i]; } a[i + 1] = ai; From 8a373741c149bc0c60811a62b728786f9a61242e Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Mon, 14 Mar 2022 22:08:16 +0300 Subject: [PATCH 12/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Improved mixed insertion sort * Optimized insertion sort * Improved merging sort * Optimized soring tests --- .../classes/java/util/DualPivotQuicksort.java | 2460 ++++++++--------- test/jdk/java/util/Arrays/Sorting.java | 683 ++--- .../java.base/java/util/SortingHelper.java | 234 +- 3 files changed, 1534 insertions(+), 1843 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index bf713959e3e76..2369cc1e2cfcb 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2022, 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 @@ -56,50 +56,48 @@ 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 = 113; + 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 = 26; + private static final int MAX_INSERTION_SORT_SIZE = 20; - /** - * 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 use merging sort. */ - private static final int MIN_MERGING_SORT_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. + * Min number of runs for parallel merging. */ - private static final int MIN_FIRST_RUNS_FACTOR = 7; + private static final int MIN_PARALLEL_RUN_MERGING_COUNT = 4; /** - * Max capacity of the index array for tracking runs. + * Min array size to invoke parallel merging of parts. */ - private static final int MAX_RUN_CAPACITY = 5 << 10; + private static final int MIN_PARALLEL_PART_MERGING_SIZE = 4 << 10; - /** - * Min number of runs, required by parallel merging. - */ - private static final int MIN_RUN_COUNT = 4; + /* ------------------ Radix sort section ------------------ */ /** - * Min array size to use parallel merging of parts. + * Min array size to use Radix sort. */ - private static final int MIN_PARALLEL_MERGE_PARTS_SIZE = 4 << 10; + private static final int MIN_RADIX_SORT_SIZE = 768; + + /* ------------------ Counting sort section --------------- */ /** * Min size of a byte array to use counting sort. @@ -111,29 +109,21 @@ private DualPivotQuicksort() {} */ private static final int MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE = 1750; - /** - * Min array size to use Radix sort. - */ - private static final int MIN_RADIX_SORT_SIZE = 6 << 10; - - /** - * Threshold of mixed insertion sort is increased by this value. - */ - private static final int DEPTH = 3 << 1; + /* -------------------- Common section -------------------- */ /** - * Min depth to invoke Radix sort. + * Max recursive depth before switching to heap sort. */ - private static final int MIN_RADIX_SORT_DEPTH = DEPTH << 2; + private static final int MAX_RECURSION_DEPTH = 64 << 1; /** - * Max recursive partitioning depth before using heap sort. + * Min array size to perform sorting in parallel. */ - private static final int MAX_RECURSION_DEPTH = 64 * DEPTH; + private static final int MIN_PARALLEL_SORT_SIZE = 4 << 10; /** - * Max length of additional buffer, - * limited by max_heap / 64 or 256m elements (2gb max). + * Max length of additional buffer, limited by + * max_heap / 64 or 256mb elements (2gb max). */ private static final int MAX_BUFFER_LENGTH = (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, 256L << 20); @@ -176,10 +166,10 @@ static void sort(Sorter sorter, int[] a, int bits, int low, int high) { int end = high - 1, 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) { - mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); + mixedInsertionSort(a, low, high); return; } @@ -187,27 +177,18 @@ 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) { - insertionSort(a, low, high, (bits & 1) == 0); + insertionSort(a, low, high); 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_MERGING_SORT_SIZE && (bits & 1) > 0) + if (size > MIN_MERGING_SORT_SIZE * bits && tryMergingSort(sorter, a, low, size)) { return; } - /* - * Switch to heap sort, if execution time is quadratic. - */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); - return; - } - /* * Use an inexpensive approximation of the golden ratio * to select five sample elements and determine pivots. @@ -228,7 +209,7 @@ && tryMergingSort(sorter, a, low, size)) { int a3 = a[e3]; boolean isRandom = - a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]; /* * Sort these elements in place by the combination @@ -269,13 +250,21 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data, * taking into account parallel context. */ - if (size > MIN_RADIX_SORT_SIZE - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) && 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; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -461,10 +450,10 @@ && tryRadixSort(sorter, a, low, high)) { * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted - * @param end the index of the last element for simple insertion sort * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(int[] a, int low, int end, int high) { + static void mixedInsertionSort(int[] a, int low, int high) { + int end = high - (((high - low) >> 4) << 3); /* * Start with pin insertion sort. @@ -550,202 +539,28 @@ private static void mixedInsertionSort(int[] a, int low, int end, int high) { * @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 - * @param leftmost indicates that the range is the leftmost part - */ - private static void insertionSort(int[] a, int low, int high, boolean leftmost) { - if (leftmost) { - 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; - } - } - } else { - for (int i; ++low < high; ) { - int ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Tries to sort the specified range of the array - * using LSD (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, int[] a, int low, int high) { - int[] b; int offset = low, size = high - low; - - /* - * Allocate additional buffer. - */ - if (sorter != null && (b = (int[]) sorter.b) != null) { - offset = sorter.offset; - } else { - if ((b = (int[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[a[i] & 0x3FF]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(b[i] >>> 10) & 0x7FF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(a[i] >>> 10) & 0x7FF]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(b[i] >>> 21) ^ 0x400]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 creates histogram. - * - * @param count the count array - * @param last the last index of count array - * @param total the total number of elements - * @param high the index of the last element, exclusive - * @return {@code true} if the digit must be processed, otherwise {@code false} */ - private static boolean processDigit(int[] count, int last, int total, int high) { + static void insertionSort(int[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + int ai = a[i = k]; - /* - * Check if we can skip given digit. - */ - for (int c : count) { - if (c == total) { - return false; - } - if (c < 0) { - break; - } - } + if (ai < a[low]) { - /* - * Compute the histogram. - */ - count[last] += high; + do { + a[i] = a[i - 1]; + } while (--i > low); - for (int i = last; i > 0; --i) { - count[i - 1] += count[i]; - } - return true; - } + a[low] = 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; - } - } + } else if (ai < a[i - 1]) { - /** - * 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 - 1]; + } while (ai < a[--i]); - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + a[i + 1] = ai; } } - a[p] = value; } /** @@ -757,12 +572,11 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { * @param size the array size * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { /* - * 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. + * Element run[i] holds the start index of + * i-th sub-sequence in non-descending order. */ int[] run = null; int high = low + size; @@ -774,7 +588,7 @@ private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) for (int k = low + 1; k < high; ) { /* - * Find the end index of the current run. + * Find the end of the current run. */ if (a[k - 1] < a[k]) { @@ -799,75 +613,59 @@ private static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) } /* - * Check special cases. + * Check if the runs are too + * long to continue scanning. + */ + if (count > 1 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { + 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; - } - - // Initial min 127, max 1023, extended to 5120 - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[(size >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { // Start new run - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } + } else if (a[last - 1] > a[last]) { // Start the new run - if (++count == MAX_RUN_CAPACITY) { + if (++count == run.length) { /* * Array is not highly structured. */ return false; } - - if (count == run.length) { - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } } + run[count] = (last = k); + /* + * Check single-element run at the end. + */ if (++k == high) { - /* - * This is single-element run at the end. - */ --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) { offset = sorter.offset; - } else { - if ((b = (int[]) tryAllocate(a, size)) == null) { - return false; - } + } else if ((b = (int[]) tryAllocate(a, size)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -909,7 +707,7 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, */ int[] a1, a2; - if (parallel && hi - lo > MIN_RUN_COUNT) { + if (parallel && hi - lo > MIN_PARALLEL_RUN_MERGING_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(); @@ -965,7 +763,7 @@ private static void mergeParts(Merger merger, int[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { break; } @@ -1025,27 +823,197 @@ 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. - * - * 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. + * Tries to sort the specified range of the array + * using LSD (Least Significant Digit) Radix sort. * * @param a the array to be sorted - * @param parallelism the parallelism level * @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(long[] a, int parallelism, int low, int high) { - if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); - } else { + 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 = (int[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (int[]) tryAllocate(a, 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, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = low; i < high; ++i) { + b[count1[a[i] & 0x3FF]++ - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = start; i < last; ++i) { + a[count2[(b[i] >>> 10) & 0x7FF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(a[i] >>> 10) & 0x7FF]++ - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = start; i < last; ++i) { + a[count3[(b[i] >>> 21) ^ 0x400]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 creates histogram. + * + * @param count the count array + * @param last the last index of count array + * @param total the total number of elements + * @param high the index of the last element, exclusive + * @return {@code true} if the digit must be processed, otherwise {@code false} + */ + private static boolean processDigit(int[] count, int last, int total, int high) { + + /* + * Check if we can skip given digit. + */ + for (int c : count) { + if (c == total) { + return false; + } + if (c < 0) { + break; + } + } + + /* + * Compute the histogram. + */ + count[last] += high; + + for (int i = last; i > 0; --i) { + count[i - 1] += count[i]; + } + 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. + * + * @param a the array to be sorted + * @param parallelism the parallelism level + * @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(long[] a, int parallelism, int low, int high) { + 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); } } @@ -1065,10 +1033,10 @@ static void sort(Sorter sorter, long[] a, int bits, int low, int high) { int end = high - 1, 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) { - mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); + mixedInsertionSort(a, low, high); return; } @@ -1076,27 +1044,18 @@ 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) { - insertionSort(a, low, high, (bits & 1) == 0); + insertionSort(a, low, high); 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_MERGING_SORT_SIZE && (bits & 1) > 0) + if (size > MIN_MERGING_SORT_SIZE * bits && tryMergingSort(sorter, a, low, size)) { return; } - /* - * Switch to heap sort, if execution time is quadratic. - */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); - return; - } - /* * Use an inexpensive approximation of the golden ratio * to select five sample elements and determine pivots. @@ -1117,7 +1076,7 @@ && tryMergingSort(sorter, a, low, size)) { long a3 = a[e3]; boolean isRandom = - a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]; /* * Sort these elements in place by the combination @@ -1158,13 +1117,21 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data, * taking into account parallel context. */ - if (size > MIN_RADIX_SORT_SIZE - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) && 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; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -1350,10 +1317,10 @@ && tryRadixSort(sorter, a, low, high)) { * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted - * @param end the index of the last element for simple insertion sort * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(long[] a, int low, int end, int high) { + static void mixedInsertionSort(long[] a, int low, int high) { + int end = high - (((high - low) >> 4) << 3); /* * Start with pin insertion sort. @@ -1439,253 +1406,58 @@ private static void mixedInsertionSort(long[] a, int low, int end, int high) { * @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 - * @param leftmost indicates that the range is the leftmost part */ - private static void insertionSort(long[] a, int low, int high, boolean leftmost) { - if (leftmost) { - 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; - } - } - } else { - for (int i; ++low < high; ) { - long ai = a[i = low]; + static void insertionSort(long[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + long ai = a[i = k]; + + if (ai < a[low]) { + + do { + a[i] = a[i - 1]; + } while (--i > low); + + a[low] = ai; + + } else if (ai < a[i - 1]) { + + do { + a[i] = a[i - 1]; + } while (ai < a[--i]); - while (ai < a[--i]) { - a[i + 1] = a[i]; - } a[i + 1] = ai; } } } /** - * Tries to sort the specified range of the array - * using LSD (Least Significant Digit) Radix sort. + * 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, inclusive, to be sorted - * @param high the index of the last element, exclusive, to be sorted + * @param low the index of the first element to be sorted + * @param size the array size * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { - long[] b; int offset = low, size = high - low; + static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { /* - * Allocate additional buffer. + * Element run[i] holds the start index of + * i-th sub-sequence in non-descending order. */ - if (sorter != null && (b = (long[]) sorter.b) != null) { - offset = sorter.offset; - } else { - if ((b = (long[]) tryAllocate(a, size)) == null) { - return false; - } - } - - int start = low - offset; - int last = high - offset; + int[] run = null; + int high = low + size; + int count = 1, last = low; /* - * Count the number of all digits. + * Identify all possible runs. */ - 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 k = low + 1; k < high; ) { - 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - boolean processDigit4 = processDigit(count4, 2047, -size, high); - boolean processDigit5 = processDigit(count5, 2047, -size, high); - boolean processDigit6 = processDigit(count6, 1023, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[(int) (a[i] & 0x3FF)]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(int) ((b[i] >>> 10) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(int) ((a[i] >>> 10) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(int) ((b[i] >>> 21) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count3[(int) ((a[i] >>> 21) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 4-th digit. - */ - if (processDigit4) { - if (processDigit1 ^ processDigit2 ^ processDigit3) { - for (int i = start; i < last; ++i) { - a[count4[(int) ((b[i] >>> 32) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count5[(int) ((b[i] >>> 43) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count6[(int) ((b[i] >>> 54) ^ 0x200)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - - /** - * 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 {@code true} if the array is finally sorted, otherwise {@code false} - */ - private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { - - /* - * 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. - */ - int[] run = null; - int high = low + size; - int count = 1, last = low; - - /* - * Identify all possible runs. - */ - for (int k = low + 1; k < high; ) { - - /* - * Find the end index of the current run. - */ - if (a[k - 1] < a[k]) { + /* + * Find the end of the current run. + */ + if (a[k - 1] < a[k]) { // Identify ascending sequence while (++k < high && a[k - 1] <= a[k]); @@ -1708,75 +1480,59 @@ private static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size } /* - * Check special cases. + * Check if the runs are too + * long to continue scanning. + */ + if (count > 1 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { + 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; - } - - // Initial min 127, max 1023, extended to 5120 - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[(size >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { // Start new run + } else if (a[last - 1] > a[last]) { // Start the new run - 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) { + if (++count == run.length) { /* * Array is not highly structured. */ return false; } - - if (count == run.length) { - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } } + run[count] = (last = k); + /* + * Check single-element run at the end. + */ if (++k == high) { - /* - * This is single-element run at the end. - */ --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) { offset = sorter.offset; - } else { - if ((b = (long[]) tryAllocate(a, size)) == null) { - return false; - } + } else if ((b = (long[]) tryAllocate(a, size)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -1818,7 +1574,7 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, */ long[] a1, a2; - if (parallel && hi - lo > MIN_RUN_COUNT) { + if (parallel && hi - lo > MIN_PARALLEL_RUN_MERGING_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(); @@ -1874,7 +1630,7 @@ private static void mergeParts(Merger merger, long[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { break; } @@ -1934,41 +1690,211 @@ 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 (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 = (long[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (long[]) tryAllocate(a, 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, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + boolean processDigit4 = processDigit(count4, 2047, -size, high); + boolean processDigit5 = processDigit(count5, 2047, -size, high); + boolean processDigit6 = processDigit(count6, 1023, -size, high); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = low; i < high; ++i) { + b[count1[(int) (a[i] & 0x3FF)]++ - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = start; i < last; ++i) { + a[count2[(int) ((b[i] >>> 10) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(int) ((a[i] >>> 10) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = start; i < last; ++i) { + a[count3[(int) ((b[i] >>> 21) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count3[(int) ((a[i] >>> 21) & 0x7FF)]++ - offset] = a[i]; + } + } } + + /* + * Process the 4-th digit. + */ + if (processDigit4) { + if (processDigit1 ^ processDigit2 ^ processDigit3) { + for (int i = start; i < last; ++i) { + a[count4[(int) ((b[i] >>> 32) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 = start; i < last; ++i) { + a[count5[(int) ((b[i] >>> 43) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 = start; i < last; ++i) { + a[count6[(int) ((b[i] >>> 54) ^ 0x200)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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] + + /** + * 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 + */ + 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); } } @@ -2022,6 +1948,36 @@ private static void countingSort(byte[] a, int low, int high) { } } + /** + * 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[low]) { + + do { + a[i] = a[i - 1]; + } while (--i > low); + + a[low] = ai; + + } else if (ai < a[i - 1]) { + + do { + a[i] = a[i - 1]; + } while (ai < a[--i]); + + a[i + 1] = ai; + } + } + } + // #[char] /** @@ -2040,6 +1996,47 @@ static void sort(char[] a, int low, int high) { } } + /** + * 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 all values. + */ + 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 c = count[i]; + + do { + a[--high] = (char) i; + } while (--c > 0); + } + } + } + /** * Sorts the specified range of the array using Dual-Pivot Quicksort. * @@ -2054,7 +2051,7 @@ static void sort(char[] a, int bits, int low, int high) { int end = high - 1, 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); @@ -2064,7 +2061,7 @@ static void sort(char[] a, int bits, int low, int high) { /* * Switch to counting sort, if execution time is quadratic. */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { + if ((bits += 2) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } @@ -2292,23 +2289,56 @@ 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]; - } + if (ai < a[low]) { + + do { + a[i] = a[i - 1]; + } while (--i > low); + + a[low] = ai; + + } else if (ai < a[i - 1]) { + + do { + a[i] = a[i - 1]; + } while (ai < a[--i]); + a[i + 1] = 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_OR_CHAR_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; + + /** + * 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. @@ -2317,53 +2347,39 @@ 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 the histogram for all values. */ - for (int i = high; i > low; ++count[a[--i]]); + for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); /* * 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 + 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 = NUM_CHAR_VALUES; high > low; ) { - while (count[--i] == 0); - int c = count[i]; + for (int i = MAX_SHORT_INDEX; high > low; ) { + while (count[--i & 0xFFFF] == 0); + + int value = i & 0xFFFF; + int c = count[value]; do { - a[--high] = (char) i; + a[--high] = (short) value; } while (--c > 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 range of the array using Dual-Pivot Quicksort. * @@ -2378,7 +2394,7 @@ static void sort(short[] a, int bits, int low, int high) { int end = high - 1, 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); @@ -2388,7 +2404,7 @@ static void sort(short[] a, int bits, int low, int high) { /* * Switch to counting sort, if execution time is quadratic. */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { + if ((bits += 2) > MAX_RECURSION_DEPTH) { countingSort(a, low, high); return; } @@ -2616,65 +2632,25 @@ 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 the histogram for all values. - */ - for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); + if (ai < a[low]) { - /* - * Put 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; + do { + a[i] = a[i - 1]; + } while (--i > low); - 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); + a[low] = ai; - int value = i & 0xFFFF; - int c = count[value]; + } else if (ai < a[i - 1]) { do { - a[--high] = (short) value; - } while (--c > 0); + a[i] = a[i - 1]; + } while (ai < a[--i]); + + a[i + 1] = ai; } } } @@ -2771,10 +2747,10 @@ static void sort(Sorter sorter, float[] a, int bits, int low, int high) { int end = high - 1, 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) { - mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); + mixedInsertionSort(a, low, high); return; } @@ -2782,27 +2758,18 @@ 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) { - insertionSort(a, low, high, (bits & 1) == 0); + insertionSort(a, low, high); 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_MERGING_SORT_SIZE && (bits & 1) > 0) + if (size > MIN_MERGING_SORT_SIZE * bits && tryMergingSort(sorter, a, low, size)) { return; } - /* - * Switch to heap sort, if execution time is quadratic. - */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); - return; - } - /* * Use an inexpensive approximation of the golden ratio * to select five sample elements and determine pivots. @@ -2823,7 +2790,7 @@ && tryMergingSort(sorter, a, low, size)) { float a3 = a[e3]; boolean isRandom = - a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]; /* * Sort these elements in place by the combination @@ -2864,13 +2831,21 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data, * taking into account parallel context. */ - if (size > MIN_RADIX_SORT_SIZE - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) && 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; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -3056,10 +3031,10 @@ && tryRadixSort(sorter, a, low, high)) { * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted - * @param end the index of the last element for simple insertion sort * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(float[] a, int low, int end, int high) { + static void mixedInsertionSort(float[] a, int low, int high) { + int end = high - (((high - low) >> 4) << 3); /* * Start with pin insertion sort. @@ -3119,205 +3094,54 @@ private static void mixedInsertionSort(float[] a, int low, int end, int high) { } a[++i + 1] = a1; - while (a2 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a2; - - } else if (a1 < a[i - 1]) { - - 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; - } - } - } - - /** - * 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 - * @param leftmost indicates that the range is the leftmost part - */ - private static void insertionSort(float[] a, int low, int high, boolean leftmost) { - if (leftmost) { - 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; - } - } - } else { - for (int i; ++low < high; ) { - float ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Tries to sort the specified range of the array - * using LSD (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 = (float[]) sorter.b) != null) { - offset = sorter.offset; - } else { - if ((b = (float[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[fti(a[i]) & 0x3FF]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(fti(b[i]) >>> 10) & 0x7FF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(fti(a[i]) >>> 10) & 0x7FF]++ - offset] = a[i]; + while (a2 < a[--i]) { + a[i + 1] = a[i]; } - } - } + a[i + 1] = a2; - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(fti(b[i]) >>> 21) & 0x7FF]++] = b[i]; + } else if (a1 < a[i - 1]) { + + while (a2 < a[--i]) { + a[i + 2] = a[i]; } - } else { - for (int i = low; i < high; ++i) { - b[count3[(fti(a[i]) >>> 21) & 0x7FF]++ - offset] = a[i]; + a[++i + 1] = a2; + + while (a1 < a[--i]) { + a[i + 1] = a[i]; } + a[i + 1] = a1; } } - - /* - * 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 number. - * - * @param f the given number - * @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. + * 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 */ - 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; - } - } + static void insertionSort(float[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + float ai = a[i = k]; - /** - * 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 (ai < a[low]) { - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + do { + a[i] = a[i - 1]; + } while (--i > low); + + a[low] = ai; + + } else if (ai < a[i - 1]) { + + do { + a[i] = a[i - 1]; + } while (ai < a[--i]); + + a[i + 1] = ai; } } - a[p] = value; } /** @@ -3329,12 +3153,11 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { * @param size the array size * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { /* - * 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. + * Element run[i] holds the start index of + * i-th sub-sequence in non-descending order. */ int[] run = null; int high = low + size; @@ -3346,7 +3169,7 @@ private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int siz for (int k = low + 1; k < high; ) { /* - * Find the end index of the current run. + * Find the end of the current run. */ if (a[k - 1] < a[k]) { @@ -3371,75 +3194,59 @@ private static boolean tryMergingSort(Sorter sorter, float[] a, int low, int siz } /* - * Check special cases. + * Check if the runs are too + * long to continue scanning. + */ + if (count > 1 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { + 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; - } - - // Initial min 127, max 1023, extended to 5120 - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[(size >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { // Start new run - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } + } else if (a[last - 1] > a[last]) { // Start the new run - if (++count == MAX_RUN_CAPACITY) { + if (++count == run.length) { /* * Array is not highly structured. */ return false; } - - if (count == run.length) { - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } } + run[count] = (last = k); + /* + * Check single-element run at the end. + */ if (++k == high) { - /* - * This is single-element run at the end. - */ --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) { offset = sorter.offset; - } else { - if ((b = (float[]) tryAllocate(a, size)) == null) { - return false; - } + } else if ((b = (float[]) tryAllocate(a, size)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -3481,7 +3288,7 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, */ float[] a1, a2; - if (parallel && hi - lo > MIN_RUN_COUNT) { + if (parallel && hi - lo > MIN_PARALLEL_RUN_MERGING_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(); @@ -3537,7 +3344,7 @@ private static void mergeParts(Merger merger, float[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { break; } @@ -3566,35 +3373,182 @@ private static void mergeParts(Merger merger, float[] dst, int k, */ int d = mi2 - lo2 + mi1 - lo1; - /* - * Merge the right sub-parts in parallel. - */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + /* + * Merge the right sub-parts in parallel. + */ + merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + + /* + * Process the sub-left parts. + */ + hi1 = mi1; + hi2 = mi2; + } + } + + /* + * Merge small parts sequentially. + */ + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } + if (dst != a1 || k < lo1) { + while (lo1 < hi1) { + dst[k++] = a1[lo1++]; + } + } + if (dst != a2 || k < lo2) { + while (lo2 < hi2) { + dst[k++] = a2[lo2++]; + } + } + } + + /** + * Tries to sort the specified range of the array + * using LSD (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 = (float[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (float[]) tryAllocate(a, 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, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = low; i < high; ++i) { + b[count1[fti(a[i]) & 0x3FF]++ - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = start; i < last; ++i) { + a[count2[(fti(b[i]) >>> 10) & 0x7FF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(fti(a[i]) >>> 10) & 0x7FF]++ - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = start; i < last; ++i) { + a[count3[(fti(b[i]) >>> 21) & 0x7FF]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 - /* - * Process the sub-left parts. - */ - hi1 = mi1; - hi2 = mi2; + if (k > high) { + break; } - } - - /* - * Merge small parts sequentially. - */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; - } - if (dst != a1 || k < lo1) { - while (lo1 < hi1) { - dst[k++] = a1[lo1++]; + if (k == high || a[k] < a[k - 1]) { + --k; } - } - if (dst != a2 || k < lo2) { - while (lo2 < hi2) { - dst[k++] = a2[lo2++]; + if (a[k] <= value) { + break; } } + a[p] = value; } // #[double] @@ -3689,10 +3643,10 @@ static void sort(Sorter sorter, double[] a, int bits, int low, int high) { int end = high - 1, 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) { - mixedInsertionSort(a, low, high - ((size >> 2) << 1), high); + mixedInsertionSort(a, low, high); return; } @@ -3700,27 +3654,18 @@ 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) { - insertionSort(a, low, high, (bits & 1) == 0); + insertionSort(a, low, high); 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_MERGING_SORT_SIZE && (bits & 1) > 0) + if (size > MIN_MERGING_SORT_SIZE * bits && tryMergingSort(sorter, a, low, size)) { return; } - /* - * Switch to heap sort, if execution time is quadratic. - */ - if ((bits += DEPTH) > MAX_RECURSION_DEPTH) { - heapSort(a, low, high); - return; - } - /* * Use an inexpensive approximation of the golden ratio * to select five sample elements and determine pivots. @@ -3741,7 +3686,7 @@ && tryMergingSort(sorter, a, low, size)) { double a3 = a[e3]; boolean isRandom = - a[e1] > a[e2] || a[e2] > a[e3] || a[e3] > a[e4] || a[e4] > a[e5]; + a[e1] > a[e2] || a[e2] > a3 || a3 > a[e4] || a[e4] > a[e5]; /* * Sort these elements in place by the combination @@ -3782,13 +3727,21 @@ && tryMergingSort(sorter, a, low, size)) { * Try Radix sort on large fully random data, * taking into account parallel context. */ - if (size > MIN_RADIX_SORT_SIZE - && a[e1] < a[e2] && a[e2] < a[e4] && a[e4] < a[e5] && isRandom - && (sorter == null || bits > MIN_RADIX_SORT_DEPTH) + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) && 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; + } + // Pointers int lower = low; // The index of the last element of the left part int upper = end; // The index of the first element of the right part @@ -3974,10 +3927,10 @@ && tryRadixSort(sorter, a, low, high)) { * * @param a the array to be sorted * @param low the index of the first element, inclusive, to be sorted - * @param end the index of the last element for simple insertion sort * @param high the index of the last element, exclusive, to be sorted */ - private static void mixedInsertionSort(double[] a, int low, int end, int high) { + static void mixedInsertionSort(double[] a, int low, int high) { + int end = high - (((high - low) >> 4) << 3); /* * Start with pin insertion sort. @@ -4050,246 +4003,41 @@ private static void mixedInsertionSort(double[] a, int low, int end, int high) { a[++i + 1] = a2; while (a1 < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = a1; - } - } - } - - /** - * 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 - * @param leftmost indicates that the range is the leftmost part - */ - private static void insertionSort(double[] a, int low, int high, boolean leftmost) { - if (leftmost) { - 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; - } - } - } else { - for (int i; ++low < high; ) { - double ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - } - } - - /** - * Tries to sort the specified range of the array - * using LSD (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 = (double[]) sorter.b) != null) { - offset = sorter.offset; - } else { - if ((b = (double[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - boolean processDigit4 = processDigit(count4, 2047, -size, high); - boolean processDigit5 = processDigit(count5, 2047, -size, high); - boolean processDigit6 = processDigit(count6, 1023, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[(int) (dtl(a[i]) & 0x3FF)]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(int) ((dtl(b[i]) >>> 10) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(int) ((dtl(a[i]) >>> 10) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(int) ((dtl(b[i]) >>> 21) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count4[(int) ((dtl(b[i]) >>> 32) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count5[(int) ((dtl(b[i]) >>> 43) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count6[(int) ((dtl(b[i]) >>> 54) & 0x3FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 number. - * - * @param f the given number - * @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 - */ - 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; + a[i + 1] = a[i]; + } + a[i + 1] = a1; + } } } /** - * Pushes specified element down during heap sort. + * Sorts the specified range of the array using insertion sort. * - * @param a the given array - * @param p the start index - * @param value the given element + * @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 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 + static void insertionSort(double[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + double ai = a[i = k]; - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; + if (ai < a[low]) { + + do { + a[i] = a[i - 1]; + } while (--i > low); + + a[low] = ai; + + } else if (ai < a[i - 1]) { + + do { + a[i] = a[i - 1]; + } while (ai < a[--i]); + + a[i + 1] = ai; } } - a[p] = value; } /** @@ -4301,12 +4049,11 @@ private static void pushDown(double[] a, int p, double value, int low, int high) * @param size the array size * @return {@code true} if the array is finally sorted, otherwise {@code false} */ - private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { /* - * 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. + * Element run[i] holds the start index of + * i-th sub-sequence in non-descending order. */ int[] run = null; int high = low + size; @@ -4318,7 +4065,7 @@ private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int si for (int k = low + 1; k < high; ) { /* - * Find the end index of the current run. + * Find the end of the current run. */ if (a[k - 1] < a[k]) { @@ -4343,75 +4090,59 @@ private static boolean tryMergingSort(Sorter sorter, double[] a, int low, int si } /* - * Check special cases. + * Check if the runs are too + * long to continue scanning. + */ + if (count > 1 && k - low < count * MIN_RUN_SIZE) { + return false; + } + + /* + * Process the run. */ if (run == null) { + 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; - } - - // Initial min 127, max 1023, extended to 5120 - run = new int[((size >> 10) | 0x7F) & 0x3FF]; + run = new int[(size >> 9) & 0x1FF | 0x3F]; run[0] = low; - } else if (a[last - 1] > a[last]) { // Start new run - - if (count > (k - low) >> MIN_FIRST_RUNS_FACTOR) { - /* - * The first runs are not long - * enough to continue scanning. - */ - return false; - } + } else if (a[last - 1] > a[last]) { // Start the new run - if (++count == MAX_RUN_CAPACITY) { + if (++count == run.length) { /* * Array is not highly structured. */ return false; } - - if (count == run.length) { - /* - * Increase capacity of index array. - */ - run = Arrays.copyOf(run, count << 1); - } } + run[count] = (last = k); + /* + * Check single-element run at the end. + */ if (++k == high) { - /* - * This is single-element run at the end. - */ --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) { offset = sorter.offset; - } else { - if ((b = (double[]) tryAllocate(a, size)) == null) { - return false; - } + } else if ((b = (double[]) tryAllocate(a, size)) == null) { + return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); } @@ -4453,7 +4184,7 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, */ double[] a1, a2; - if (parallel && hi - lo > MIN_RUN_COUNT) { + if (parallel && hi - lo > MIN_PARALLEL_RUN_MERGING_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(); @@ -4509,7 +4240,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_MERGE_PARTS_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { break; } @@ -4569,6 +4300,207 @@ private static void mergeParts(Merger merger, double[] dst, int k, } } + /** + * Tries to sort the specified range of the array + * using LSD (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 = (double[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (double[]) tryAllocate(a, 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, 1023, -size, high); + boolean processDigit2 = processDigit(count2, 2047, -size, high); + boolean processDigit3 = processDigit(count3, 2047, -size, high); + boolean processDigit4 = processDigit(count4, 2047, -size, high); + boolean processDigit5 = processDigit(count5, 2047, -size, high); + boolean processDigit6 = processDigit(count6, 1023, -size, high); + + /* + * Process the 1-st digit. + */ + if (processDigit1) { + for (int i = low; i < high; ++i) { + b[count1[(int) (dtl(a[i]) & 0x3FF)]++ - offset] = a[i]; + } + } + + /* + * Process the 2-nd digit. + */ + if (processDigit2) { + if (processDigit1) { + for (int i = start; i < last; ++i) { + a[count2[(int) ((dtl(b[i]) >>> 10) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + b[count2[(int) ((dtl(a[i]) >>> 10) & 0x7FF)]++ - offset] = a[i]; + } + } + } + + /* + * Process the 3-rd digit. + */ + if (processDigit3) { + if (processDigit1 ^ processDigit2) { + for (int i = start; i < last; ++i) { + a[count3[(int) ((dtl(b[i]) >>> 21) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 = start; i < last; ++i) { + a[count4[(int) ((dtl(b[i]) >>> 32) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 = start; i < last; ++i) { + a[count5[(int) ((dtl(b[i]) >>> 43) & 0x7FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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 = start; i < last; ++i) { + a[count6[(int) ((dtl(b[i]) >>> 54) & 0x3FF)]++] = b[i]; + } + } else { + for (int i = low; i < high; ++i) { + 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] /** @@ -4590,7 +4522,7 @@ private Sorter(Object a, int parallelism, int low, int size, int depth) { while ((parallelism >>= 1) > 0 && (size >>= 8) > 0) { depth -= 2; } - this.b = depth == 0 ? null : tryAllocate(a, this.size); + this.b = tryAllocate(a, this.size); this.depth = b == null ? 0 : depth; } @@ -4606,7 +4538,7 @@ private Sorter(CountedCompleter parent, } @Override - public final void compute() { + public void compute() { if (depth < 0) { setPendingCount(2); int half = size >> 1; @@ -4629,7 +4561,7 @@ public final void compute() { } @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; @@ -4677,7 +4609,7 @@ private Merger(CountedCompleter parent, Object dst, int k, } @Override - public final void compute() { + public void compute() { if (dst instanceof int[]) { mergeParts(this, (int[]) dst, k, (int[]) a1, lo1, hi1, (int[]) a2, lo2, hi2); @@ -4726,7 +4658,7 @@ private RunMerger(Object a, Object b, int offset, } @Override - protected final Object compute() { + protected Object compute() { if (a instanceof int[]) { return mergeRuns((int[]) a, (int[]) b, offset, aim, true, run, lo, hi); } diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index 582bc745c58fb..3c8b5c4f848ef 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 2022, 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 @@ -35,7 +35,7 @@ */ import java.io.PrintStream; -import java.util.Comparator; +import java.util.Arrays; import java.util.Random; import java.util.SortingHelper; @@ -44,22 +44,22 @@ public class Sorting { private static final PrintStream out = System.out; private static final PrintStream err = System.err; - // Lengths of arrays for 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, 8, 55, 100, 10_000 }; + { 1, 2, 14, 100, 500, 1_000, 10_000 }; - // Initial random values for long run (default) - private static final TestRandom[] LONG_RUN_RANDOMS = - { TestRandom.DEDA, TestRandom.BABA, TestRandom.C0FFEE }; + // 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 }; // Initial random values for 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 }; + // Constant to fill the left part of array private static final int A380 = 0xA380; @@ -69,6 +69,7 @@ public class Sorting { private final SortingHelper sortingHelper; private final TestRandom[] randoms; private final int[] lengths; + private final boolean fix; private Object[] gold; private Object[] test; @@ -79,104 +80,114 @@ 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.RADIX_SORT, randoms, lengths).testCore(); 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) { - testSubArray(length, random); - testMergingSort(length, random); - testNegativeZero(length, random); - testFloatingPointSorting(length, random); + for (TestRandom random : randoms) { + testNegativeZero(length, random); + testFloatingPointSorting(length, random); + } } } private void testAll() { + testCore(); + testNullArray(); + for (int length : lengths) { createData(length); - testAll(length); + testRange(length); } } - private void testAll(int length) { - testCore(length); - - for (TestRandom random : randoms) { - testStability(length, random); - testOutOfBounds(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(); - } + sortingHelper.sort(new int[] {}); + sortingHelper.sort(new int[] {}, 0, 0); - private void testStability(int length, TestRandom random) { - printTestName("Test stability", random, length); + sortingHelper.sort(new long[] {}); + sortingHelper.sort(new long[] {}, 0, 0); - Pair[] a = build(length, random); - sortingHelper.sort(a); - checkSorted(a); - checkStable(a); + sortingHelper.sort(new byte[] {}); + sortingHelper.sort(new byte[] {}, 0, 0); - a = build(length, random); - sortingHelper.sort(a, pairComparator); - checkSorted(a); - checkStable(a); + sortingHelper.sort(new char[] {}); + sortingHelper.sort(new char[] {}, 0, 0); - out.println(); + sortingHelper.sort(new short[] {}); + sortingHelper.sort(new short[] {}, 0, 0); + + sortingHelper.sort(new float[] {}); + sortingHelper.sort(new float[] {}, 0, 0); + + sortingHelper.sort(new double[] {}); + sortingHelper.sort(new double[] {}, 0, 0); } - private void testEmptyAndNullIntArray() { - sortingHelper.sort(new int[] {}); - sortingHelper.sort(new int[] {}, 0, 0); + private void testNullArray() { + testNullIntArray(); + testNullLongArray(); + testNullByteArray(); + testNullCharArray(); + testNullShortArray(); + testNullFloatArray(); + testNullDoubleArray(); + } + private void testNullIntArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -191,10 +202,7 @@ private void testEmptyAndNullIntArray() { fail(sortingHelper + "(int[]) shouldn't catch null array"); } - private void testEmptyAndNullLongArray() { - sortingHelper.sort(new long[] {}); - sortingHelper.sort(new long[] {}, 0, 0); - + private void testNullLongArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -209,10 +217,7 @@ private void testEmptyAndNullLongArray() { fail(sortingHelper + "(long[]) shouldn't catch null array"); } - private void testEmptyAndNullByteArray() { - sortingHelper.sort(new byte[] {}); - sortingHelper.sort(new byte[] {}, 0, 0); - + private void testNullByteArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -227,10 +232,7 @@ private void testEmptyAndNullByteArray() { fail(sortingHelper + "(byte[]) shouldn't catch null array"); } - private void testEmptyAndNullCharArray() { - sortingHelper.sort(new char[] {}); - sortingHelper.sort(new char[] {}, 0, 0); - + private void testNullCharArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -245,10 +247,7 @@ private void testEmptyAndNullCharArray() { fail(sortingHelper + "(char[]) shouldn't catch null array"); } - private void testEmptyAndNullShortArray() { - sortingHelper.sort(new short[] {}); - sortingHelper.sort(new short[] {}, 0, 0); - + private void testNullShortArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -263,10 +262,7 @@ private void testEmptyAndNullShortArray() { fail(sortingHelper + "(short[]) shouldn't catch null array"); } - private void testEmptyAndNullFloatArray() { - sortingHelper.sort(new float[] {}); - sortingHelper.sort(new float[] {}, 0, 0); - + private void testNullFloatArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -281,10 +277,7 @@ private void testEmptyAndNullFloatArray() { fail(sortingHelper + "(float[]) shouldn't catch null array"); } - private void testEmptyAndNullDoubleArray() { - sortingHelper.sort(new double[] {}); - sortingHelper.sort(new double[] {}, 0, 0); - + private void testNullDoubleArray() { try { sortingHelper.sort(null); } catch (NullPointerException expected) { @@ -299,31 +292,27 @@ private void testEmptyAndNullDoubleArray() { 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, + printTestName("Test subarray", length, ", m = " + m + ", " + getType(i)); - sortingHelper.sort(test[i], fromIndex, toIndex); - checkSubArray(test[i], fromIndex, toIndex); + sortingHelper.sort(test[i], m, toIndex); + checkSubArray(test[i], m, toIndex); } } out.println(); } - private void testOutOfBounds(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; @@ -331,61 +320,16 @@ private void testOutOfBounds(int length, TestRandom random) { convertData(length); for (int i = 0; i < test.length; ++i) { - printTestName("Test range check", random, length, + printTestName("Test range check", length, ", m = " + m + ", " + getType(i)); - checkOutOfBounds(test[i], m); + 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) { @@ -398,6 +342,7 @@ private void testWithInsertionSort(int length, TestRandom random) { ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); sortByInsertionSort(gold[i]); + checkSorted(gold[i]); compare(test[i], gold[i]); } } @@ -405,19 +350,17 @@ 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, + printTestName("Test structured", length, ", m = " + m + ", " + getType(i) + " " + builder); sortingHelper.sort(test[i]); checkSorted(test[i]); @@ -428,6 +371,9 @@ 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); @@ -445,6 +391,9 @@ 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); @@ -507,20 +456,19 @@ private void testFloatingPointSorting(int length, TestRandom random) { } } for (int m = MAX; m > 4; --m) { - int t = length / m; - int g = t, z = t, n = t, p = t; - int a = length - g - z - n - p - s; + int g = length / m; + int a = length - g - g - g - g - s; 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 = " + g + + ", n = " + g + ", p = " + g + ", " + getType(i)); FloatingPointBuilder builder = FloatingPointBuilder.values() [i - 5]; - builder.build(gold[i], a, g, z, n, p, random); + 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 + 2, g); + compare(test[i], gold[i], a, g + 2, g); } } out.println(); @@ -562,7 +510,7 @@ 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); } } @@ -641,6 +589,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"); @@ -652,7 +604,7 @@ private void checkNegativeZero(Object a) { } else if (a instanceof double[]) { checkNegativeZero((double[]) a); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } @@ -678,7 +630,7 @@ 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); } } @@ -740,7 +692,7 @@ 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); } } @@ -824,7 +776,7 @@ private String getType(int i) { if (a instanceof double[]) { return "DOUBLE"; } - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); return null; } @@ -844,7 +796,7 @@ private void checkSorted(Object a) { } else if (a instanceof double[]) { checkSorted((double[]) a); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } @@ -935,7 +887,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; } @@ -963,7 +915,7 @@ private int checkSumXor(byte[] a) { for (byte e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(char[] a) { @@ -972,7 +924,7 @@ private int checkSumXor(char[] a) { for (char e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(short[] a) { @@ -981,7 +933,7 @@ private int checkSumXor(short[] a) { for (short e : a) { checkSum ^= e; } - return (int) checkSum; + return checkSum; } private int checkSumXor(float[] a) { @@ -1024,7 +976,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; } @@ -1052,7 +1004,7 @@ private int checkSumPlus(byte[] a) { for (byte e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(char[] a) { @@ -1061,7 +1013,7 @@ private int checkSumPlus(char[] a) { for (char e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(short[] a) { @@ -1070,7 +1022,7 @@ private int checkSumPlus(short[] a) { for (short e : a) { checkSum += e; } - return (int) checkSum; + return checkSum; } private int checkSumPlus(float[] a) { @@ -1092,100 +1044,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) { @@ -1204,7 +1063,7 @@ 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); } } @@ -1348,27 +1207,27 @@ private void checkSubArray(double[] a, int fromIndex, int toIndex) { } } - private void checkOutOfBounds(Object a, int m) { + private void checkRange(Object a, int m) { if (a instanceof int[]) { - checkOutOfBounds((int[]) a, m); + checkRange((int[]) a, m); } else if (a instanceof long[]) { - checkOutOfBounds((long[]) a, m); + checkRange((long[]) a, m); } else if (a instanceof byte[]) { - checkOutOfBounds((byte[]) a, m); + checkRange((byte[]) a, m); } else if (a instanceof char[]) { - checkOutOfBounds((char[]) a, m); + checkRange((char[]) a, m); } else if (a instanceof short[]) { - checkOutOfBounds((short[]) a, m); + checkRange((short[]) a, m); } else if (a instanceof float[]) { - checkOutOfBounds((float[]) a, m); + checkRange((float[]) a, m); } else if (a instanceof double[]) { - checkOutOfBounds((double[]) a, m); + checkRange((double[]) a, m); } else { - fail("Unknown type of array: " + a.getClass().getName()); + fail(a); } } - private void checkOutOfBounds(int[] a, int m) { + private void checkRange(int[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1388,7 +1247,7 @@ private void checkOutOfBounds(int[] a, int m) { } } - private void checkOutOfBounds(long[] a, int m) { + private void checkRange(long[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1408,7 +1267,7 @@ private void checkOutOfBounds(long[] a, int m) { } } - private void checkOutOfBounds(byte[] a, int m) { + private void checkRange(byte[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1428,7 +1287,7 @@ private void checkOutOfBounds(byte[] a, int m) { } } - private void checkOutOfBounds(char[] a, int m) { + private void checkRange(char[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1448,7 +1307,7 @@ private void checkOutOfBounds(char[] a, int m) { } } - private void checkOutOfBounds(short[] a, int m) { + private void checkRange(short[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1468,7 +1327,7 @@ private void checkOutOfBounds(short[] a, int m) { } } - private void checkOutOfBounds(float[] a, int m) { + private void checkRange(float[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1488,7 +1347,7 @@ private void checkOutOfBounds(float[] a, int m) { } } - private void checkOutOfBounds(double[] a, int m) { + private void checkRange(double[] a, int m) { try { sortingHelper.sort(a, m + 1, m); fail(sortingHelper + " does not throw IllegalArgumentException " + @@ -1514,7 +1373,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); } } @@ -1526,10 +1385,6 @@ 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], @@ -1545,9 +1400,9 @@ private void createData(int 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) { @@ -1559,77 +1414,108 @@ 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); } - private static enum TypeConverter { + private enum TypeConverter { + + INT { + void convert(int[] src, Object dst, boolean fix) { + if (fix) { + src[0] = Integer.MIN_VALUE; + } + } + }, LONG { - void convert(int[] src, Object dst) { + 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]; + b[i] = src[i]; + } + if (fix) { + b[0] = Long.MIN_VALUE; } } }, BYTE { - void convert(int[] src, Object dst) { + void convert(int[] src, Object dst, boolean fix) { byte[] b = (byte[]) dst; 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) { + void convert(int[] src, Object dst, boolean fix) { char[] b = (char[]) dst; 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) { + void convert(int[] src, Object dst, boolean fix) { short[] b = (short[]) dst; 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) { + void convert(int[] src, Object dst, boolean fix) { float[] b = (float[]) dst; 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) { + 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]; + 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 { void build(int[] a, int m) { for (int i = 0; i < m; ++i) { @@ -1645,7 +1531,7 @@ void build(int[] a, int m) { abstract void build(int[] a, int m); } - private static enum UnsortedBuilder { + private enum UnsortedBuilder { RANDOM { void build(int[] a, int m, Random random) { @@ -1669,46 +1555,28 @@ void build(int[] a, int m, Random random) { } }, - ASCENDING { + UNIFORM { void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = m + i; - } - } - }, + int mask = (m << 15) - 1; - DESCENDING { - void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { - a[i] = a.length - m - i; + a[i] = random.nextInt() & mask; } } }, - EQUAL { + REPEATED { void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { - a[i] = m; - } - } - }, - - UNIFORM { - void build(int[] a, int m, Random random) { - int mask = (m << 15) - 1; - - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextInt() & mask; + a[i] = i % m; } } }, - MASKED { + DUPLICATED { void build(int[] a, int m, Random random) { - int mask = (m << 15) - 1; - for (int i = 0; i < a.length; ++i) { - a[i] = (i ^ 0xFF) & mask; + a[i] = random.nextInt(m); } } }, @@ -1740,24 +1608,53 @@ void build(int[] a, int m, Random random) { } }, - REPEATED { + SHUFFLE { void build(int[] a, int m, Random random) { + 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 { + void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { - a[i] = i % m; + a[i] = m + i; } } }, - DUPLICATED { - void build(int[] a, int m, Random random) { + DESCENDING { + void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { - a[i] = random.nextInt(m); + a[i] = a.length - m - i; + } + } + }, + + EQUAL { + void build(int[] a, int m) { + Arrays.fill(a, m); + } + }, + + MASKED { + 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) { + void build(int[] a, int m) { int middle = a.length / (m + 1); for (int i = 0; i < middle; ++i) { @@ -1771,7 +1668,7 @@ void build(int[] a, int m, Random random) { }, STAGGER { - void build(int[] a, int m, Random random) { + void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = (i * m + i) % a.length; } @@ -1779,86 +1676,27 @@ void build(int[] a, int m, Random random) { }, PLATEAU { - void build(int[] a, int m, Random random) { + 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 k = 0, j = 0; - - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextBoolean() ? (k += 2) : (j += 2); - } - } - }, - LATCH { - void build(int[] a, int m, Random random) { + 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) { 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 { 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; } }, @@ -1868,7 +1706,7 @@ void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = i; } - reverse(a, 0, a.length - 1); + reverse(a, m, a.length - 1); } }, @@ -1877,7 +1715,7 @@ void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = i; } - reverse(a, 0, 2); + reverse(a, 0, Math.min(m, a.length)); } }, @@ -1912,7 +1750,7 @@ private static void reverse(int[] a, int lo, int hi) { } } - private static enum NegativeZeroBuilder { + private enum NegativeZeroBuilder { FLOAT { void build(Object o, Random random) { @@ -1937,7 +1775,7 @@ void build(Object o, Random random) { abstract void build(Object o, Random random); } - private static enum FloatingPointBuilder { + private enum FloatingPointBuilder { FLOAT { void build(Object o, int a, int g, int z, int n, int p, Random random) { @@ -2024,43 +1862,6 @@ private static void fillWithValue(double[] a, double value, int fromIndex, int c } } - 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 DEDA = new TestRandom(0xDEDA); @@ -2077,6 +1878,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 d58da59168a84..29b483c156bbd 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, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2022, 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 @@ -38,134 +38,82 @@ public enum SortingHelper { DUAL_PIVOT_QUICKSORT("Dual-Pivot Quicksort") { - - @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); - } - } - @Override public void sort(Object a, int low, int high) { - if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, SEQUENTIAL, low, high); - } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, SEQUENTIAL, 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); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, SEQUENTIAL, low, high); - } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, SEQUENTIAL, low, high); - } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, SEQUENTIAL, low, high); - } else { - fail(a); - } + sort(a, SEQUENTIAL, low, high); } }, PARALLEL_SORT("Parallel sort") { - @Override - public void sort(Object a) { - if (a instanceof int[]) { - DualPivotQuicksort.sort((int[]) a, PARALLEL, 0, ((int[]) a).length); - } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, PARALLEL, 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, PARALLEL, 0, ((char[]) a).length); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, PARALLEL, 0, ((short[]) a).length); - } else if (a instanceof float[]) { - DualPivotQuicksort.sort((float[]) a, PARALLEL, 0, ((float[]) a).length); - } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, PARALLEL, 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, PARALLEL, low, high); + DualPivotQuicksort.mixedInsertionSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.sort((long[]) a, PARALLEL, 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, 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); + DualPivotQuicksort.mixedInsertionSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.sort((double[]) a, PARALLEL, low, high); + DualPivotQuicksort.mixedInsertionSort((double[]) a, low, high); } else { fail(a); } } }, - HEAP_SORT("Heap 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(null, (int[]) a, BIG_DEPTH, 0, ((int[]) a).length); + DualPivotQuicksort.insertionSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, 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, BIG_DEPTH, 0, ((char[]) a).length); + DualPivotQuicksort.insertionSort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, BIG_DEPTH, 0, ((short[]) a).length); + DualPivotQuicksort.insertionSort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.sort(null, (float[]) a, BIG_DEPTH, 0, ((float[]) a).length); + DualPivotQuicksort.insertionSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, 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(null, (int[]) a, BIG_DEPTH, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (int[]) a, low, high - low)); } else if (a instanceof long[]) { - DualPivotQuicksort.sort(null, (long[]) a, BIG_DEPTH, 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, 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); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (float[]) a, low, high - low)); } else if (a instanceof double[]) { - DualPivotQuicksort.sort(null, (double[]) a, BIG_DEPTH, low, high); + check("Merging", DualPivotQuicksort.tryMergingSort(null, (double[]) a, low, high - low)); } else { fail(a); } @@ -173,44 +121,45 @@ public void sort(Object a, int low, int high) { }, RADIX_SORT("Radix sort") { - @Override - public void sort(Object a) { + public void sort(Object a, int low, int high) { if (a instanceof int[]) { - DualPivotQuicksort.tryRadixSort(null, (int[]) a, 0, ((int[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (int[]) a, low, high)); } else if (a instanceof long[]) { - DualPivotQuicksort.tryRadixSort(null, (long[]) a, 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, 0, ((char[]) a).length); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, 0, ((short[]) a).length); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.tryRadixSort(null, (float[]) a, 0, ((float[]) a).length); + check("Radix", DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high)); } else if (a instanceof double[]) { - DualPivotQuicksort.tryRadixSort(null, (double[]) a, 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.tryRadixSort(null, (int[]) a, low, high); + DualPivotQuicksort.heapSort((int[]) a, low, high); } else if (a instanceof long[]) { - DualPivotQuicksort.tryRadixSort(null, (long[]) a, 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, 0, low, high); + DualPivotQuicksort.sort((char[]) a, low, high); } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, 0, low, high); + DualPivotQuicksort.sort((short[]) a, low, high); } else if (a instanceof float[]) { - DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high); + DualPivotQuicksort.heapSort((float[]) a, low, high); } else if (a instanceof double[]) { - DualPivotQuicksort.tryRadixSort(null, (double[]) a, low, high); + DualPivotQuicksort.heapSort((double[]) a, low, high); } else { fail(a); } @@ -218,7 +167,6 @@ public void sort(Object a, int low, int high) { }, ARRAYS_SORT("Arrays.sort") { - @Override public void sort(Object a) { if (a instanceof int[]) { @@ -260,21 +208,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[]) { @@ -316,33 +252,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); - public void sort(Object[] a) { - fail(a); - } - - public void sort(Object[] a, Comparator comparator) { - fail(a); + 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; } @@ -351,11 +285,41 @@ 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 String name; + private static void fail(String message) { + throw new RuntimeException(message); + } + + private final String name; /** * Parallelism level for sequential sorting. @@ -366,10 +330,4 @@ private static void fail(Object a) { * Parallelism level for parallel sorting. */ private static final int PARALLEL = 88; - - /** - * Heap sort will be invoked, if recursion depth is too big. - * Value is taken from DualPivotQuicksort.MAX_RECURSION_DEPTH. - */ - private static final int BIG_DEPTH = 64 * (3 << 1); } From 6acc0bdb94a588dba830728e04e096dc10ebf974 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Thu, 30 Jun 2022 19:14:57 +0300 Subject: [PATCH 13/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Improved parallel sorting * Optimized buffer allocation --- .../classes/java/util/DualPivotQuicksort.java | 709 +++++++++--------- test/jdk/java/util/Arrays/Sorting.java | 140 +--- .../java.base/java/util/SortingHelper.java | 4 +- 3 files changed, 361 insertions(+), 492 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 2369cc1e2cfcb..165793f8fca34 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -26,7 +26,6 @@ package java.util; import java.util.concurrent.CountedCompleter; -import java.util.concurrent.RecursiveTask; /** * This class implements powerful and fully optimized versions, both @@ -45,9 +44,9 @@ * @author Josh Bloch * @author Doug Lea * - * @version 2020.06.14 + * @version 2022.06.14 * - * @since 1.7 * 14 ^ 18 + * @since 1.7 * 14 & 20 */ final class DualPivotQuicksort { @@ -66,7 +65,7 @@ private DualPivotQuicksort() {} /** * Max array size to use insertion sort. */ - private static final int MAX_INSERTION_SORT_SIZE = 20; + private static final int MAX_INSERTION_SORT_SIZE = 37; /* ----------------- Merging sort section ----------------- */ @@ -80,16 +79,6 @@ private DualPivotQuicksort() {} */ private static final int MIN_RUN_SIZE = 128; - /** - * Min number of runs for parallel merging. - */ - private static final int MIN_PARALLEL_RUN_MERGING_COUNT = 4; - - /** - * Min array size to invoke parallel merging of parts. - */ - private static final int MIN_PARALLEL_PART_MERGING_SIZE = 4 << 10; - /* ------------------ Radix sort section ------------------ */ /** @@ -111,22 +100,29 @@ private DualPivotQuicksort() {} /* -------------------- Common section -------------------- */ + /** + * Min array size to perform sorting in parallel. + */ + private static final int MIN_PARALLEL_SORT_SIZE = 640; + /** * Max recursive depth before switching to heap sort. */ private static final int MAX_RECURSION_DEPTH = 64 << 1; /** - * Min array size to perform sorting in parallel. + * Max size of additional buffer, + * limited by max_heap / 128 or 2 GB max. */ - private static final int MIN_PARALLEL_SORT_SIZE = 4 << 10; + private static final int MAX_4_BYTE_BUFFER_SIZE = + (int) Math.min(Runtime.getRuntime().maxMemory() >> 7, Integer.MAX_VALUE); /** - * Max length of additional buffer, limited by - * max_heap / 64 or 256mb elements (2gb max). + * Max size of additional buffer, + * limited by max_heap / 256 or 2 GB max. */ - private static final int MAX_BUFFER_LENGTH = - (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, 256L << 20); + private static final int MAX_8_BYTE_BUFFER_SIZE = + (int) Math.min(Runtime.getRuntime().maxMemory() >> 8, Integer.MAX_VALUE); /** * Sorts the specified range of the array using parallel merge @@ -144,8 +140,10 @@ private DualPivotQuicksort() {} * @param high the index of the last element, exclusive, to be sorted */ static void sort(int[] a, int parallelism, int low, int high) { - if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + int size = high - low; + + if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { + new Sorter(a, tryIntAllocate(size), parallelism, low, size, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -163,7 +161,7 @@ static void sort(int[] a, int parallelism, 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 adaptive mixed insertion sort on small non-leftmost parts. @@ -185,7 +183,7 @@ static void sort(Sorter sorter, int[] a, int bits, int low, int high) { * Try merging sort on large part. */ if (size > MIN_MERGING_SORT_SIZE * bits - && tryMergingSort(sorter, a, low, size)) { + && tryMergingSort(sorter, a, low, high)) { return; } @@ -201,6 +199,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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; @@ -352,8 +351,8 @@ && tryRadixSort(sorter, a, low, high)) { * 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, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); } else { sort(sorter, a, bits | 1, lower + 1, upper); sort(sorter, a, bits | 1, upper + 1, high); @@ -427,7 +426,7 @@ && tryRadixSort(sorter, a, low, high)) { * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, upper, high); } else { sort(sorter, a, bits | 1, upper, high); } @@ -453,38 +452,42 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ static void mixedInsertionSort(int[] a, int low, int high) { - int end = high - (((high - low) >> 4) << 3); + int size = high - low; /* - * Start with pin insertion sort. + * Invoke simple insertion sort on small part. */ - for (int i, p = high; ++low < end && low < p; ) { - int ai = a[i = low]; - - /* - * Find pin element, smaller than the given element. - */ - while (ai < a[--p]); + if (size < MAX_INSERTION_SORT_SIZE) { + for (int i; ++low < high; ) { + int ai = a[i = low]; - /* - * Swap these elements. - */ - ai = a[p]; a[p] = a[i]; - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; } - a[i + 1] = ai; + return; } /* - * Continue with simple insertion sort. + * The index of the last element + * for pin insertion sort, exclusive. + */ + int end = high - 3 * (size >> 3 << 1); + + /* + * Start with pin insertion sort. */ - for (int i; low < end; ++low) { - int ai = a[i = low]; + for (int i, p = high; ++low < end; ) { + int ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } /* * Insert element into sorted part. @@ -568,27 +571,26 @@ static void insertionSort(int[] a, int low, int high) { * * @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 + * @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 tryMergingSort(Sorter sorter, int[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, int[] a, int low, int high) { /* - * Element run[i] holds the start index of - * i-th sub-sequence 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 of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -616,7 +618,7 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { * Check if the runs are too * long to continue scanning. */ - if (count > 1 && k - low < count * MIN_RUN_SIZE) { + if (count > 6 && k - low < count * MIN_RUN_SIZE) { return false; } @@ -633,7 +635,7 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { return true; } - run = new int[(size >> 9) & 0x1FF | 0x3F]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; } else if (a[last - 1] > a[last]) { // Start the new run @@ -646,6 +648,9 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { } } + /* + * Save the current run. + */ run[count] = (last = k); /* @@ -664,7 +669,7 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int size) { if (sorter != null && (b = (int[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, size)) == null) { + } else if ((b = tryIntAllocate(high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -703,19 +708,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_PARALLEL_RUN_MERGING_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]; @@ -724,7 +720,10 @@ 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) { + /* + * 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); @@ -763,7 +762,7 @@ private static void mergeParts(Merger merger, int[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -775,7 +774,7 @@ 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; @@ -788,17 +787,17 @@ private static void mergeParts(Merger merger, int[] dst, int k, } /* - * Reserve space for the left sub-parts. + * Reserve space for the left part. */ int d = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge the right part in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left part. */ hi1 = mi1; hi2 = mi2; @@ -825,7 +824,7 @@ private static void mergeParts(Merger merger, int[] dst, int k, /** * Tries to sort the specified range of the array - * using LSD (Least Significant Digit) Radix sort. + * 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 @@ -840,7 +839,7 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { */ if (sorter != null && (b = (int[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, size)) == null) { + } else if ((b = tryIntAllocate(size)) == null) { return false; } @@ -993,6 +992,20 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { a[p] = value; } + /** + * Tries to allocate memory for additional buffer. + * + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static int[] tryIntAllocate(int size) { + try { + return size < MAX_4_BYTE_BUFFER_SIZE ? new int[size] : null; + } catch (OutOfMemoryError e) { + return null; + } + } + // #[long] /** @@ -1011,8 +1024,10 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { * @param high the index of the last element, exclusive, to be sorted */ static void sort(long[] a, int parallelism, int low, int high) { - if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + int size = high - low; + + if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { + new Sorter(a, tryLongAllocate(size), parallelism, low, size, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -1030,7 +1045,7 @@ static void sort(long[] a, int parallelism, 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 adaptive mixed insertion sort on small non-leftmost parts. @@ -1052,7 +1067,7 @@ static void sort(Sorter sorter, long[] a, int bits, int low, int high) { * Try merging sort on large part. */ if (size > MIN_MERGING_SORT_SIZE * bits - && tryMergingSort(sorter, a, low, size)) { + && tryMergingSort(sorter, a, low, high)) { return; } @@ -1068,6 +1083,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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; @@ -1219,8 +1235,8 @@ && tryRadixSort(sorter, a, low, high)) { * 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, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); } else { sort(sorter, a, bits | 1, lower + 1, upper); sort(sorter, a, bits | 1, upper + 1, high); @@ -1294,7 +1310,7 @@ && tryRadixSort(sorter, a, low, high)) { * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, upper, high); } else { sort(sorter, a, bits | 1, upper, high); } @@ -1320,38 +1336,42 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ static void mixedInsertionSort(long[] a, int low, int high) { - int end = high - (((high - low) >> 4) << 3); + int size = high - low; /* - * Start with pin insertion sort. + * Invoke simple insertion sort on small part. */ - for (int i, p = high; ++low < end && low < p; ) { - long ai = a[i = low]; + if (size < MAX_INSERTION_SORT_SIZE) { + for (int i; ++low < high; ) { + long ai = a[i = low]; - /* - * Find pin element, smaller than the given element. - */ - while (ai < a[--p]); - - /* - * Swap these elements. - */ - ai = a[p]; a[p] = a[i]; - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; } - a[i + 1] = ai; + return; } /* - * Continue with simple insertion sort. + * The index of the last element + * for pin insertion sort, exclusive. */ - for (int i; low < end; ++low) { - long ai = a[i = low]; + int end = high - 3 * (size >> 3 << 1); + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + long ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } /* * Insert element into sorted part. @@ -1435,27 +1455,26 @@ static void insertionSort(long[] a, int low, int high) { * * @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 + * @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 tryMergingSort(Sorter sorter, long[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, long[] a, int low, int high) { /* - * Element run[i] holds the start index of - * i-th sub-sequence 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 of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -1483,7 +1502,7 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { * Check if the runs are too * long to continue scanning. */ - if (count > 1 && k - low < count * MIN_RUN_SIZE) { + if (count > 6 && k - low < count * MIN_RUN_SIZE) { return false; } @@ -1500,7 +1519,7 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { return true; } - run = new int[(size >> 9) & 0x1FF | 0x3F]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; } else if (a[last - 1] > a[last]) { // Start the new run @@ -1513,6 +1532,9 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { } } + /* + * Save the current run. + */ run[count] = (last = k); /* @@ -1531,7 +1553,7 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int size) { if (sorter != null && (b = (long[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, size)) == null) { + } else if ((b = tryLongAllocate(high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -1570,19 +1592,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_PARALLEL_RUN_MERGING_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]; @@ -1591,7 +1604,10 @@ 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) { + /* + * 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); @@ -1630,7 +1646,7 @@ private static void mergeParts(Merger merger, long[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -1642,7 +1658,7 @@ 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; @@ -1655,17 +1671,17 @@ private static void mergeParts(Merger merger, long[] dst, int k, } /* - * Reserve space for the left sub-parts. + * Reserve space for the left part. */ int d = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge the right part in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left part. */ hi1 = mi1; hi2 = mi2; @@ -1692,7 +1708,7 @@ private static void mergeParts(Merger merger, long[] dst, int k, /** * Tries to sort the specified range of the array - * using LSD (Least Significant Digit) Radix sort. + * 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 @@ -1707,7 +1723,7 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { */ if (sorter != null && (b = (long[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, size)) == null) { + } else if ((b = tryLongAllocate(size)) == null) { return false; } @@ -1880,6 +1896,20 @@ private static void pushDown(long[] a, int p, long value, int low, int high) { a[p] = value; } + /** + * Tries to allocate memory for additional buffer. + * + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static long[] tryLongAllocate(int size) { + try { + return size < MAX_8_BYTE_BUFFER_SIZE ? new long[size] : null; + } catch (OutOfMemoryError e) { + return null; + } + } + // #[byte] /** @@ -2048,7 +2078,7 @@ private static void countingSort(char[] a, int low, int high) { */ 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 part. @@ -2078,6 +2108,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; @@ -2391,7 +2422,7 @@ private static void countingSort(short[] a, int low, int high) { */ 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 part. @@ -2421,6 +2452,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; @@ -2696,8 +2728,10 @@ static void sort(float[] a, int parallelism, int low, int high) { * Phase 2. Sort everything except NaNs, * which are already in place. */ - if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + int size = high - low; + + if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { + new Sorter(a, tryFloatAllocate(size), parallelism, low, size, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -2744,7 +2778,7 @@ static void sort(float[] a, int parallelism, 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 adaptive mixed insertion sort on small non-leftmost parts. @@ -2766,7 +2800,7 @@ static void sort(Sorter sorter, float[] a, int bits, int low, int high) { * Try merging sort on large part. */ if (size > MIN_MERGING_SORT_SIZE * bits - && tryMergingSort(sorter, a, low, size)) { + && tryMergingSort(sorter, a, low, high)) { return; } @@ -2782,6 +2816,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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; @@ -2933,8 +2968,8 @@ && tryRadixSort(sorter, a, low, high)) { * 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, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); } else { sort(sorter, a, bits | 1, lower + 1, upper); sort(sorter, a, bits | 1, upper + 1, high); @@ -3008,7 +3043,7 @@ && tryRadixSort(sorter, a, low, high)) { * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, upper, high); } else { sort(sorter, a, bits | 1, upper, high); } @@ -3034,38 +3069,42 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ static void mixedInsertionSort(float[] a, int low, int high) { - int end = high - (((high - low) >> 4) << 3); + int size = high - low; /* - * Start with pin insertion sort. + * Invoke simple insertion sort on small part. */ - for (int i, p = high; ++low < end && low < p; ) { - float ai = a[i = low]; - - /* - * Find pin element, smaller than the given element. - */ - while (ai < a[--p]); + if (size < MAX_INSERTION_SORT_SIZE) { + for (int i; ++low < high; ) { + float ai = a[i = low]; - /* - * Swap these elements. - */ - ai = a[p]; a[p] = a[i]; - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; } - a[i + 1] = ai; + return; } /* - * Continue with simple insertion sort. + * The index of the last element + * for pin insertion sort, exclusive. + */ + int end = high - 3 * (size >> 3 << 1); + + /* + * Start with pin insertion sort. */ - for (int i; low < end; ++low) { - float ai = a[i = low]; + for (int i, p = high; ++low < end; ) { + float ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } /* * Insert element into sorted part. @@ -3149,27 +3188,26 @@ static void insertionSort(float[] a, int low, int high) { * * @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 + * @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 tryMergingSort(Sorter sorter, float[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, float[] a, int low, int high) { /* - * Element run[i] holds the start index of - * i-th sub-sequence 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 of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -3197,7 +3235,7 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { * Check if the runs are too * long to continue scanning. */ - if (count > 1 && k - low < count * MIN_RUN_SIZE) { + if (count > 6 && k - low < count * MIN_RUN_SIZE) { return false; } @@ -3214,7 +3252,7 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { return true; } - run = new int[(size >> 9) & 0x1FF | 0x3F]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; } else if (a[last - 1] > a[last]) { // Start the new run @@ -3227,6 +3265,9 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { } } + /* + * Save the current run. + */ run[count] = (last = k); /* @@ -3245,7 +3286,7 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int size) { if (sorter != null && (b = (float[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, size)) == null) { + } else if ((b = tryFloatAllocate(high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -3284,19 +3325,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_PARALLEL_RUN_MERGING_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]; @@ -3305,7 +3337,10 @@ 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) { + /* + * 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); @@ -3344,7 +3379,7 @@ private static void mergeParts(Merger merger, float[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -3356,7 +3391,7 @@ 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; @@ -3369,17 +3404,17 @@ private static void mergeParts(Merger merger, float[] dst, int k, } /* - * Reserve space for the left sub-parts. + * Reserve space for the left part. */ int d = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge the right part in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left part. */ hi1 = mi1; hi2 = mi2; @@ -3406,7 +3441,7 @@ private static void mergeParts(Merger merger, float[] dst, int k, /** * Tries to sort the specified range of the array - * using LSD (Least Significant Digit) Radix sort. + * 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 @@ -3421,7 +3456,7 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { */ if (sorter != null && (b = (float[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, size)) == null) { + } else if ((b = tryFloatAllocate(size)) == null) { return false; } @@ -3551,6 +3586,20 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { a[p] = value; } + /** + * Tries to allocate memory for additional buffer. + * + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static float[] tryFloatAllocate(int size) { + try { + return size < MAX_4_BYTE_BUFFER_SIZE ? new float[size] : null; + } catch (OutOfMemoryError e) { + return null; + } + } + // #[double] /** @@ -3592,8 +3641,10 @@ static void sort(double[] a, int parallelism, int low, int high) { * Phase 2. Sort everything except NaNs, * which are already in place. */ - if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + int size = high - low; + + if (parallelism > 1 && size > MIN_PARALLEL_SORT_SIZE) { + new Sorter(a, tryDoubleAllocate(size), parallelism, low, size, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -3640,7 +3691,7 @@ static void sort(double[] a, int parallelism, 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 adaptive mixed insertion sort on small non-leftmost parts. @@ -3662,7 +3713,7 @@ static void sort(Sorter sorter, double[] a, int bits, int low, int high) { * Try merging sort on large part. */ if (size > MIN_MERGING_SORT_SIZE * bits - && tryMergingSort(sorter, a, low, size)) { + && tryMergingSort(sorter, a, low, high)) { return; } @@ -3678,6 +3729,7 @@ && tryMergingSort(sorter, a, low, size)) { * 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; @@ -3829,8 +3881,8 @@ && tryRadixSort(sorter, a, low, high)) { * 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, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); } else { sort(sorter, a, bits | 1, lower + 1, upper); sort(sorter, a, bits | 1, upper + 1, high); @@ -3904,7 +3956,7 @@ && tryRadixSort(sorter, a, low, high)) { * equal and therefore already sorted. */ if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.forkSorter(bits | 1, upper, high); + sorter.fork(bits | 1, upper, high); } else { sort(sorter, a, bits | 1, upper, high); } @@ -3930,38 +3982,42 @@ && tryRadixSort(sorter, a, low, high)) { * @param high the index of the last element, exclusive, to be sorted */ static void mixedInsertionSort(double[] a, int low, int high) { - int end = high - (((high - low) >> 4) << 3); + int size = high - low; /* - * Start with pin insertion sort. + * Invoke simple insertion sort on small part. */ - for (int i, p = high; ++low < end && low < p; ) { - double ai = a[i = low]; - - /* - * Find pin element, smaller than the given element. - */ - while (ai < a[--p]); - - /* - * Swap these elements. - */ - ai = a[p]; a[p] = a[i]; + if (size < MAX_INSERTION_SORT_SIZE) { + for (int i; ++low < high; ) { + double ai = a[i = low]; - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; + while (ai < a[--i]) { + a[i + 1] = a[i]; + } + a[i + 1] = ai; } - a[i + 1] = ai; + return; } /* - * Continue with simple insertion sort. + * The index of the last element + * for pin insertion sort, exclusive. + */ + int end = high - 3 * (size >> 3 << 1); + + /* + * Start with pin insertion sort. */ - for (int i; low < end; ++low) { - double ai = a[i = low]; + for (int i, p = high; ++low < end; ) { + double ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } /* * Insert element into sorted part. @@ -4045,27 +4101,26 @@ static void insertionSort(double[] a, int low, int high) { * * @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 + * @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 tryMergingSort(Sorter sorter, double[] a, int low, int size) { + static boolean tryMergingSort(Sorter sorter, double[] a, int low, int high) { /* - * Element run[i] holds the start index of - * i-th sub-sequence 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 of the current run. + * Find the next run. */ if (a[k - 1] < a[k]) { @@ -4093,7 +4148,7 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { * Check if the runs are too * long to continue scanning. */ - if (count > 1 && k - low < count * MIN_RUN_SIZE) { + if (count > 6 && k - low < count * MIN_RUN_SIZE) { return false; } @@ -4110,7 +4165,7 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { return true; } - run = new int[(size >> 9) & 0x1FF | 0x3F]; + run = new int[((high - low) >> 9) & 0x1FF | 0x3F]; run[0] = low; } else if (a[last - 1] > a[last]) { // Start the new run @@ -4123,6 +4178,9 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { } } + /* + * Save the current run. + */ run[count] = (last = k); /* @@ -4141,7 +4199,7 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int size) { if (sorter != null && (b = (double[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, size)) == null) { + } else if ((b = tryDoubleAllocate(high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -4180,19 +4238,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_PARALLEL_RUN_MERGING_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]; @@ -4201,7 +4250,10 @@ 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) { + /* + * 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); @@ -4240,7 +4292,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, /* * Small parts will be merged sequentially. */ - if (hi1 - lo1 < MIN_PARALLEL_PART_MERGING_SIZE) { + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { break; } @@ -4252,7 +4304,7 @@ 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; @@ -4265,17 +4317,17 @@ private static void mergeParts(Merger merger, double[] dst, int k, } /* - * Reserve space for the left sub-parts. + * Reserve space for the left part. */ int d = mi2 - lo2 + mi1 - lo1; /* - * Merge the right sub-parts in parallel. + * Merge the right part in parallel. */ - merger.forkMerger(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); /* - * Process the sub-left parts. + * Iterate along the left part. */ hi1 = mi1; hi2 = mi2; @@ -4302,7 +4354,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, /** * Tries to sort the specified range of the array - * using LSD (Least Significant Digit) Radix sort. + * 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 @@ -4317,7 +4369,7 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { */ if (sorter != null && (b = (double[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, size)) == null) { + } else if ((b = tryDoubleAllocate(size)) == null) { return false; } @@ -4501,6 +4553,20 @@ private static void pushDown(double[] a, int p, double value, int low, int high) a[p] = value; } + /** + * Tries to allocate memory for additional buffer. + * + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static double[] tryDoubleAllocate(int size) { + try { + return size < MAX_8_BYTE_BUFFER_SIZE ? new double[size] : null; + } catch (OutOfMemoryError e) { + return null; + } + } + // #[class] /** @@ -4513,17 +4579,17 @@ private static final class Sorter extends CountedCompleter { private final Object a, b; private final int low, size, offset, depth; - private Sorter(Object a, int parallelism, int low, int size, int depth) { + private Sorter(Object a, Object b, int parallelism, int low, int size, int depth) { this.a = a; + this.b = b; this.low = low; this.size = size; this.offset = low; - while ((parallelism >>= 1) > 0 && (size >>= 8) > 0) { + while (b != null && (parallelism >>= 1) > 0 && (size >>= 8) > 0) { depth -= 2; } - this.b = tryAllocate(a, this.size); - this.depth = b == null ? 0 : depth; + this.depth = depth; } private Sorter(CountedCompleter parent, @@ -4561,7 +4627,7 @@ public void compute() { } @Override - public void onCompletion(CountedCompleter caller) { + public void onCompletion(CountedCompleter parent) { if (depth < 0) { int mi = low + (size >> 1); boolean src = (depth & 1) == 0; @@ -4579,7 +4645,7 @@ public void onCompletion(CountedCompleter caller) { } } - private void forkSorter(int depth, int low, int high) { + private void fork(int depth, int low, int high) { addToPendingCount(1); new Sorter(this, a, b, low, high - low, offset, depth).fork(); } @@ -4628,90 +4694,9 @@ public void compute() { propagateCompletion(); } - private void forkMerger(Object dst, int k, - Object a1, int lo1, int hi1, Object a2, int lo2, int hi2) { + private void fork(Object dst, int k, Object a1, int lo1, int hi1, Object a2, int lo2, int hi2) { addToPendingCount(1); new Merger(this, dst, k, a1, lo1, hi1, a2, lo2, hi2).fork(); } } - - /** - * This class implements parallel merging of runs. - */ - private static final class RunMerger extends RecursiveTask { - private static final long serialVersionUID = 123456789L; - - @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 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 array: " + a.getClass().getName()); - } - - private RunMerger forkMe() { - fork(); - return this; - } - - private Object getDestination() { - join(); - return getRawResult(); - } - } - - /** - * Tries to allocate memory for additional buffer. - * - * @param a the array of given type - * @param length the additional buffer length - * @return {@code null} if requested length is too large, otherwise created buffer - */ - private static Object tryAllocate(Object a, int length) { - if (length > MAX_BUFFER_LENGTH) { - return null; - } - try { - if (a instanceof int[]) { - return new int[length]; - } - if (a instanceof long[]) { - return new long[length]; - } - if (a instanceof float[]) { - return new float[length]; - } - if (a instanceof double[]) { - return new double[length]; - } - throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); - } catch (OutOfMemoryError e) { - return null; - } - } } diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index 3c8b5c4f848ef..d8f5e3c10f0f5 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -139,7 +139,6 @@ private void testCore() { private void testAll() { testCore(); - testNullArray(); for (int length : lengths) { createData(length); @@ -177,121 +176,6 @@ private void testEmptyArray() { sortingHelper.sort(new double[] {}, 0, 0); } - private void testNullArray() { - testNullIntArray(); - testNullLongArray(); - testNullByteArray(); - testNullCharArray(); - testNullShortArray(); - testNullFloatArray(); - testNullDoubleArray(); - } - - private void testNullIntArray() { - 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 testNullLongArray() { - 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 testNullByteArray() { - 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 testNullCharArray() { - 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 testNullShortArray() { - 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 testNullFloatArray() { - 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 testNullDoubleArray() { - 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) { if (fix || length < 4) { return; @@ -315,7 +199,7 @@ private void testSubArray(int length) { 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; + ((int[]) gold[0])[i - 1] = i % m + m % i; } convertData(length); @@ -415,7 +299,7 @@ private void testNegativeZero(int length, TestRandom random) { 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]); @@ -463,7 +347,7 @@ private void testFloatingPointSorting(int length, TestRandom random) { printTestName("Test float-pointing sorting", random, length, ", a = " + a + ", g = " + g + ", z = " + g + ", n = " + g + ", p = " + g + ", " + getType(i)); - FloatingPointBuilder builder = FloatingPointBuilder.values() [i - 5]; + 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); @@ -1231,7 +1115,7 @@ 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); @@ -1251,7 +1135,7 @@ 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); @@ -1271,7 +1155,7 @@ 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); @@ -1291,7 +1175,7 @@ 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); @@ -1311,7 +1195,7 @@ 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); @@ -1331,7 +1215,7 @@ 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); @@ -1351,7 +1235,7 @@ 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); @@ -1415,11 +1299,11 @@ private String hex(long a, int b) { } private void printTestName(String test, int length, String message) { - out.println( "[" + sortingHelper + "] '" + test + "' length = " + length + message); + out.println("[" + sortingHelper + "] '" + test + "' length = " + length + message); } private void printTestName(String test, TestRandom random, int length, String message) { - out.println( "[" + sortingHelper + "] '" + test + + out.println("[" + sortingHelper + "] '" + test + "' length = " + length + ", random = " + random + message); } 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 29b483c156bbd..45bb7b202fc17 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 @@ -31,9 +31,9 @@ * * @author Vladimir Yaroslavskiy * - * @version 2020.06.14 + * @version 2022.06.14 * - * @since 14 & 18 + * @since 14 & 20 */ public enum SortingHelper { From 1cb6fd7ad73982b11b39afe7b80d8bcf80ff4357 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Thu, 30 Jun 2022 19:37:19 +0300 Subject: [PATCH 14/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Fix @since version --- src/java.base/share/classes/java/util/DualPivotQuicksort.java | 2 +- .../jdk/java/util/Arrays/java.base/java/util/SortingHelper.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 165793f8fca34..9b34c9c031c3f 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -46,7 +46,7 @@ * * @version 2022.06.14 * - * @since 1.7 * 14 & 20 + * @since 1.7 * 14 ^ 20 */ final class DualPivotQuicksort { 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 45bb7b202fc17..afc5c3a324dc3 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 @@ -33,7 +33,7 @@ * * @version 2022.06.14 * - * @since 14 & 20 + * @since 14 ^ 20 */ public enum SortingHelper { From 618bdb5f8d2a4354462a886c854efbdfc97c6d48 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Thu, 7 Jul 2022 18:55:24 +0300 Subject: [PATCH 15/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Added JMH --- .../classes/java/util/DualPivotQuicksort.java | 2 +- test/jdk/java/util/Arrays/Sorting.java | 31 + .../openjdk/bench/java/util/ArraysSort.java | 552 ++++++++++++++++++ 3 files changed, 584 insertions(+), 1 deletion(-) create mode 100644 test/micro/org/openjdk/bench/java/util/ArraysSort.java diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 9b34c9c031c3f..153a29b3a475a 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -103,7 +103,7 @@ private DualPivotQuicksort() {} /** * Min array size to perform sorting in parallel. */ - private static final int MIN_PARALLEL_SORT_SIZE = 640; + private static final int MIN_PARALLEL_SORT_SIZE = 1024; /** * Max recursive depth before switching to heap sort. diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index d8f5e3c10f0f5..58ff871cf8743 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -1310,6 +1310,7 @@ private void printTestName(String test, TestRandom random, int length, String me private enum TypeConverter { INT { + @Override void convert(int[] src, Object dst, boolean fix) { if (fix) { src[0] = Integer.MIN_VALUE; @@ -1318,6 +1319,7 @@ void convert(int[] src, Object dst, boolean fix) { }, LONG { + @Override void convert(int[] src, Object dst, boolean fix) { long[] b = (long[]) dst; @@ -1331,6 +1333,7 @@ void convert(int[] src, Object dst, boolean fix) { }, BYTE { + @Override void convert(int[] src, Object dst, boolean fix) { byte[] b = (byte[]) dst; @@ -1344,6 +1347,7 @@ void convert(int[] src, Object dst, boolean fix) { }, CHAR { + @Override void convert(int[] src, Object dst, boolean fix) { char[] b = (char[]) dst; @@ -1357,6 +1361,7 @@ void convert(int[] src, Object dst, boolean fix) { }, SHORT { + @Override void convert(int[] src, Object dst, boolean fix) { short[] b = (short[]) dst; @@ -1370,6 +1375,7 @@ void convert(int[] src, Object dst, boolean fix) { }, FLOAT { + @Override void convert(int[] src, Object dst, boolean fix) { float[] b = (float[]) dst; @@ -1383,6 +1389,7 @@ void convert(int[] src, Object dst, boolean fix) { }, DOUBLE { + @Override void convert(int[] src, Object dst, boolean fix) { double[] b = (double[]) dst; @@ -1401,6 +1408,7 @@ void convert(int[] src, Object dst, boolean fix) { private enum SortedBuilder { STEPS { + @Override void build(int[] a, int m) { for (int i = 0; i < m; ++i) { a[i] = 0; @@ -1418,6 +1426,7 @@ void build(int[] a, int m) { private enum UnsortedBuilder { RANDOM { + @Override void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { a[i] = random.nextInt(); @@ -1426,6 +1435,7 @@ void build(int[] a, int m, Random random) { }, PERMUTATION { + @Override void build(int[] a, int m, Random random) { int mask = ~(0x000000FF << (random.nextInt(4) * 2)); @@ -1440,6 +1450,7 @@ void build(int[] a, int m, Random random) { }, UNIFORM { + @Override void build(int[] a, int m, Random random) { int mask = (m << 15) - 1; @@ -1450,6 +1461,7 @@ void build(int[] a, int m, Random random) { }, REPEATED { + @Override void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { a[i] = i % m; @@ -1458,6 +1470,7 @@ void build(int[] a, int m, Random random) { }, DUPLICATED { + @Override void build(int[] a, int m, Random random) { for (int i = 0; i < a.length; ++i) { a[i] = random.nextInt(m); @@ -1466,6 +1479,7 @@ void build(int[] a, int m, Random random) { }, SAWTOOTH { + @Override void build(int[] a, int m, Random random) { int incCount = 1; int decCount = a.length; @@ -1493,6 +1507,7 @@ void build(int[] a, int m, Random random) { }, SHUFFLE { + @Override void build(int[] a, int m, Random random) { for (int i = 0, j = 0, k = 1; i < a.length; ++i) { a[i] = random.nextInt(m) > 0 ? (j += 2) : (k += 2); @@ -1506,6 +1521,7 @@ 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; @@ -1514,6 +1530,7 @@ void build(int[] a, int m) { }, DESCENDING { + @Override void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = a.length - m - i; @@ -1522,12 +1539,14 @@ void build(int[] a, int m) { }, 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; @@ -1538,6 +1557,7 @@ void build(int[] a, int m) { }, ORGAN_PIPES { + @Override void build(int[] a, int m) { int middle = a.length / (m + 1); @@ -1552,6 +1572,7 @@ void build(int[] a, int m) { }, STAGGER { + @Override void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = (i * m + i) % a.length; @@ -1560,6 +1581,7 @@ void build(int[] a, int m) { }, PLATEAU { + @Override void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = Math.min(i, m); @@ -1568,6 +1590,7 @@ void build(int[] a, int m) { }, LATCH { + @Override void build(int[] a, int m) { int max = a.length / m; max = Math.max(max, 2); @@ -1579,6 +1602,7 @@ void build(int[] a, int m) { }, POINT { + @Override void build(int[] a, int m) { Arrays.fill(a, 0); a[a.length / 2] = m; @@ -1586,6 +1610,7 @@ void build(int[] a, int m) { }, LINE { + @Override void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = i; @@ -1595,6 +1620,7 @@ void build(int[] a, int m) { }, PEARL { + @Override void build(int[] a, int m) { for (int i = 0; i < a.length; ++i) { a[i] = i; @@ -1604,6 +1630,7 @@ void build(int[] a, int m) { }, RING { + @Override void build(int[] a, int m) { int k1 = a.length / 3; int k2 = a.length / 3 * 2; @@ -1637,6 +1664,7 @@ private static void reverse(int[] a, int lo, int hi) { private enum NegativeZeroBuilder { FLOAT { + @Override void build(Object o, Random random) { float[] a = (float[]) o; @@ -1647,6 +1675,7 @@ void build(Object o, Random random) { }, DOUBLE { + @Override void build(Object o, Random random) { double[] a = (double[]) o; @@ -1662,6 +1691,7 @@ void build(Object o, Random random) { 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(); @@ -1697,6 +1727,7 @@ void build(Object o, int a, int g, int z, int n, int p, Random random) { }, 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(); diff --git a/test/micro/org/openjdk/bench/java/util/ArraysSort.java b/test/micro/org/openjdk/bench/java/util/ArraysSort.java new file mode 100644 index 0000000000000..bf38cd8fc5c83 --- /dev/null +++ b/test/micro/org/openjdk/bench/java/util/ArraysSort.java @@ -0,0 +1,552 @@ +/* + * Copyright (c) 2022, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +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.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.Warmup; + +/** + * Microbenchmark for Arrays.sort() and Arrays.parallelSort(). + * + * @author Vladimir Yaroslavskiy + * + * @version 2022.06.14 + * + * @since 20 + */ +@Fork(1) +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 4, time = 3, timeUnit = TimeUnit.SECONDS) +public class ArraysSort { + + @Param({ "100", "1000", "10000", "100000", "1000000" }) + int size; + + Random random; + + @Setup(Level.Iteration) + public void start() { + random = new Random(0x777); + } + + public static class Int extends ArraysSort { + + @Param + private Type type; + + int[] gold; + + public enum Type { + + RANDOM { + @Override + void build(int[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt(); + } + } + }, + + REPEATED { + @Override + void build(int[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = i % 7; + } + } + }, + + STAGGER { + @Override + void build(int[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (i * 5) % a.length; + } + } + }, + + SHUFFLE { + @Override + void build(int[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(int[] a, Random random); + } + + @Setup + public void setup() { + gold = new int[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Long extends ArraysSort { + + @Param + private Type type; + + long[] gold; + + public enum Type { + + RANDOM { + @Override + void build(long[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextLong(); + } + } + }, + + REPEATED { + @Override + void build(long[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = i % 7; + } + } + }, + + STAGGER { + @Override + void build(long[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (i * 5L) % a.length; + } + } + }, + + SHUFFLE { + @Override + void build(long[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(long[] a, Random random); + } + + @Setup + public void setup() { + gold = new long[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Byte extends ArraysSort { + + @Param + private Type type; + + byte[] gold; + + public enum Type { + + RANDOM { + @Override + void build(byte[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (byte) random.nextInt(); + } + } + }, + + REPEATED { + @Override + void build(byte[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (byte) (i % 7); + } + } + }, + + STAGGER { + @Override + void build(byte[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (byte) ((i * 5) % a.length); + } + } + }, + + SHUFFLE { + @Override + void build(byte[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = (byte) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); + } + } + }; + + abstract void build(byte[] a, Random random); + } + + @Setup + public void setup() { + gold = new byte[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Char extends ArraysSort { + + @Param + private Type type; + + char[] gold; + + public enum Type { + + RANDOM { + @Override + void build(char[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (char) random.nextInt(); + } + } + }, + + REPEATED { + @Override + void build(char[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (char) (i % 7); + } + } + }, + + STAGGER { + @Override + void build(char[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (char) ((i * 5) % a.length); + } + } + }, + + SHUFFLE { + @Override + void build(char[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = (char) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); + } + } + }; + + abstract void build(char[] a, Random random); + } + + @Setup + public void setup() { + gold = new char[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Short extends ArraysSort { + + @Param + private Type type; + + short[] gold; + + public enum Type { + + RANDOM { + @Override + void build(short[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (short) random.nextInt(); + } + } + }, + + REPEATED { + @Override + void build(short[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (short) (i % 7); + } + } + }, + + STAGGER { + @Override + void build(short[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (short) ((i * 5) % a.length); + } + } + }, + + SHUFFLE { + @Override + void build(short[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = (short) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); + } + } + }; + + abstract void build(short[] a, Random random); + } + + @Setup + public void setup() { + gold = new short[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Float extends ArraysSort { + + @Param + private Type type; + + float[] gold; + + public enum Type { + + RANDOM { + @Override + void build(float[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextFloat(); + } + } + }, + + REPEATED { + @Override + void build(float[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = i % 7; + } + } + }, + + STAGGER { + @Override + void build(float[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (i * 5) % a.length; + } + } + }, + + SHUFFLE { + @Override + void build(float[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(float[] a, Random random); + } + + @Setup + public void setup() { + gold = new float[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } + + public static class Double extends ArraysSort { + + @Param + private Type type; + + double[] gold; + + public enum Type { + + RANDOM { + @Override + void build(double[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextDouble(); + } + } + }, + + REPEATED { + @Override + void build(double[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = i % 7; + } + } + }, + + STAGGER { + @Override + void build(double[] a, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = (i * 5) % a.length; + } + } + }, + + SHUFFLE { + @Override + void build(double[] a, Random random) { + for (int i = 0, j = 0, k = 1; i < a.length; ++i) { + a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); + } + } + }; + + abstract void build(double[] a, Random random); + } + + @Setup + public void setup() { + gold = new double[size]; + } + + @Setup(Level.Invocation) + public void init() { + type.build(gold, random); + } + + @Benchmark + public void testSort() { + Arrays.sort(gold); + } + + @Benchmark + public void testParallelSort() { + Arrays.parallelSort(gold); + } + } +} From fcda2aa830924fe1317937a67fa6ca3f545f9d4f Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 2 Aug 2022 16:53:33 +0300 Subject: [PATCH 16/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Optimized and unified buffer allocation * Minor comment changes --- .../classes/java/util/DualPivotQuicksort.java | 175 +++++++----------- 1 file changed, 68 insertions(+), 107 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index 153a29b3a475a..f3d06d635a551 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -112,17 +112,10 @@ private DualPivotQuicksort() {} /** * Max size of additional buffer, - * limited by max_heap / 128 or 2 GB max. + * limited by max_heap / 64 or 2 GB max. */ - private static final int MAX_4_BYTE_BUFFER_SIZE = - (int) Math.min(Runtime.getRuntime().maxMemory() >> 7, Integer.MAX_VALUE); - - /** - * Max size of additional buffer, - * limited by max_heap / 256 or 2 GB max. - */ - private static final int MAX_8_BYTE_BUFFER_SIZE = - (int) Math.min(Runtime.getRuntime().maxMemory() >> 8, Integer.MAX_VALUE); + private static final int MAX_BUFFER_SIZE = + (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, Integer.MAX_VALUE); /** * Sorts the specified range of the array using parallel merge @@ -140,10 +133,8 @@ private DualPivotQuicksort() {} * @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) { - new Sorter(a, tryIntAllocate(size), parallelism, low, size, 0).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); } @@ -470,8 +461,8 @@ static void mixedInsertionSort(int[] a, int low, int high) { } /* - * The index of the last element - * for pin insertion sort, exclusive. + * Split part into the golden ratio + * for pin and pair insertion sorts. */ int end = high - 3 * (size >> 3 << 1); @@ -669,7 +660,7 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int high) { if (sorter != null && (b = (int[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryIntAllocate(high - low)) == null) { + } else if ((b = (int[]) tryAllocate(a, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -794,7 +785,7 @@ private static void mergeParts(Merger merger, int[] dst, int k, /* * Merge the right part in parallel. */ - merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + d, mi1, hi1, mi2, hi2); /* * Iterate along the left part. @@ -839,7 +830,7 @@ static boolean tryRadixSort(Sorter sorter, int[] a, int low, int high) { */ if (sorter != null && (b = (int[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryIntAllocate(size)) == null) { + } else if ((b = (int[]) tryAllocate(a, size)) == null) { return false; } @@ -992,20 +983,6 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { a[p] = value; } - /** - * Tries to allocate memory for additional buffer. - * - * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static int[] tryIntAllocate(int size) { - try { - return size < MAX_4_BYTE_BUFFER_SIZE ? new int[size] : null; - } catch (OutOfMemoryError e) { - return null; - } - } - // #[long] /** @@ -1024,10 +1001,8 @@ private static int[] tryIntAllocate(int size) { * @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) { - new Sorter(a, tryLongAllocate(size), parallelism, low, size, 0).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); } @@ -1354,8 +1329,8 @@ static void mixedInsertionSort(long[] a, int low, int high) { } /* - * The index of the last element - * for pin insertion sort, exclusive. + * Split part into the golden ratio + * for pin and pair insertion sorts. */ int end = high - 3 * (size >> 3 << 1); @@ -1553,7 +1528,7 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int high) { if (sorter != null && (b = (long[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryLongAllocate(high - low)) == null) { + } else if ((b = (long[]) tryAllocate(a, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -1678,7 +1653,7 @@ private static void mergeParts(Merger merger, long[] dst, int k, /* * Merge the right part in parallel. */ - merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + d, mi1, hi1, mi2, hi2); /* * Iterate along the left part. @@ -1723,7 +1698,7 @@ static boolean tryRadixSort(Sorter sorter, long[] a, int low, int high) { */ if (sorter != null && (b = (long[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryLongAllocate(size)) == null) { + } else if ((b = (long[]) tryAllocate(a, size)) == null) { return false; } @@ -1896,20 +1871,6 @@ private static void pushDown(long[] a, int p, long value, int low, int high) { a[p] = value; } - /** - * Tries to allocate memory for additional buffer. - * - * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static long[] tryLongAllocate(int size) { - try { - return size < MAX_8_BYTE_BUFFER_SIZE ? new long[size] : null; - } catch (OutOfMemoryError e) { - return null; - } - } - // #[byte] /** @@ -2728,10 +2689,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) { - new Sorter(a, tryFloatAllocate(size), parallelism, low, size, 0).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); } @@ -3087,8 +3046,8 @@ static void mixedInsertionSort(float[] a, int low, int high) { } /* - * The index of the last element - * for pin insertion sort, exclusive. + * Split part into the golden ratio + * for pin and pair insertion sorts. */ int end = high - 3 * (size >> 3 << 1); @@ -3286,7 +3245,7 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int high) { if (sorter != null && (b = (float[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryFloatAllocate(high - low)) == null) { + } else if ((b = (float[]) tryAllocate(a, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -3411,7 +3370,7 @@ private static void mergeParts(Merger merger, float[] dst, int k, /* * Merge the right part in parallel. */ - merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + d, mi1, hi1, mi2, hi2); /* * Iterate along the left part. @@ -3456,7 +3415,7 @@ static boolean tryRadixSort(Sorter sorter, float[] a, int low, int high) { */ if (sorter != null && (b = (float[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryFloatAllocate(size)) == null) { + } else if ((b = (float[]) tryAllocate(a, size)) == null) { return false; } @@ -3586,20 +3545,6 @@ private static void pushDown(float[] a, int p, float value, int low, int high) { a[p] = value; } - /** - * Tries to allocate memory for additional buffer. - * - * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static float[] tryFloatAllocate(int size) { - try { - return size < MAX_4_BYTE_BUFFER_SIZE ? new float[size] : null; - } catch (OutOfMemoryError e) { - return null; - } - } - // #[double] /** @@ -3641,10 +3586,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) { - new Sorter(a, tryDoubleAllocate(size), parallelism, low, size, 0).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); } @@ -4000,8 +3943,8 @@ static void mixedInsertionSort(double[] a, int low, int high) { } /* - * The index of the last element - * for pin insertion sort, exclusive. + * Split part into the golden ratio + * for pin and pair insertion sorts. */ int end = high - 3 * (size >> 3 << 1); @@ -4199,7 +4142,7 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int high) { if (sorter != null && (b = (double[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryDoubleAllocate(high - low)) == null) { + } else if ((b = (double[]) tryAllocate(a, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -4324,7 +4267,7 @@ private static void mergeParts(Merger merger, double[] dst, int k, /* * Merge the right part in parallel. */ - merger.fork(dst, k + d, a1, mi1, hi1, a2, mi2, hi2); + merger.fork(k + d, mi1, hi1, mi2, hi2); /* * Iterate along the left part. @@ -4369,7 +4312,7 @@ static boolean tryRadixSort(Sorter sorter, double[] a, int low, int high) { */ if (sorter != null && (b = (double[]) sorter.b) != null) { offset = sorter.offset; - } else if ((b = tryDoubleAllocate(size)) == null) { + } else if ((b = (double[]) tryAllocate(a, size)) == null) { return false; } @@ -4553,43 +4496,30 @@ private static void pushDown(double[] a, int p, double value, int low, int high) a[p] = value; } - /** - * Tries to allocate memory for additional buffer. - * - * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static double[] tryDoubleAllocate(int size) { - try { - return size < MAX_8_BYTE_BUFFER_SIZE ? new double[size] : null; - } catch (OutOfMemoryError e) { - return null; - } - } - // #[class] /** * This class implements parallel sorting. */ private static final class Sorter extends CountedCompleter { + private static final long serialVersionUID = 123456789L; @SuppressWarnings("serial") private final Object a, b; private final int low, size, offset, depth; - private Sorter(Object a, Object b, int parallelism, int low, int size, int depth) { + private Sorter(Object a, int parallelism, int low, int size, int depth) { this.a = a; - this.b = b; this.low = low; this.size = size; this.offset = low; - while (b != null && (parallelism >>= 1) > 0 && (size >>= 8) > 0) { + while ((parallelism >>= 1) > 0 && (size >>= 8) > 0) { depth -= 2; } - this.depth = depth; + this.b = tryAllocate(a, this.size); + this.depth = b == null ? 0 : depth; } private Sorter(CountedCompleter parent, @@ -4655,6 +4585,7 @@ private void fork(int depth, int low, int high) { * This class implements parallel merging. */ private static final class Merger extends CountedCompleter { + private static final long serialVersionUID = 123456789L; @SuppressWarnings("serial") @@ -4694,9 +4625,39 @@ public void compute() { propagateCompletion(); } - private void fork(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(); } } + + /** + * Tries to allocate additional buffer. + * + * @param a the given array + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static Object tryAllocate(Object a, int size) { + try { + if (size > MAX_BUFFER_SIZE) { + return null; + } + if (a instanceof int[]) { + return new int[size]; + } + if (a instanceof long[]) { + return new long[size]; + } + if (a instanceof float[]) { + return new float[size]; + } + if (a instanceof double[]) { + return new double[size]; + } + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); + } catch (OutOfMemoryError e) { + return null; + } + } } From 4772617d6ac5bac24292fd190461cd1897c08553 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Tue, 1 Nov 2022 01:25:40 +0300 Subject: [PATCH 17/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Optimized mixed insertion sort * Optimized insertion sort * Optimized Radix sort * Updated microbenchmark --- .../classes/java/util/DualPivotQuicksort.java | 9236 ++++++++--------- test/jdk/java/util/Arrays/Sorting.java | 3596 +++---- .../java.base/java/util/SortingHelper.java | 666 +- .../openjdk/bench/java/util/ArraysSort.java | 465 +- 4 files changed, 6815 insertions(+), 7148 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index f3d06d635a551..bec5505c293ab 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -1,4663 +1,4573 @@ -/* - * Copyright (c) 2009, 2022, 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 - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package java.util; - -import java.util.concurrent.CountedCompleter; - -/** - * This class implements powerful and fully optimized versions, both - * sequential and parallel, of the Dual-Pivot Quicksort algorithm by - * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm - * offers O(n log(n)) performance on all data sets, and is typically - * faster than traditional (one-pivot) Quicksort implementations. - * - * There are also additional algorithms, invoked from the Dual-Pivot - * 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 2022.06.14 - * - * @since 1.7 * 14 ^ 20 - */ -final class DualPivotQuicksort { - - /** - * Prevents instantiation. - */ - private DualPivotQuicksort() {} - - /* ---------------- Insertion sort section ---------------- */ - - /** - * Max array size to use mixed insertion sort. - */ - 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 = 37; - - /* ----------------- Merging sort section ----------------- */ - - /** - * Min array size to use merging sort. - */ - private static final int MIN_MERGING_SORT_SIZE = 512; - - /** - * Min size of run to continue scanning. - */ - private static final int MIN_RUN_SIZE = 128; - - /* ------------------ Radix sort section ------------------ */ - - /** - * Min array size to use Radix sort. - */ - private static final int MIN_RADIX_SORT_SIZE = 768; - - /* ------------------ Counting sort section --------------- */ - - /** - * Min size of a byte array to use counting sort. - */ - private static final int MIN_BYTE_COUNTING_SORT_SIZE = 64; - - /** - * Min size of a short or char array to use counting sort. - */ - private static final int MIN_SHORT_OR_CHAR_COUNTING_SORT_SIZE = 1750; - - /* -------------------- Common section -------------------- */ - - /** - * Min array size to perform sorting in parallel. - */ - private static final int MIN_PARALLEL_SORT_SIZE = 1024; - - /** - * Max recursive depth before switching to heap sort. - */ - private static final int MAX_RECURSION_DEPTH = 64 << 1; - - /** - * Max size of additional buffer, - * limited by max_heap / 64 or 2 GB max. - */ - private static final int MAX_BUFFER_SIZE = - (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, Integer.MAX_VALUE); - - /** - * 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. - * - * @param a the array to be sorted - * @param parallelism the parallelism level - * @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(int[] a, int parallelism, int low, int high) { - 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 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 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) { - while (true) { - int size = high - low; - - /* - * Run adaptive mixed insertion sort on small non-leftmost parts. - */ - if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high); - return; - } - - /* - * Invoke insertion sort on small leftmost part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Try merging sort on large part. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - int a3 = a[e3]; - - boolean isRandom = - 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. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { int t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { int t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { int t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { int t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - /* - * Try Radix sort on large fully random data, - * taking into account parallel context. - */ - isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; - - if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) - && 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; - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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 pivot1 = a[e1]; - int pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - int ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively (possibly in parallel), - * excluding known pivots. - */ - if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.fork(bits | 1, lower + 1, upper); - sorter.fork(bits | 1, upper + 1, high); - } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); - } - - } 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 pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - 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 - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * 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.fork(bits | 1, upper, high); - } else { - sort(sorter, a, bits | 1, upper, high); - } - } - high = lower; // Iterate along the left part - } - } - - /** - * Sorts the specified range of the array using mixed 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 - * is less than any elements from the given part. Therefore, - * expensive check of the left range can be skipped on each - * iteration unless it is the leftmost call. - * - * @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 mixedInsertionSort(int[] a, int low, int high) { - int size = high - low; - - /* - * Invoke simple insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - for (int i; ++low < high; ) { - int ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - return; - } - - /* - * Split part into the golden ratio - * for pin and pair insertion sorts. - */ - int end = high - 3 * (size >> 3 << 1); - - /* - * Start with pin insertion sort. - */ - for (int i, p = high; ++low < end; ) { - int ai = a[i = low], pin = a[--p]; - - /* - * Swap larger element with pin. - */ - if (ai > pin) { - ai = pin; - a[p] = a[i]; - } - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - - /* - * Finish with pair insertion sort. - */ - for (int i; low < high; ++low) { - int 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) { - - 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]) { - - 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; - } - } - } - - /** - * 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(int[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - int ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - - /** - * 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, 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 tryMergingSort(Sorter sorter, int[] a, int low, int high) { - - /* - * The element run[i] holds the start index - * of i-th sequence in non-descending order. - */ - int count = 1; - int[] run = null; - - /* - * Identify all possible runs. - */ - for (int k = low + 1, last = low; k < high; ) { - - /* - * Find the next run. - */ - if (a[k - 1] < a[k]) { - - // Identify ascending sequence - while (++k < high && a[k - 1] <= a[k]); - - } else if (a[k - 1] > a[k]) { - - // Identify descending sequence - while (++k < high && a[k - 1] >= a[k]); - - // Reverse into ascending order - for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { - int ai = a[i]; a[i] = a[j]; a[j] = ai; - } - } else { // Identify constant sequence - for (int ak = a[k]; ++k < high && ak == a[k]; ); - - if (k < high) { - continue; - } - } - - /* - * 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) { - /* - * Array is monotonous sequence - * and therefore already sorted. - */ - return true; - } - - 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; - } - } - - /* - * Save the current run. - */ - run[count] = (last = k); - - /* - * Check single-element run at the end. - */ - if (++k == high) { - --k; - } - } - - /* - * Merge all runs. - */ - if (count > 1) { - int[] b; int offset = low; - - if (sorter != null && (b = (int[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, high - low)) == null) { - return false; - } - mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); - } - return true; - } - - /** - * Merges the specified runs. - * - * @param a the source array - * @param b the temporary buffer used in merging - * @param offset the start index in the source, inclusive - * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) - * @param parallel indicates whether merging is performed in parallel - * @param run the start indexes of the runs, inclusive - * @param lo the start index of the first run, inclusive - * @param hi the start index of the last run, inclusive - * @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) { - - if (hi - lo == 1) { - if (aim >= 0) { - return a; - } - System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); - return b; - } - - /* - * Split into approximately equal parts. - */ - int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; - while (run[++mi + 1] <= rmi); - - /* - * Merge runs of each part. - */ - 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]; - int lo1 = a1 == b ? run[lo] - offset : run[lo]; - int hi1 = a1 == b ? run[mi] - offset : run[mi]; - int lo2 = a2 == b ? run[mi] - offset : run[mi]; - int hi2 = a2 == b ? run[hi] - offset : run[hi]; - - /* - * 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); - } - return dst; - } - - /** - * Merges the sorted parts. - * - * @param merger parallel context - * @param dst the destination where parts are merged - * @param k the start index of the destination, inclusive - * @param a1 the first part - * @param lo1 the start index of the first part, inclusive - * @param hi1 the end index of the first part, exclusive - * @param a2 the second part - * @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) { - - if (merger != null && a1 == a2) { - - while (true) { - - /* - * The first part must be larger. - */ - if (hi1 - lo1 < hi2 - lo2) { - int lo = lo1; lo1 = lo2; lo2 = lo; - int hi = hi1; hi1 = hi2; hi2 = hi; - } - - /* - * Small parts will be merged sequentially. - */ - if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { - break; - } - - /* - * Find the median of the larger part. - */ - int mi1 = (lo1 + hi1) >>> 1; - int key = a1[mi1]; - int mi2 = hi2; - - /* - * Divide the smaller part. - */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; - - if (key > a2[t]) { - loo = t + 1; - } else { - mi2 = t; - } - } - - /* - * Reserve space for the left part. - */ - int d = mi2 - lo2 + mi1 - lo1; - - /* - * Merge the right part in parallel. - */ - merger.fork(k + d, mi1, hi1, mi2, hi2); - - /* - * Iterate along the left part. - */ - hi1 = mi1; - hi2 = mi2; - } - } - - /* - * Merge small parts sequentially. - */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; - } - if (dst != a1 || k < lo1) { - while (lo1 < hi1) { - dst[k++] = a1[lo1++]; - } - } - if (dst != a2 || k < lo2) { - while (lo2 < hi2) { - dst[k++] = a2[lo2++]; - } - } - } - - /** - * 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, int[] a, int low, int high) { - int[] b; int offset = low, size = high - low; - - /* - * Allocate additional buffer. - */ - if (sorter != null && (b = (int[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[a[i] & 0x3FF]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(b[i] >>> 10) & 0x7FF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(a[i] >>> 10) & 0x7FF]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(b[i] >>> 21) ^ 0x400]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 creates histogram. - * - * @param count the count array - * @param last the last index of count array - * @param total the total number of elements - * @param high the index of the last element, exclusive - * @return {@code true} if the digit must be processed, otherwise {@code false} - */ - private static boolean processDigit(int[] count, int last, int total, int high) { - - /* - * Check if we can skip given digit. - */ - for (int c : count) { - if (c == total) { - return false; - } - if (c < 0) { - break; - } - } - - /* - * Compute the histogram. - */ - count[last] += high; - - for (int i = last; i > 0; --i) { - count[i - 1] += count[i]; - } - 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. - * - * @param a the array to be sorted - * @param parallelism the parallelism level - * @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(long[] a, int parallelism, int low, int high) { - 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 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 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) { - while (true) { - int size = high - low; - - /* - * Run adaptive mixed insertion sort on small non-leftmost parts. - */ - if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high); - return; - } - - /* - * Invoke insertion sort on small leftmost part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Try merging sort on large part. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - long a3 = a[e3]; - - boolean isRandom = - 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. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { long t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { long t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { long t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { long t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - /* - * Try Radix sort on large fully random data, - * taking into account parallel context. - */ - isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; - - if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) - && 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; - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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. - */ - long pivot1 = a[e1]; - long pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - long ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively (possibly in parallel), - * excluding known pivots. - */ - if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.fork(bits | 1, lower + 1, upper); - sorter.fork(bits | 1, upper + 1, high); - } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); - } - - } else { // Partitioning with one pivot - - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - long pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - 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 - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * 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.fork(bits | 1, upper, high); - } else { - sort(sorter, a, bits | 1, upper, high); - } - } - high = lower; // Iterate along the left part - } - } - - /** - * Sorts the specified range of the array using mixed 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 - * is less than any elements from the given part. Therefore, - * expensive check of the left range can be skipped on each - * iteration unless it is the leftmost call. - * - * @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 mixedInsertionSort(long[] a, int low, int high) { - int size = high - low; - - /* - * Invoke simple insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - for (int i; ++low < high; ) { - long ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - return; - } - - /* - * Split part into the golden ratio - * for pin and pair insertion sorts. - */ - int end = high - 3 * (size >> 3 << 1); - - /* - * Start with pin insertion sort. - */ - for (int i, p = high; ++low < end; ) { - long ai = a[i = low], pin = a[--p]; - - /* - * Swap larger element with pin. - */ - if (ai > pin) { - ai = pin; - a[p] = a[i]; - } - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - - /* - * Finish with pair insertion sort. - */ - for (int i; low < high; ++low) { - long 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) { - - 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]) { - - 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; - } - } - } - - /** - * 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(long[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - long ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - - /** - * 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, 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 tryMergingSort(Sorter sorter, long[] a, int low, int high) { - - /* - * The element run[i] holds the start index - * of i-th sequence in non-descending order. - */ - int count = 1; - int[] run = null; - - /* - * Identify all possible runs. - */ - for (int k = low + 1, last = low; k < high; ) { - - /* - * Find the next run. - */ - if (a[k - 1] < a[k]) { - - // Identify ascending sequence - while (++k < high && a[k - 1] <= a[k]); - - } else if (a[k - 1] > a[k]) { - - // Identify descending sequence - while (++k < high && a[k - 1] >= a[k]); - - // Reverse into ascending order - for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { - long ai = a[i]; a[i] = a[j]; a[j] = ai; - } - } else { // Identify constant sequence - for (long ak = a[k]; ++k < high && ak == a[k]; ); - - if (k < high) { - continue; - } - } - - /* - * 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) { - /* - * Array is monotonous sequence - * and therefore already sorted. - */ - return true; - } - - 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; - } - } - - /* - * Save the current run. - */ - run[count] = (last = k); - - /* - * Check single-element run at the end. - */ - if (++k == high) { - --k; - } - } - - /* - * Merge all runs. - */ - if (count > 1) { - long[] b; int offset = low; - - if (sorter != null && (b = (long[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, high - low)) == null) { - return false; - } - mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); - } - return true; - } - - /** - * Merges the specified runs. - * - * @param a the source array - * @param b the temporary buffer used in merging - * @param offset the start index in the source, inclusive - * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) - * @param parallel indicates whether merging is performed in parallel - * @param run the start indexes of the runs, inclusive - * @param lo the start index of the first run, inclusive - * @param hi the start index of the last run, inclusive - * @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) { - - if (hi - lo == 1) { - if (aim >= 0) { - return a; - } - System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); - return b; - } - - /* - * Split into approximately equal parts. - */ - int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; - while (run[++mi + 1] <= rmi); - - /* - * Merge runs of each part. - */ - 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]; - int lo1 = a1 == b ? run[lo] - offset : run[lo]; - int hi1 = a1 == b ? run[mi] - offset : run[mi]; - int lo2 = a2 == b ? run[mi] - offset : run[mi]; - int hi2 = a2 == b ? run[hi] - offset : run[hi]; - - /* - * 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); - } - return dst; - } - - /** - * Merges the sorted parts. - * - * @param merger parallel context - * @param dst the destination where parts are merged - * @param k the start index of the destination, inclusive - * @param a1 the first part - * @param lo1 the start index of the first part, inclusive - * @param hi1 the end index of the first part, exclusive - * @param a2 the second part - * @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) { - - if (merger != null && a1 == a2) { - - while (true) { - - /* - * The first part must be larger. - */ - if (hi1 - lo1 < hi2 - lo2) { - int lo = lo1; lo1 = lo2; lo2 = lo; - int hi = hi1; hi1 = hi2; hi2 = hi; - } - - /* - * Small parts will be merged sequentially. - */ - if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { - break; - } - - /* - * Find the median of the larger part. - */ - int mi1 = (lo1 + hi1) >>> 1; - long key = a1[mi1]; - int mi2 = hi2; - - /* - * Divide the smaller part. - */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; - - if (key > a2[t]) { - loo = t + 1; - } else { - mi2 = t; - } - } - - /* - * Reserve space for the left part. - */ - int d = mi2 - lo2 + mi1 - lo1; - - /* - * Merge the right part in parallel. - */ - merger.fork(k + d, mi1, hi1, mi2, hi2); - - /* - * Iterate along the left part. - */ - hi1 = mi1; - hi2 = mi2; - } - } - - /* - * Merge small parts sequentially. - */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; - } - if (dst != a1 || k < lo1) { - while (lo1 < hi1) { - dst[k++] = a1[lo1++]; - } - } - if (dst != a2 || k < lo2) { - while (lo2 < hi2) { - dst[k++] = a2[lo2++]; - } - } - } - - /** - * 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, long[] a, int low, int high) { - long[] b; int offset = low, size = high - low; - - /* - * Allocate additional buffer. - */ - if (sorter != null && (b = (long[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - boolean processDigit4 = processDigit(count4, 2047, -size, high); - boolean processDigit5 = processDigit(count5, 2047, -size, high); - boolean processDigit6 = processDigit(count6, 1023, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[(int) (a[i] & 0x3FF)]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(int) ((b[i] >>> 10) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(int) ((a[i] >>> 10) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(int) ((b[i] >>> 21) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count3[(int) ((a[i] >>> 21) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 4-th digit. - */ - if (processDigit4) { - if (processDigit1 ^ processDigit2 ^ processDigit3) { - for (int i = start; i < last; ++i) { - a[count4[(int) ((b[i] >>> 32) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count5[(int) ((b[i] >>> 43) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count6[(int) ((b[i] >>> 54) ^ 0x200)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 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(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 - - if (k > high) { - break; - } - if (k == high || a[k] < a[k - 1]) { - --k; - } - if (a[k] <= value) { - break; - } - } - a[p] = value; - } - -// #[byte] - - /** - * 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 - */ - 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); - } - } - - /** - * The number of distinct byte values. - */ - private static final int NUM_BYTE_VALUES = 1 << 8; - - /** - * Max index of byte counter. - */ - private static final int MAX_BYTE_INDEX = Byte.MAX_VALUE + NUM_BYTE_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(byte[] a, int low, int high) { - int[] count = new int[NUM_BYTE_VALUES]; - - /* - * Compute the histogram for all values. - */ - for (int i = high; i > low; ++count[a[--i] & 0xFF]); - - /* - * 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 (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 value = i & 0xFF; - int c = count[value]; - - do { - a[--high] = (byte) value; - } while (--c > 0); - } - } - } - - /** - * 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[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - -// #[char] - - /** - * 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(char[] 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); - } - } - - /** - * 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 all values. - */ - 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 c = count[i]; - - do { - a[--high] = (char) i; - } while (--c > 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 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 size = high - low; - - /* - * Invoke insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Switch to counting sort, if execution time is quadratic. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - char a3 = a[e3]; - - /* - * Sort these elements in place by the combination - * of 4-element sorting network and insertion sort. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { char t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { char t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { char t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { char t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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. - */ - char pivot1 = a[e1]; - char pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - char ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively, - * excluding known pivots. - */ - sort(a, bits | 1, lower + 1, upper); - sort(a, bits | 1, upper + 1, high); - - } else { // Partitioning with one pivot - - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - char pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - for (int k = ++upper; --k > lower; ) { - char ak = a[k]; - - if (ak != pivot) { - a[k] = pivot; - - if (ak < pivot) { // Move a[k] to the left side - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * Sort the right part, excluding known pivot. - * All elements from the central part are - * equal and therefore already sorted. - */ - sort(a, bits | 1, upper, high); - } - high = lower; // Iterate along the left part - } - } - - /** - * 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(char[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - char ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - -// #[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); - } - } - - /** - * 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 the histogram for all values. - */ - for (int i = high; i > low; ++count[a[--i] & 0xFFFF]); - - /* - * Put 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); - } - } - } - - /** - * 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 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 size = high - low; - - /* - * Invoke insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Switch to counting sort, if execution time is quadratic. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - short a3 = a[e3]; - - /* - * Sort these elements in place by the combination - * of 4-element sorting network and insertion sort. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { short t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { short t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { short t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { short t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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. - */ - short pivot1 = a[e1]; - short pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - short ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively, - * excluding known pivots. - */ - sort(a, bits | 1, lower + 1, upper); - sort(a, bits | 1, upper + 1, high); - - } else { // Partitioning with one pivot - - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - short pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - for (int k = ++upper; --k > lower; ) { - short ak = a[k]; - - if (ak != pivot) { - a[k] = pivot; - - if (ak < pivot) { // Move a[k] to the left side - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * Sort the right part, excluding known pivot. - * All elements from the central part are - * equal and therefore already sorted. - */ - sort(a, bits | 1, upper, high); - } - high = lower; // Iterate along the left part - } - } - - /** - * 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(short[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - short ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - -// #[float] - - /** - * 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. - * - * @param a the array to be sorted - * @param parallelism the parallelism level - * @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(float[] a, int parallelism, int low, int high) { - /* - * Phase 1. Count the number of negative zero -0.0f, - * turn them into positive zero, and move all NaNs - * to the end of the array. - */ - int numNegativeZero = 0; - - for (int k = high; k > low; ) { - float ak = a[--k]; - - if (ak == 0.0f && Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f - numNegativeZero += 1; - a[k] = 0.0f; - } else if (ak != ak) { // ak is NaN - a[k] = a[--high]; - a[high] = ak; - } - } - - /* - * Phase 2. Sort everything except NaNs, - * which are already in place. - */ - 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); - } - - /* - * Phase 3. Turn positive zero 0.0f - * back into negative zero -0.0f. - */ - if (++numNegativeZero == 1) { - return; - } - - /* - * Find the position one less than - * the index of the first zero. - */ - while (low <= high) { - int middle = (low + high) >>> 1; - - if (a[middle] < 0) { - low = middle + 1; - } else { - high = middle - 1; - } - } - - /* - * Replace the required number of 0.0f by -0.0f. - */ - while (--numNegativeZero > 0) { - a[++high] = -0.0f; - } - } - - /** - * 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 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) { - while (true) { - int size = high - low; - - /* - * Run adaptive mixed insertion sort on small non-leftmost parts. - */ - if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high); - return; - } - - /* - * Invoke insertion sort on small leftmost part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Try merging sort on large part. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - float a3 = a[e3]; - - boolean isRandom = - 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. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { float t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { float t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { float t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { float t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - /* - * Try Radix sort on large fully random data, - * taking into account parallel context. - */ - isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; - - if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) - && 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; - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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. - */ - float pivot1 = a[e1]; - float pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - float ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively (possibly in parallel), - * excluding known pivots. - */ - if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.fork(bits | 1, lower + 1, upper); - sorter.fork(bits | 1, upper + 1, high); - } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); - } - - } else { // Partitioning with one pivot - - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - float pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - 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 - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * 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.fork(bits | 1, upper, high); - } else { - sort(sorter, a, bits | 1, upper, high); - } - } - high = lower; // Iterate along the left part - } - } - - /** - * Sorts the specified range of the array using mixed 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 - * is less than any elements from the given part. Therefore, - * expensive check of the left range can be skipped on each - * iteration unless it is the leftmost call. - * - * @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 mixedInsertionSort(float[] a, int low, int high) { - int size = high - low; - - /* - * Invoke simple insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - for (int i; ++low < high; ) { - float ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - return; - } - - /* - * Split part into the golden ratio - * for pin and pair insertion sorts. - */ - int end = high - 3 * (size >> 3 << 1); - - /* - * Start with pin insertion sort. - */ - for (int i, p = high; ++low < end; ) { - float ai = a[i = low], pin = a[--p]; - - /* - * Swap larger element with pin. - */ - if (ai > pin) { - ai = pin; - a[p] = a[i]; - } - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - - /* - * Finish with pair insertion sort. - */ - for (int i; low < high; ++low) { - float 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) { - - 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]) { - - 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; - } - } - } - - /** - * 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(float[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - float ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - - /** - * 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, 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 tryMergingSort(Sorter sorter, float[] a, int low, int high) { - - /* - * The element run[i] holds the start index - * of i-th sequence in non-descending order. - */ - int count = 1; - int[] run = null; - - /* - * Identify all possible runs. - */ - for (int k = low + 1, last = low; k < high; ) { - - /* - * Find the next run. - */ - if (a[k - 1] < a[k]) { - - // Identify ascending sequence - while (++k < high && a[k - 1] <= a[k]); - - } else if (a[k - 1] > a[k]) { - - // Identify descending sequence - while (++k < high && a[k - 1] >= a[k]); - - // Reverse into ascending order - for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { - float ai = a[i]; a[i] = a[j]; a[j] = ai; - } - } else { // Identify constant sequence - for (float ak = a[k]; ++k < high && ak == a[k]; ); - - if (k < high) { - continue; - } - } - - /* - * 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) { - /* - * Array is monotonous sequence - * and therefore already sorted. - */ - return true; - } - - 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; - } - } - - /* - * Save the current run. - */ - run[count] = (last = k); - - /* - * Check single-element run at the end. - */ - if (++k == high) { - --k; - } - } - - /* - * Merge all runs. - */ - if (count > 1) { - float[] b; int offset = low; - - if (sorter != null && (b = (float[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, high - low)) == null) { - return false; - } - mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); - } - return true; - } - - /** - * Merges the specified runs. - * - * @param a the source array - * @param b the temporary buffer used in merging - * @param offset the start index in the source, inclusive - * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) - * @param parallel indicates whether merging is performed in parallel - * @param run the start indexes of the runs, inclusive - * @param lo the start index of the first run, inclusive - * @param hi the start index of the last run, inclusive - * @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) { - - if (hi - lo == 1) { - if (aim >= 0) { - return a; - } - System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); - return b; - } - - /* - * Split into approximately equal parts. - */ - int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; - while (run[++mi + 1] <= rmi); - - /* - * Merge runs of each part. - */ - 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]; - int lo1 = a1 == b ? run[lo] - offset : run[lo]; - int hi1 = a1 == b ? run[mi] - offset : run[mi]; - int lo2 = a2 == b ? run[mi] - offset : run[mi]; - int hi2 = a2 == b ? run[hi] - offset : run[hi]; - - /* - * 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); - } - return dst; - } - - /** - * Merges the sorted parts. - * - * @param merger parallel context - * @param dst the destination where parts are merged - * @param k the start index of the destination, inclusive - * @param a1 the first part - * @param lo1 the start index of the first part, inclusive - * @param hi1 the end index of the first part, exclusive - * @param a2 the second part - * @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) { - - if (merger != null && a1 == a2) { - - while (true) { - - /* - * The first part must be larger. - */ - if (hi1 - lo1 < hi2 - lo2) { - int lo = lo1; lo1 = lo2; lo2 = lo; - int hi = hi1; hi1 = hi2; hi2 = hi; - } - - /* - * Small parts will be merged sequentially. - */ - if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { - break; - } - - /* - * Find the median of the larger part. - */ - int mi1 = (lo1 + hi1) >>> 1; - float key = a1[mi1]; - int mi2 = hi2; - - /* - * Divide the smaller part. - */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; - - if (key > a2[t]) { - loo = t + 1; - } else { - mi2 = t; - } - } - - /* - * Reserve space for the left part. - */ - int d = mi2 - lo2 + mi1 - lo1; - - /* - * Merge the right part in parallel. - */ - merger.fork(k + d, mi1, hi1, mi2, hi2); - - /* - * Iterate along the left part. - */ - hi1 = mi1; - hi2 = mi2; - } - } - - /* - * Merge small parts sequentially. - */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; - } - if (dst != a1 || k < lo1) { - while (lo1 < hi1) { - dst[k++] = a1[lo1++]; - } - } - if (dst != a2 || k < lo2) { - while (lo2 < hi2) { - dst[k++] = a2[lo2++]; - } - } - } - - /** - * 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 = (float[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[fti(a[i]) & 0x3FF]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(fti(b[i]) >>> 10) & 0x7FF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(fti(a[i]) >>> 10) & 0x7FF]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(fti(b[i]) >>> 21) & 0x7FF]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 - * 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. - * - * @param a the array to be sorted - * @param parallelism the parallelism level - * @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(double[] a, int parallelism, int low, int high) { - /* - * Phase 1. Count the number of negative zero -0.0d, - * turn them into positive zero, and move all NaNs - * to the end of the array. - */ - int numNegativeZero = 0; - - for (int k = high; k > low; ) { - double ak = a[--k]; - - if (ak == 0.0d && Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d - numNegativeZero += 1; - a[k] = 0.0d; - } else if (ak != ak) { // ak is NaN - a[k] = a[--high]; - a[high] = ak; - } - } - - /* - * Phase 2. Sort everything except NaNs, - * which are already in place. - */ - 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); - } - - /* - * Phase 3. Turn positive zero 0.0d - * back into negative zero -0.0d. - */ - if (++numNegativeZero == 1) { - return; - } - - /* - * Find the position one less than - * the index of the first zero. - */ - while (low <= high) { - int middle = (low + high) >>> 1; - - if (a[middle] < 0) { - low = middle + 1; - } else { - high = middle - 1; - } - } - - /* - * Replace the required number of 0.0d by -0.0d. - */ - while (--numNegativeZero > 0) { - a[++high] = -0.0d; - } - } - - /** - * 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 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) { - while (true) { - int size = high - low; - - /* - * Run adaptive mixed insertion sort on small non-leftmost parts. - */ - if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { - mixedInsertionSort(a, low, high); - return; - } - - /* - * Invoke insertion sort on small leftmost part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - insertionSort(a, low, high); - return; - } - - /* - * Try merging sort on large part. - */ - 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. - */ - int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; - - /* - * Five elements around (and including) the central element - * will be used for pivot selection as described below. The - * 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; - int e2 = (e1 + e3) >>> 1; - int e4 = (e3 + e5) >>> 1; - double a3 = a[e3]; - - boolean isRandom = - 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. - * - * 1 ------------o-----o------------ - * | | - * 2 ------o-----|-----o-----o------ - * | | | - * 4 ------|-----o-----o-----o------ - * | | - * 5 ------o-----------o------------ - */ - if (a[e2] > a[e5]) { double t = a[e2]; a[e2] = a[e5]; a[e5] = t; } - if (a[e1] > a[e4]) { double t = a[e1]; a[e1] = a[e4]; a[e4] = t; } - if (a[e1] > a[e2]) { double t = a[e1]; a[e1] = a[e2]; a[e2] = t; } - if (a[e4] > a[e5]) { double t = a[e4]; a[e4] = a[e5]; a[e5] = 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; - } else { - a[e3] = a[e2]; a[e2] = a3; - } - } else if (a3 > a[e4]) { - if (a3 > a[e5]) { - a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; - } else { - a[e3] = a[e4]; a[e4] = a3; - } - } - - /* - * Try Radix sort on large fully random data, - * taking into account parallel context. - */ - isRandom &= a[e1] < a[e2] && a[e2] < a[e3] & a[e3] < a[e4] && a[e4] < a[e5]; - - if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) - && 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; - } - - // Pointers - int lower = low; // The index of the last element of the left part - int upper = end; // The index of the first element of the right part - - /* - * Partitioning with two pivots on array of 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. - */ - double pivot1 = a[e1]; - double pivot2 = a[e5]; - - /* - * 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]; - - /* - * 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 - * - * 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 - * - * Invariants: - * - * all in (low, lower] < pivot1 - * all in (k, upper) in [pivot1, pivot2] - * all in [upper, end) > pivot2 - */ - for (int unused = --lower, k = ++upper; --k > lower; ) { - double ak = a[k]; - - if (ak < pivot1) { // Move a[k] to the left side - while (a[++lower] < pivot1) { - if (lower == k) { - break; - } - } - 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 side - a[k] = a[--upper]; - a[upper] = ak; - } - } - - /* - * Swap the pivots into their final positions. - */ - a[low] = a[lower]; a[lower] = pivot1; - a[end] = a[upper]; a[upper] = pivot2; - - /* - * Sort non-left parts recursively (possibly in parallel), - * excluding known pivots. - */ - if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { - sorter.fork(bits | 1, lower + 1, upper); - sorter.fork(bits | 1, upper + 1, high); - } else { - sort(sorter, a, bits | 1, lower + 1, upper); - sort(sorter, a, bits | 1, upper + 1, high); - } - - } else { // Partitioning with one pivot - - /* - * Use the third of the five sorted elements as the pivot. - * This value is inexpensive approximation of the median. - */ - double pivot = a[e3]; - - /* - * 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]; - - /* - * Dutch National Flag partitioning - * - * left part central part right part - * +------------------------------------------------------+ - * | < pivot | ? | == pivot | > 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 - * - * Invariants: - * - * all in (low, lower] < pivot - * all in (k, upper) == pivot - * all in [upper, end] > pivot - */ - 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 - 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 - a[--upper] = ak; - } - } - } - - /* - * Swap the pivot into its final position. - */ - a[low] = a[lower]; a[lower] = pivot; - - /* - * 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.fork(bits | 1, upper, high); - } else { - sort(sorter, a, bits | 1, upper, high); - } - } - high = lower; // Iterate along the left part - } - } - - /** - * Sorts the specified range of the array using mixed 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 - * is less than any elements from the given part. Therefore, - * expensive check of the left range can be skipped on each - * iteration unless it is the leftmost call. - * - * @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 mixedInsertionSort(double[] a, int low, int high) { - int size = high - low; - - /* - * Invoke simple insertion sort on small part. - */ - if (size < MAX_INSERTION_SORT_SIZE) { - for (int i; ++low < high; ) { - double ai = a[i = low]; - - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - return; - } - - /* - * Split part into the golden ratio - * for pin and pair insertion sorts. - */ - int end = high - 3 * (size >> 3 << 1); - - /* - * Start with pin insertion sort. - */ - for (int i, p = high; ++low < end; ) { - double ai = a[i = low], pin = a[--p]; - - /* - * Swap larger element with pin. - */ - if (ai > pin) { - ai = pin; - a[p] = a[i]; - } - - /* - * Insert element into sorted part. - */ - while (ai < a[--i]) { - a[i + 1] = a[i]; - } - a[i + 1] = ai; - } - - /* - * Finish with pair insertion sort. - */ - 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) { - - 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]) { - - 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; - } - } - } - - /** - * 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(double[] a, int low, int high) { - for (int i, k = low; ++k < high; ) { - double ai = a[i = k]; - - if (ai < a[low]) { - - do { - a[i] = a[i - 1]; - } while (--i > low); - - a[low] = ai; - - } else if (ai < a[i - 1]) { - - do { - a[i] = a[i - 1]; - } while (ai < a[--i]); - - a[i + 1] = ai; - } - } - } - - /** - * 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, 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 tryMergingSort(Sorter sorter, double[] a, int low, int high) { - - /* - * The element run[i] holds the start index - * of i-th sequence in non-descending order. - */ - int count = 1; - int[] run = null; - - /* - * Identify all possible runs. - */ - for (int k = low + 1, last = low; k < high; ) { - - /* - * Find the next run. - */ - if (a[k - 1] < a[k]) { - - // Identify ascending sequence - while (++k < high && a[k - 1] <= a[k]); - - } else if (a[k - 1] > a[k]) { - - // Identify descending sequence - while (++k < high && a[k - 1] >= a[k]); - - // Reverse into ascending order - for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { - double ai = a[i]; a[i] = a[j]; a[j] = ai; - } - } else { // Identify constant sequence - for (double ak = a[k]; ++k < high && ak == a[k]; ); - - if (k < high) { - continue; - } - } - - /* - * 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) { - /* - * Array is monotonous sequence - * and therefore already sorted. - */ - return true; - } - - 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; - } - } - - /* - * Save the current run. - */ - run[count] = (last = k); - - /* - * Check single-element run at the end. - */ - if (++k == high) { - --k; - } - } - - /* - * Merge all runs. - */ - if (count > 1) { - double[] b; int offset = low; - - if (sorter != null && (b = (double[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, high - low)) == null) { - return false; - } - mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); - } - return true; - } - - /** - * Merges the specified runs. - * - * @param a the source array - * @param b the temporary buffer used in merging - * @param offset the start index in the source, inclusive - * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) - * @param parallel indicates whether merging is performed in parallel - * @param run the start indexes of the runs, inclusive - * @param lo the start index of the first run, inclusive - * @param hi the start index of the last run, inclusive - * @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) { - - if (hi - lo == 1) { - if (aim >= 0) { - return a; - } - System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); - return b; - } - - /* - * Split into approximately equal parts. - */ - int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; - while (run[++mi + 1] <= rmi); - - /* - * Merge runs of each part. - */ - 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]; - int lo1 = a1 == b ? run[lo] - offset : run[lo]; - int hi1 = a1 == b ? run[mi] - offset : run[mi]; - int lo2 = a2 == b ? run[mi] - offset : run[mi]; - int hi2 = a2 == b ? run[hi] - offset : run[hi]; - - /* - * 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); - } - return dst; - } - - /** - * Merges the sorted parts. - * - * @param merger parallel context - * @param dst the destination where parts are merged - * @param k the start index of the destination, inclusive - * @param a1 the first part - * @param lo1 the start index of the first part, inclusive - * @param hi1 the end index of the first part, exclusive - * @param a2 the second part - * @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) { - - if (merger != null && a1 == a2) { - - while (true) { - - /* - * The first part must be larger. - */ - if (hi1 - lo1 < hi2 - lo2) { - int lo = lo1; lo1 = lo2; lo2 = lo; - int hi = hi1; hi1 = hi2; hi2 = hi; - } - - /* - * Small parts will be merged sequentially. - */ - if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { - break; - } - - /* - * Find the median of the larger part. - */ - int mi1 = (lo1 + hi1) >>> 1; - double key = a1[mi1]; - int mi2 = hi2; - - /* - * Divide the smaller part. - */ - for (int loo = lo2; loo < mi2; ) { - int t = (loo + mi2) >>> 1; - - if (key > a2[t]) { - loo = t + 1; - } else { - mi2 = t; - } - } - - /* - * Reserve space for the left part. - */ - int d = mi2 - lo2 + mi1 - lo1; - - /* - * Merge the right part in parallel. - */ - merger.fork(k + d, mi1, hi1, mi2, hi2); - - /* - * Iterate along the left part. - */ - hi1 = mi1; - hi2 = mi2; - } - } - - /* - * Merge small parts sequentially. - */ - while (lo1 < hi1 && lo2 < hi2) { - dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; - } - if (dst != a1 || k < lo1) { - while (lo1 < hi1) { - dst[k++] = a1[lo1++]; - } - } - if (dst != a2 || k < lo2) { - while (lo2 < hi2) { - dst[k++] = a2[lo2++]; - } - } - } - - /** - * 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 = (double[]) sorter.b) != null) { - offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, 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, 1023, -size, high); - boolean processDigit2 = processDigit(count2, 2047, -size, high); - boolean processDigit3 = processDigit(count3, 2047, -size, high); - boolean processDigit4 = processDigit(count4, 2047, -size, high); - boolean processDigit5 = processDigit(count5, 2047, -size, high); - boolean processDigit6 = processDigit(count6, 1023, -size, high); - - /* - * Process the 1-st digit. - */ - if (processDigit1) { - for (int i = low; i < high; ++i) { - b[count1[(int) (dtl(a[i]) & 0x3FF)]++ - offset] = a[i]; - } - } - - /* - * Process the 2-nd digit. - */ - if (processDigit2) { - if (processDigit1) { - for (int i = start; i < last; ++i) { - a[count2[(int) ((dtl(b[i]) >>> 10) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - b[count2[(int) ((dtl(a[i]) >>> 10) & 0x7FF)]++ - offset] = a[i]; - } - } - } - - /* - * Process the 3-rd digit. - */ - if (processDigit3) { - if (processDigit1 ^ processDigit2) { - for (int i = start; i < last; ++i) { - a[count3[(int) ((dtl(b[i]) >>> 21) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count4[(int) ((dtl(b[i]) >>> 32) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count5[(int) ((dtl(b[i]) >>> 43) & 0x7FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = start; i < last; ++i) { - a[count6[(int) ((dtl(b[i]) >>> 54) & 0x3FF)]++] = b[i]; - } - } else { - for (int i = low; i < high; ++i) { - 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 = 123456789L; - - @SuppressWarnings("serial") - private final Object a, b; - private final int low, size, offset, depth; - - private Sorter(Object a, int parallelism, int low, int size, int depth) { - this.a = a; - this.low = low; - this.size = size; - this.offset = low; - - while ((parallelism >>= 1) > 0 && (size >>= 8) > 0) { - depth -= 2; - } - this.b = tryAllocate(a, this.size); - this.depth = b == null ? 0 : depth; - } - - private Sorter(CountedCompleter parent, - Object a, Object b, int low, int size, int offset, int depth) { - super(parent); - this.a = a; - this.b = b; - this.low = low; - this.size = size; - this.offset = offset; - this.depth = depth; - } - - @Override - 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(); - } else { - if (a instanceof int[]) { - sort(this, (int[]) a, depth, low, low + size); - } else if (a instanceof long[]) { - sort(this, (long[]) a, depth, low, low + size); - } else if (a instanceof float[]) { - sort(this, (float[]) a, depth, low, low + size); - } else if (a instanceof double[]) { - sort(this, (double[]) a, depth, low, low + size); - } else { - throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); - } - } - tryComplete(); - } - - @Override - public void onCompletion(CountedCompleter parent) { - 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 - ).invoke(); - } - } - - private void fork(int depth, int low, int high) { - addToPendingCount(1); - 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 = 123456789L; - - @SuppressWarnings("serial") - private final Object 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) { - super(parent); - this.dst = dst; - this.k = k; - this.a1 = a1; - this.lo1 = lo1; - this.hi1 = hi1; - this.a2 = a2; - this.lo2 = lo2; - this.hi2 = hi2; - } - - @Override - public void compute() { - if (dst instanceof int[]) { - mergeParts(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); - } else if (dst instanceof float[]) { - mergeParts(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); - } else { - throw new IllegalArgumentException("Unknown array: " + dst.getClass().getName()); - } - propagateCompletion(); - } - - 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(); - } - } - - /** - * Tries to allocate additional buffer. - * - * @param a the given array - * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static Object tryAllocate(Object a, int size) { - try { - if (size > MAX_BUFFER_SIZE) { - return null; - } - if (a instanceof int[]) { - return new int[size]; - } - if (a instanceof long[]) { - return new long[size]; - } - if (a instanceof float[]) { - return new float[size]; - } - if (a instanceof double[]) { - return new double[size]; - } - throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); - } catch (OutOfMemoryError e) { - return null; - } - } -} +/* + * Copyright (c) 2009, 2022, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.util; + +import java.util.concurrent.CountedCompleter; + +/** + * This class implements powerful and fully optimized versions, both + * sequential and parallel, of the Dual-Pivot Quicksort algorithm by + * Vladimir Yaroslavskiy, Jon Bentley and Josh Bloch. This algorithm + * offers O(n log(n)) performance on all data sets, and is typically + * faster than traditional (one-pivot) Quicksort implementations. + * + * There are also additional algorithms, invoked from the Dual-Pivot + * 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 2022.06.14 + * + * @since 1.7 * 14 ^ 20 + */ +final class DualPivotQuicksort { + + /** + * Prevents instantiation. + */ + private DualPivotQuicksort() {} + + /* ---------------- Insertion sort section ---------------- */ + + /** + * Max array size to use mixed insertion sort. + */ + 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; + + /* ----------------- Merging sort section ----------------- */ + + /** + * Min array size to use merging sort. + */ + private static final int MIN_MERGING_SORT_SIZE = 512; + + /** + * Min size of run to continue scanning. + */ + private static final int MIN_RUN_SIZE = 128; + + /* ------------------ Radix sort section ------------------ */ + + /** + * Min array size to use Radix sort. + */ + private static final int MIN_RADIX_SORT_SIZE = 800; + + /* ------------------ Counting sort section --------------- */ + + /** + * Min size of a byte array to use counting sort. + */ + private static final int MIN_BYTE_COUNTING_SORT_SIZE = 36; + + /** + * Min size of a char array to use counting sort. + */ + private static final int MIN_CHAR_COUNTING_SORT_SIZE = 1700; + + /** + * Min size of a short array to use counting sort. + */ + private static final int MIN_SHORT_COUNTING_SORT_SIZE = 2100; + + /* -------------------- Common section -------------------- */ + + /** + * Min array size to perform sorting in parallel. + */ + private static final int MIN_PARALLEL_SORT_SIZE = 1024; + + /** + * Max recursive depth before switching to heap sort. + */ + private static final int MAX_RECURSION_DEPTH = 64 << 1; + + /** + * Max size of additional buffer, + * limited by max_heap / 64 or 2 GB max. + */ + private static final int MAX_BUFFER_SIZE = + (int) Math.min(Runtime.getRuntime().maxMemory() >> 6, Integer.MAX_VALUE); + + /** + * 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. + * + * @param a the array to be sorted + * @param parallelism the parallelism level + * @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(int[] a, int parallelism, int low, int high) { + 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 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 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) { + while (true) { + int size = high - low; + + /* + * Run adaptive mixed insertion sort on small non-leftmost parts. + */ + if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { + mixedInsertionSort(a, low, high); + return; + } + + /* + * Invoke insertion sort on small leftmost part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Try merging sort on large part. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + int a3 = a[e3]; + + boolean isRandom = + 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. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { int t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { int t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { int t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { int t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + /* + * Try Radix sort on large fully random data, + * taking into account parallel context. + */ + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) + && 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; + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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 pivot1 = a[e1]; + int pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + int ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively (possibly in parallel), + * excluding known pivots. + */ + if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { + sorter.fork(bits | 1, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); + } else { + sort(sorter, a, bits | 1, lower + 1, upper); + sort(sorter, a, bits | 1, upper + 1, high); + } + + } 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 pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + 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 + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * 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.fork(bits | 1, upper, high); + } else { + sort(sorter, a, bits | 1, upper, high); + } + } + high = lower; // Iterate along the left part + } + } + + /** + * Sorts the specified range of the array using mixed 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 + * is less than any elements from the given part. Therefore, + * expensive check of the left range can be skipped on each + * iteration unless it is the leftmost call. + * + * @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 mixedInsertionSort(int[] a, int low, int high) { + + /* + * 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 - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + int ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } + + /* + * 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) { + int 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) { + + 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]) { + + 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; + } + } + } + + /** + * 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(int[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + int ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + + /** + * 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, 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 tryMergingSort(Sorter sorter, int[] a, int low, int high) { + + /* + * The element run[i] holds the start index + * of i-th sequence in non-descending order. + */ + int count = 1; + int[] run = null; + + /* + * Identify all possible runs. + */ + for (int k = low + 1, last = low; k < high; ) { + + /* + * Find the next run. + */ + if (a[k - 1] < a[k]) { + + // Identify ascending sequence + while (++k < high && a[k - 1] <= a[k]); + + } else if (a[k - 1] > a[k]) { + + // Identify descending sequence + while (++k < high && a[k - 1] >= a[k]); + + // Reverse into ascending order + for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { + int ai = a[i]; a[i] = a[j]; a[j] = ai; + } + } else { // Identify constant sequence + for (int ak = a[k]; ++k < high && ak == a[k]; ); + + if (k < high) { + continue; + } + } + + /* + * 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) { + /* + * Array is monotonous sequence + * and therefore already sorted. + */ + return true; + } + + 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; + } + } + + /* + * Save the current run. + */ + run[count] = (last = k); + + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; + } + } + + /* + * Merge all runs. + */ + if (count > 1) { + int[] b; int offset = low; + + if (sorter != null && (b = (int[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (int[]) tryAllocate(a, high - low)) == null) { + return false; + } + mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); + } + return true; + } + + /** + * Merges the specified runs. + * + * @param a the source array + * @param b the temporary buffer used in merging + * @param offset the start index in the source, inclusive + * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) + * @param parallel indicates whether merging is performed in parallel + * @param run the start indexes of the runs, inclusive + * @param lo the start index of the first run, inclusive + * @param hi the start index of the last run, inclusive + * @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) { + + if (hi - lo == 1) { + if (aim >= 0) { + return a; + } + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); + return b; + } + + /* + * Split into approximately equal parts. + */ + int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; + while (run[++mi + 1] <= rmi); + + /* + * Merge runs of each part. + */ + 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]; + int lo1 = a1 == b ? run[lo] - offset : run[lo]; + int hi1 = a1 == b ? run[mi] - offset : run[mi]; + int lo2 = a2 == b ? run[mi] - offset : run[mi]; + int hi2 = a2 == b ? run[hi] - offset : run[hi]; + + /* + * 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); + } + return dst; + } + + /** + * Merges the sorted parts. + * + * @param merger parallel context + * @param dst the destination where parts are merged + * @param k the start index of the destination, inclusive + * @param a1 the first part + * @param lo1 the start index of the first part, inclusive + * @param hi1 the end index of the first part, exclusive + * @param a2 the second part + * @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) { + + if (merger != null && a1 == a2) { + + while (true) { + + /* + * The first part must be larger. + */ + if (hi1 - lo1 < hi2 - lo2) { + int lo = lo1; lo1 = lo2; lo2 = lo; + int hi = hi1; hi1 = hi2; hi2 = hi; + } + + /* + * Small parts will be merged sequentially. + */ + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { + break; + } + + /* + * Find the median of the larger part. + */ + int mi1 = (lo1 + hi1) >>> 1; + int key = a1[mi1]; + int mi2 = hi2; + + /* + * Divide the smaller part. + */ + for (int loo = lo2; loo < mi2; ) { + int t = (loo + mi2) >>> 1; + + if (key > a2[t]) { + loo = t + 1; + } else { + mi2 = t; + } + } + + /* + * Reserve space for the left part. + */ + int d = mi2 - lo2 + mi1 - lo1; + + /* + * Merge the right part in parallel. + */ + merger.fork(k + d, mi1, hi1, mi2, hi2); + + /* + * Iterate along the left part. + */ + hi1 = mi1; + hi2 = mi2; + } + } + + /* + * Merge small parts sequentially. + */ + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } + if (dst != a1 || k < lo1) { + while (lo1 < hi1) { + dst[k++] = a1[lo1++]; + } + } + if (dst != a2 || k < lo2) { + while (lo2 < hi2) { + dst[k++] = a2[lo2++]; + } + } + } + + /** + * 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, int[] a, int low, int high) { + int[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = (int[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (int[]) tryAllocate(a, 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 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. + * + * @param a the array to be sorted + * @param parallelism the parallelism level + * @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(long[] a, int parallelism, int low, int high) { + 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 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 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) { + while (true) { + int size = high - low; + + /* + * Run adaptive mixed insertion sort on small non-leftmost parts. + */ + if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { + mixedInsertionSort(a, low, high); + return; + } + + /* + * Invoke insertion sort on small leftmost part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Try merging sort on large part. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + long a3 = a[e3]; + + boolean isRandom = + 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. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { long t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { long t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { long t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { long t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + /* + * Try Radix sort on large fully random data, + * taking into account parallel context. + */ + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) + && 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; + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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. + */ + long pivot1 = a[e1]; + long pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + long ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively (possibly in parallel), + * excluding known pivots. + */ + if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { + sorter.fork(bits | 1, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); + } else { + sort(sorter, a, bits | 1, lower + 1, upper); + sort(sorter, a, bits | 1, upper + 1, high); + } + + } else { // Partitioning with one pivot + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + long pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + 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 + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * 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.fork(bits | 1, upper, high); + } else { + sort(sorter, a, bits | 1, upper, high); + } + } + high = lower; // Iterate along the left part + } + } + + /** + * Sorts the specified range of the array using mixed 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 + * is less than any elements from the given part. Therefore, + * expensive check of the left range can be skipped on each + * iteration unless it is the leftmost call. + * + * @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 mixedInsertionSort(long[] a, int low, int high) { + + /* + * 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 - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + long ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } + + /* + * 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) { + long 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) { + + 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]) { + + 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; + } + } + } + + /** + * 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(long[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + long ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + + /** + * 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, 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 tryMergingSort(Sorter sorter, long[] a, int low, int high) { + + /* + * The element run[i] holds the start index + * of i-th sequence in non-descending order. + */ + int count = 1; + int[] run = null; + + /* + * Identify all possible runs. + */ + for (int k = low + 1, last = low; k < high; ) { + + /* + * Find the next run. + */ + if (a[k - 1] < a[k]) { + + // Identify ascending sequence + while (++k < high && a[k - 1] <= a[k]); + + } else if (a[k - 1] > a[k]) { + + // Identify descending sequence + while (++k < high && a[k - 1] >= a[k]); + + // Reverse into ascending order + for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { + long ai = a[i]; a[i] = a[j]; a[j] = ai; + } + } else { // Identify constant sequence + for (long ak = a[k]; ++k < high && ak == a[k]; ); + + if (k < high) { + continue; + } + } + + /* + * 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) { + /* + * Array is monotonous sequence + * and therefore already sorted. + */ + return true; + } + + 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; + } + } + + /* + * Save the current run. + */ + run[count] = (last = k); + + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; + } + } + + /* + * Merge all runs. + */ + if (count > 1) { + long[] b; int offset = low; + + if (sorter != null && (b = (long[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (long[]) tryAllocate(a, high - low)) == null) { + return false; + } + mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); + } + return true; + } + + /** + * Merges the specified runs. + * + * @param a the source array + * @param b the temporary buffer used in merging + * @param offset the start index in the source, inclusive + * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) + * @param parallel indicates whether merging is performed in parallel + * @param run the start indexes of the runs, inclusive + * @param lo the start index of the first run, inclusive + * @param hi the start index of the last run, inclusive + * @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) { + + if (hi - lo == 1) { + if (aim >= 0) { + return a; + } + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); + return b; + } + + /* + * Split into approximately equal parts. + */ + int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; + while (run[++mi + 1] <= rmi); + + /* + * Merge runs of each part. + */ + 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]; + int lo1 = a1 == b ? run[lo] - offset : run[lo]; + int hi1 = a1 == b ? run[mi] - offset : run[mi]; + int lo2 = a2 == b ? run[mi] - offset : run[mi]; + int hi2 = a2 == b ? run[hi] - offset : run[hi]; + + /* + * 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); + } + return dst; + } + + /** + * Merges the sorted parts. + * + * @param merger parallel context + * @param dst the destination where parts are merged + * @param k the start index of the destination, inclusive + * @param a1 the first part + * @param lo1 the start index of the first part, inclusive + * @param hi1 the end index of the first part, exclusive + * @param a2 the second part + * @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) { + + if (merger != null && a1 == a2) { + + while (true) { + + /* + * The first part must be larger. + */ + if (hi1 - lo1 < hi2 - lo2) { + int lo = lo1; lo1 = lo2; lo2 = lo; + int hi = hi1; hi1 = hi2; hi2 = hi; + } + + /* + * Small parts will be merged sequentially. + */ + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { + break; + } + + /* + * Find the median of the larger part. + */ + int mi1 = (lo1 + hi1) >>> 1; + long key = a1[mi1]; + int mi2 = hi2; + + /* + * Divide the smaller part. + */ + for (int loo = lo2; loo < mi2; ) { + int t = (loo + mi2) >>> 1; + + if (key > a2[t]) { + loo = t + 1; + } else { + mi2 = t; + } + } + + /* + * Reserve space for the left part. + */ + int d = mi2 - lo2 + mi1 - lo1; + + /* + * Merge the right part in parallel. + */ + merger.fork(k + d, mi1, hi1, mi2, hi2); + + /* + * Iterate along the left part. + */ + hi1 = mi1; + hi2 = mi2; + } + } + + /* + * Merge small parts sequentially. + */ + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } + if (dst != a1 || k < lo1) { + while (lo1 < hi1) { + dst[k++] = a1[lo1++]; + } + } + if (dst != a2 || k < lo2) { + while (lo2 < hi2) { + dst[k++] = a2[lo2++]; + } + } + } + + /** + * 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, long[] a, int low, int high) { + long[] b; int offset = low, size = high - low; + + /* + * Allocate additional buffer. + */ + if (sorter != null && (b = (long[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (long[]) tryAllocate(a, 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 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(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 + + if (k > high) { + break; + } + if (k == high || a[k] < a[k - 1]) { + --k; + } + if (a[k] <= value) { + break; + } + } + a[p] = value; + } + +// #[byte] + + /** + * 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 + */ + 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); + } + } + + /** + * The number of distinct byte values. + */ + private static final int NUM_BYTE_VALUES = 1 << 8; + + /** + * 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(byte[] a, int low, int high) { + int[] count = new int[NUM_BYTE_VALUES]; + + /* + * Compute the histogram. + */ + for (int i = high; i > low; ++count[a[--i] & 0xFF]); + + /* + * Put values on their final positions. + */ + for (int i = Byte.MAX_VALUE + 1; high > low; ) { + while (count[--i & 0xFF] == 0); + + int num = count[i & 0xFF]; + + do { + a[--high] = (byte) i; + } while (--num > 0); + } + } + + /** + * 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[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + +// #[char] + + /** + * 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(char[] a, int low, int high) { + if (high - low > MIN_CHAR_COUNTING_SORT_SIZE) { + countingSort(a, low, high); + } else { + sort(a, 0, low, high); + } + } + + /** + * 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 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 size = high - low; + + /* + * Invoke insertion sort on small part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Switch to counting sort, if execution time is quadratic. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + char a3 = a[e3]; + + /* + * Sort these elements in place by the combination + * of 4-element sorting network and insertion sort. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { char t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { char t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { char t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { char t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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. + */ + char pivot1 = a[e1]; + char pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + char ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively, + * excluding known pivots. + */ + sort(a, bits | 1, lower + 1, upper); + sort(a, bits | 1, upper + 1, high); + + } else { // Partitioning with one pivot + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + char pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + for (int k = ++upper; --k > lower; ) { + char ak = a[k]; + + if (ak != pivot) { + a[k] = pivot; + + if (ak < pivot) { // Move a[k] to the left side + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * Sort the right part, excluding known pivot. + * All elements from the central part are + * equal and therefore already sorted. + */ + sort(a, bits | 1, upper, high); + } + high = lower; // Iterate along the left part + } + } + + /** + * 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(char[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + char ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + +// #[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_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. + * + * @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 the histogram. + */ + 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 = Short.MAX_VALUE; i >= Short.MIN_VALUE; --i) { + for (low = high - count[i & 0xFFFF]; high > low; + a[--high] = (short) i + ); + } + } else { + for (int i = Short.MAX_VALUE + 1; high > low; ) { + while (count[--i & 0xFFFF] == 0); + + int num = count[i & 0xFFFF]; + + do { + a[--high] = (short) 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 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 size = high - low; + + /* + * Invoke insertion sort on small part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Switch to counting sort, if execution time is quadratic. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + short a3 = a[e3]; + + /* + * Sort these elements in place by the combination + * of 4-element sorting network and insertion sort. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { short t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { short t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { short t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { short t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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. + */ + short pivot1 = a[e1]; + short pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + short ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively, + * excluding known pivots. + */ + sort(a, bits | 1, lower + 1, upper); + sort(a, bits | 1, upper + 1, high); + + } else { // Partitioning with one pivot + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + short pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + for (int k = ++upper; --k > lower; ) { + short ak = a[k]; + + if (ak != pivot) { + a[k] = pivot; + + if (ak < pivot) { // Move a[k] to the left side + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * Sort the right part, excluding known pivot. + * All elements from the central part are + * equal and therefore already sorted. + */ + sort(a, bits | 1, upper, high); + } + high = lower; // Iterate along the left part + } + } + + /** + * 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(short[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + short ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + +// #[float] + + /** + * 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. + * + * @param a the array to be sorted + * @param parallelism the parallelism level + * @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(float[] a, int parallelism, int low, int high) { + /* + * Phase 1. Count the number of negative zero -0.0f, + * turn them into positive zero, and move all NaNs + * to the end of the array. + */ + int numNegativeZero = 0; + + for (int k = high; k > low; ) { + float ak = a[--k]; + + if (ak == 0.0f && Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f + numNegativeZero += 1; + a[k] = 0.0f; + } else if (ak != ak) { // ak is NaN + a[k] = a[--high]; + a[high] = ak; + } + } + + /* + * Phase 2. Sort everything except NaNs, + * which are already in place. + */ + 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); + } + + /* + * Phase 3. Turn positive zero 0.0f + * back into negative zero -0.0f. + */ + if (++numNegativeZero == 1) { + return; + } + + /* + * Find the position one less than + * the index of the first zero. + */ + while (low <= high) { + int middle = (low + high) >>> 1; + + if (a[middle] < 0) { + low = middle + 1; + } else { + high = middle - 1; + } + } + + /* + * Replace the required number of 0.0f by -0.0f. + */ + while (--numNegativeZero > 0) { + a[++high] = -0.0f; + } + } + + /** + * 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 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) { + while (true) { + int size = high - low; + + /* + * Run adaptive mixed insertion sort on small non-leftmost parts. + */ + if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { + mixedInsertionSort(a, low, high); + return; + } + + /* + * Invoke insertion sort on small leftmost part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Try merging sort on large part. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + float a3 = a[e3]; + + boolean isRandom = + 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. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { float t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { float t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { float t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { float t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + /* + * Try Radix sort on large fully random data, + * taking into account parallel context. + */ + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) + && 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; + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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. + */ + float pivot1 = a[e1]; + float pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + float ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively (possibly in parallel), + * excluding known pivots. + */ + if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { + sorter.fork(bits | 1, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); + } else { + sort(sorter, a, bits | 1, lower + 1, upper); + sort(sorter, a, bits | 1, upper + 1, high); + } + + } else { // Partitioning with one pivot + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + float pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + 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 + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * 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.fork(bits | 1, upper, high); + } else { + sort(sorter, a, bits | 1, upper, high); + } + } + high = lower; // Iterate along the left part + } + } + + /** + * Sorts the specified range of the array using mixed 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 + * is less than any elements from the given part. Therefore, + * expensive check of the left range can be skipped on each + * iteration unless it is the leftmost call. + * + * @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 mixedInsertionSort(float[] a, int low, int high) { + + /* + * 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 - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + float ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } + + /* + * 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) { + float 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) { + + 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]) { + + 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; + } + } + } + + /** + * 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(float[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + float ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + + /** + * 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, 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 tryMergingSort(Sorter sorter, float[] a, int low, int high) { + + /* + * The element run[i] holds the start index + * of i-th sequence in non-descending order. + */ + int count = 1; + int[] run = null; + + /* + * Identify all possible runs. + */ + for (int k = low + 1, last = low; k < high; ) { + + /* + * Find the next run. + */ + if (a[k - 1] < a[k]) { + + // Identify ascending sequence + while (++k < high && a[k - 1] <= a[k]); + + } else if (a[k - 1] > a[k]) { + + // Identify descending sequence + while (++k < high && a[k - 1] >= a[k]); + + // Reverse into ascending order + for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { + float ai = a[i]; a[i] = a[j]; a[j] = ai; + } + } else { // Identify constant sequence + for (float ak = a[k]; ++k < high && ak == a[k]; ); + + if (k < high) { + continue; + } + } + + /* + * 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) { + /* + * Array is monotonous sequence + * and therefore already sorted. + */ + return true; + } + + 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; + } + } + + /* + * Save the current run. + */ + run[count] = (last = k); + + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; + } + } + + /* + * Merge all runs. + */ + if (count > 1) { + float[] b; int offset = low; + + if (sorter != null && (b = (float[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (float[]) tryAllocate(a, high - low)) == null) { + return false; + } + mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); + } + return true; + } + + /** + * Merges the specified runs. + * + * @param a the source array + * @param b the temporary buffer used in merging + * @param offset the start index in the source, inclusive + * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) + * @param parallel indicates whether merging is performed in parallel + * @param run the start indexes of the runs, inclusive + * @param lo the start index of the first run, inclusive + * @param hi the start index of the last run, inclusive + * @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) { + + if (hi - lo == 1) { + if (aim >= 0) { + return a; + } + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); + return b; + } + + /* + * Split into approximately equal parts. + */ + int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; + while (run[++mi + 1] <= rmi); + + /* + * Merge runs of each part. + */ + 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]; + int lo1 = a1 == b ? run[lo] - offset : run[lo]; + int hi1 = a1 == b ? run[mi] - offset : run[mi]; + int lo2 = a2 == b ? run[mi] - offset : run[mi]; + int hi2 = a2 == b ? run[hi] - offset : run[hi]; + + /* + * 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); + } + return dst; + } + + /** + * Merges the sorted parts. + * + * @param merger parallel context + * @param dst the destination where parts are merged + * @param k the start index of the destination, inclusive + * @param a1 the first part + * @param lo1 the start index of the first part, inclusive + * @param hi1 the end index of the first part, exclusive + * @param a2 the second part + * @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) { + + if (merger != null && a1 == a2) { + + while (true) { + + /* + * The first part must be larger. + */ + if (hi1 - lo1 < hi2 - lo2) { + int lo = lo1; lo1 = lo2; lo2 = lo; + int hi = hi1; hi1 = hi2; hi2 = hi; + } + + /* + * Small parts will be merged sequentially. + */ + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { + break; + } + + /* + * Find the median of the larger part. + */ + int mi1 = (lo1 + hi1) >>> 1; + float key = a1[mi1]; + int mi2 = hi2; + + /* + * Divide the smaller part. + */ + for (int loo = lo2; loo < mi2; ) { + int t = (loo + mi2) >>> 1; + + if (key > a2[t]) { + loo = t + 1; + } else { + mi2 = t; + } + } + + /* + * Reserve space for the left part. + */ + int d = mi2 - lo2 + mi1 - lo1; + + /* + * Merge the right part in parallel. + */ + merger.fork(k + d, mi1, hi1, mi2, hi2); + + /* + * Iterate along the left part. + */ + hi1 = mi1; + hi2 = mi2; + } + } + + /* + * Merge small parts sequentially. + */ + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } + if (dst != a1 || k < lo1) { + while (lo1 < hi1) { + dst[k++] = a1[lo1++]; + } + } + if (dst != a2 || k < lo2) { + while (lo2 < hi2) { + dst[k++] = a2[lo2++]; + } + } + } + + /** + * 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 = (float[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (float[]) tryAllocate(a, 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 + * 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. + * + * @param a the array to be sorted + * @param parallelism the parallelism level + * @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(double[] a, int parallelism, int low, int high) { + /* + * Phase 1. Count the number of negative zero -0.0d, + * turn them into positive zero, and move all NaNs + * to the end of the array. + */ + int numNegativeZero = 0; + + for (int k = high; k > low; ) { + double ak = a[--k]; + + if (ak == 0.0d && Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d + numNegativeZero += 1; + a[k] = 0.0d; + } else if (ak != ak) { // ak is NaN + a[k] = a[--high]; + a[high] = ak; + } + } + + /* + * Phase 2. Sort everything except NaNs, + * which are already in place. + */ + 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); + } + + /* + * Phase 3. Turn positive zero 0.0d + * back into negative zero -0.0d. + */ + if (++numNegativeZero == 1) { + return; + } + + /* + * Find the position one less than + * the index of the first zero. + */ + while (low <= high) { + int middle = (low + high) >>> 1; + + if (a[middle] < 0) { + low = middle + 1; + } else { + high = middle - 1; + } + } + + /* + * Replace the required number of 0.0d by -0.0d. + */ + while (--numNegativeZero > 0) { + a[++high] = -0.0d; + } + } + + /** + * 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 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) { + while (true) { + int size = high - low; + + /* + * Run adaptive mixed insertion sort on small non-leftmost parts. + */ + if (size < MAX_MIXED_INSERTION_SORT_SIZE + bits && (bits & 1) > 0) { + mixedInsertionSort(a, low, high); + return; + } + + /* + * Invoke insertion sort on small leftmost part. + */ + if (size < MAX_INSERTION_SORT_SIZE) { + insertionSort(a, low, high); + return; + } + + /* + * Try merging sort on large part. + */ + 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. + */ + int step = (size >> 2) + (size >> 3) + (size >> 8) + 1; + + /* + * Five elements around (and including) the central element + * will be used for pivot selection as described below. The + * 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; + int e2 = (e1 + e3) >>> 1; + int e4 = (e3 + e5) >>> 1; + double a3 = a[e3]; + + boolean isRandom = + 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. + * + * 1 ------------o-----o------------ + * | | + * 2 ------o-----|-----o-----o------ + * | | | + * 4 ------|-----o-----o-----o------ + * | | + * 5 ------o-----------o------------ + */ + if (a[e2] > a[e5]) { double t = a[e2]; a[e2] = a[e5]; a[e5] = t; } + if (a[e1] > a[e4]) { double t = a[e1]; a[e1] = a[e4]; a[e4] = t; } + if (a[e1] > a[e2]) { double t = a[e1]; a[e1] = a[e2]; a[e2] = t; } + if (a[e4] > a[e5]) { double t = a[e4]; a[e4] = a[e5]; a[e5] = 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; + } else { + a[e3] = a[e2]; a[e2] = a3; + } + } else if (a3 > a[e4]) { + if (a3 > a[e5]) { + a[e3] = a[e4]; a[e4] = a[e5]; a[e5] = a3; + } else { + a[e3] = a[e4]; a[e4] = a3; + } + } + + /* + * Try Radix sort on large fully random data, + * taking into account parallel context. + */ + isRandom &= a[e1] < a[e2] && a[e2] < a[e3] && a[e3] < a[e4] && a[e4] < a[e5]; + + if (size > MIN_RADIX_SORT_SIZE && isRandom && (sorter == null || bits > 0) + && 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; + } + + // Pointers + int lower = low; // The index of the last element of the left part + int upper = end; // The index of the first element of the right part + + /* + * 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. + */ + double pivot1 = a[e1]; + double pivot2 = a[e5]; + + /* + * 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]; + + /* + * 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 + * + * 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 + * + * Invariants: + * + * all in (low, lower] < pivot1 + * all in (k, upper) in [pivot1, pivot2] + * all in [upper, end) > pivot2 + */ + for (int unused = --lower, k = ++upper; --k > lower; ) { + double ak = a[k]; + + if (ak < pivot1) { // Move a[k] to the left side + while (a[++lower] < pivot1) { + if (lower == k) { + break; + } + } + 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 side + a[k] = a[--upper]; + a[upper] = ak; + } + } + + /* + * Swap the pivots into their final positions. + */ + a[low] = a[lower]; a[lower] = pivot1; + a[end] = a[upper]; a[upper] = pivot2; + + /* + * Sort non-left parts recursively (possibly in parallel), + * excluding known pivots. + */ + if (size > MIN_PARALLEL_SORT_SIZE && sorter != null) { + sorter.fork(bits | 1, lower + 1, upper); + sorter.fork(bits | 1, upper + 1, high); + } else { + sort(sorter, a, bits | 1, lower + 1, upper); + sort(sorter, a, bits | 1, upper + 1, high); + } + + } else { // Partitioning with one pivot + + /* + * Use the third of the five sorted elements as the pivot. + * This value is inexpensive approximation of the median. + */ + double pivot = a[e3]; + + /* + * 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]; + + /* + * Dutch National Flag partitioning + * + * left part central part right part + * +------------------------------------------------------+ + * | < pivot | ? | == pivot | > 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 + * + * Invariants: + * + * all in (low, lower] < pivot + * all in (k, upper) == pivot + * all in [upper, end] > pivot + */ + 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 + 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 + a[--upper] = ak; + } + } + } + + /* + * Swap the pivot into its final position. + */ + a[low] = a[lower]; a[lower] = pivot; + + /* + * 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.fork(bits | 1, upper, high); + } else { + sort(sorter, a, bits | 1, upper, high); + } + } + high = lower; // Iterate along the left part + } + } + + /** + * Sorts the specified range of the array using mixed 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 + * is less than any elements from the given part. Therefore, + * expensive check of the left range can be skipped on each + * iteration unless it is the leftmost call. + * + * @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 mixedInsertionSort(double[] a, int low, int high) { + + /* + * 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 - 1]) { + a[i] = a[--i]; + } + a[i] = ai; + } + return; + } + + /* + * Start with pin insertion sort. + */ + for (int i, p = high; ++low < end; ) { + double ai = a[i = low], pin = a[--p]; + + /* + * Swap larger element with pin. + */ + if (ai > pin) { + ai = pin; + a[p] = a[i]; + } + + /* + * 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]; + + /* + * 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 (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]) { + + 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; + } + } + } + + /** + * 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(double[] a, int low, int high) { + for (int i, k = low; ++k < high; ) { + double ai = a[i = k]; + + if (ai < a[i - 1]) { + do { + a[i] = a[--i]; + } while (i > low && ai < a[i - 1]); + + a[i ] = ai; + } + } + } + + /** + * 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, 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 tryMergingSort(Sorter sorter, double[] a, int low, int high) { + + /* + * The element run[i] holds the start index + * of i-th sequence in non-descending order. + */ + int count = 1; + int[] run = null; + + /* + * Identify all possible runs. + */ + for (int k = low + 1, last = low; k < high; ) { + + /* + * Find the next run. + */ + if (a[k - 1] < a[k]) { + + // Identify ascending sequence + while (++k < high && a[k - 1] <= a[k]); + + } else if (a[k - 1] > a[k]) { + + // Identify descending sequence + while (++k < high && a[k - 1] >= a[k]); + + // Reverse into ascending order + for (int i = last - 1, j = k; ++i < --j && a[i] > a[j]; ) { + double ai = a[i]; a[i] = a[j]; a[j] = ai; + } + } else { // Identify constant sequence + for (double ak = a[k]; ++k < high && ak == a[k]; ); + + if (k < high) { + continue; + } + } + + /* + * 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) { + /* + * Array is monotonous sequence + * and therefore already sorted. + */ + return true; + } + + 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; + } + } + + /* + * Save the current run. + */ + run[count] = (last = k); + + /* + * Check single-element run at the end. + */ + if (++k == high) { + --k; + } + } + + /* + * Merge all runs. + */ + if (count > 1) { + double[] b; int offset = low; + + if (sorter != null && (b = (double[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (double[]) tryAllocate(a, high - low)) == null) { + return false; + } + mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); + } + return true; + } + + /** + * Merges the specified runs. + * + * @param a the source array + * @param b the temporary buffer used in merging + * @param offset the start index in the source, inclusive + * @param aim specifies merging: to source ( > 0), buffer ( < 0) or any ( == 0) + * @param parallel indicates whether merging is performed in parallel + * @param run the start indexes of the runs, inclusive + * @param lo the start index of the first run, inclusive + * @param hi the start index of the last run, inclusive + * @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) { + + if (hi - lo == 1) { + if (aim >= 0) { + return a; + } + System.arraycopy(a, run[lo], b, run[lo] - offset, run[hi] - run[lo]); + return b; + } + + /* + * Split into approximately equal parts. + */ + int mi = lo, rmi = (run[lo] + run[hi]) >>> 1; + while (run[++mi + 1] <= rmi); + + /* + * Merge runs of each part. + */ + 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]; + int lo1 = a1 == b ? run[lo] - offset : run[lo]; + int hi1 = a1 == b ? run[mi] - offset : run[mi]; + int lo2 = a2 == b ? run[mi] - offset : run[mi]; + int hi2 = a2 == b ? run[hi] - offset : run[hi]; + + /* + * 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); + } + return dst; + } + + /** + * Merges the sorted parts. + * + * @param merger parallel context + * @param dst the destination where parts are merged + * @param k the start index of the destination, inclusive + * @param a1 the first part + * @param lo1 the start index of the first part, inclusive + * @param hi1 the end index of the first part, exclusive + * @param a2 the second part + * @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) { + + if (merger != null && a1 == a2) { + + while (true) { + + /* + * The first part must be larger. + */ + if (hi1 - lo1 < hi2 - lo2) { + int lo = lo1; lo1 = lo2; lo2 = lo; + int hi = hi1; hi1 = hi2; hi2 = hi; + } + + /* + * Small parts will be merged sequentially. + */ + if (hi1 - lo1 < MIN_PARALLEL_SORT_SIZE) { + break; + } + + /* + * Find the median of the larger part. + */ + int mi1 = (lo1 + hi1) >>> 1; + double key = a1[mi1]; + int mi2 = hi2; + + /* + * Divide the smaller part. + */ + for (int loo = lo2; loo < mi2; ) { + int t = (loo + mi2) >>> 1; + + if (key > a2[t]) { + loo = t + 1; + } else { + mi2 = t; + } + } + + /* + * Reserve space for the left part. + */ + int d = mi2 - lo2 + mi1 - lo1; + + /* + * Merge the right part in parallel. + */ + merger.fork(k + d, mi1, hi1, mi2, hi2); + + /* + * Iterate along the left part. + */ + hi1 = mi1; + hi2 = mi2; + } + } + + /* + * Merge small parts sequentially. + */ + while (lo1 < hi1 && lo2 < hi2) { + dst[k++] = a1[lo1] < a2[lo2] ? a1[lo1++] : a2[lo2++]; + } + if (dst != a1 || k < lo1) { + while (lo1 < hi1) { + dst[k++] = a1[lo1++]; + } + } + if (dst != a2 || k < lo2) { + while (lo2 < hi2) { + dst[k++] = a2[lo2++]; + } + } + } + + /** + * 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 = (double[]) sorter.b) != null) { + offset = sorter.offset; + } else if ((b = (double[]) tryAllocate(a, 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 = 123456789L; + + @SuppressWarnings("serial") + private final Object a, b; + private final int low, size, offset, depth; + + private Sorter(Object 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 = tryAllocate(a, this.size); + this.depth = b == null ? 0 : depth; + } + + private Sorter(CountedCompleter parent, + Object a, Object b, int low, int size, int offset, int depth) { + super(parent); + this.a = a; + this.b = b; + this.low = low; + this.size = size; + this.offset = offset; + this.depth = depth; + } + + @Override + 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(); + } else { + if (a instanceof int[]) { + sort(this, (int[]) a, depth, low, low + size); + } else if (a instanceof long[]) { + sort(this, (long[]) a, depth, low, low + size); + } else if (a instanceof float[]) { + sort(this, (float[]) a, depth, low, low + size); + } else if (a instanceof double[]) { + sort(this, (double[]) a, depth, low, low + size); + } else { + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); + } + } + tryComplete(); + } + + @Override + public void onCompletion(CountedCompleter parent) { + 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 + ).invoke(); + } + } + + private void fork(int depth, int low, int high) { + addToPendingCount(1); + 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 = 123456789L; + + @SuppressWarnings("serial") + private final Object 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) { + super(parent); + this.dst = dst; + this.k = k; + this.a1 = a1; + this.lo1 = lo1; + this.hi1 = hi1; + this.a2 = a2; + this.lo2 = lo2; + this.hi2 = hi2; + } + + @Override + public void compute() { + if (dst instanceof int[]) { + mergeParts(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); + } else if (dst instanceof float[]) { + mergeParts(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); + } else { + throw new IllegalArgumentException("Unknown array: " + dst.getClass().getName()); + } + propagateCompletion(); + } + + 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(); + } + } + + /** + * Tries to allocate additional buffer. + * + * @param a the given array + * @param size the size of additional buffer + * @return {@code null} if requested size is too large, otherwise created buffer + */ + private static Object tryAllocate(Object a, int size) { + try { + if (size > MAX_BUFFER_SIZE) { + return null; + } + if (a instanceof int[]) { + return new int[size]; + } + if (a instanceof long[]) { + return new long[size]; + } + if (a instanceof float[]) { + return new float[size]; + } + if (a instanceof double[]) { + return new double[size]; + } + throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); + } catch (OutOfMemoryError e) { + return null; + } + } +} diff --git a/test/jdk/java/util/Arrays/Sorting.java b/test/jdk/java/util/Arrays/Sorting.java index 58ff871cf8743..4706ee7e24545 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -1,1798 +1,1798 @@ -/* - * Copyright (c) 2009, 2022, 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 - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @compile/module=java.base java/util/SortingHelper.java - * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297 8266431 - * @build Sorting - * @run main Sorting -shortrun - * @summary Exercise Arrays.sort, Arrays.parallelSort - * - * @author Vladimir Yaroslavskiy - * @author Jon Bentley - * @author Josh Bloch - */ - -import java.io.PrintStream; -import java.util.Arrays; -import java.util.Random; -import java.util.SortingHelper; - -public class Sorting { - - private static final PrintStream out = System.out; - private static final PrintStream err = System.err; - - // Lengths of arrays for short run - private static final int[] SHORT_RUN_LENGTHS = - { 1, 2, 14, 100, 500, 1_000, 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 }; - - // Initial random values for 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 }; - - // 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; - - public static void main(String[] args) { - long start = System.currentTimeMillis(); - boolean shortRun = args.length > 0 && args[0].equals("-shortrun"); - - 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.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) / 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 testBase() { - testStructured(0); - testEmptyArray(); - - for (int length : lengths) { - createData(length); - testSubArray(length); - - for (TestRandom random : randoms) { - testWithCheckSum(length, random); - testWithScrambling(length, random); - testWithInsertionSort(length, random); - } - } - } - - private void testCore() { - testBase(); - - for (int length : lengths) { - createData(length); - - for (TestRandom random : randoms) { - testNegativeZero(length, random); - testFloatingPointSorting(length, random); - } - } - } - - private void testAll() { - testCore(); - - for (int length : lengths) { - createData(length); - testRange(length); - } - } - - private void testStructured(int min) { - for (int length : lengths) { - createData(length); - testStructured(length, min); - } - } - - private void testEmptyArray() { - sortingHelper.sort(new int[] {}); - sortingHelper.sort(new int[] {}, 0, 0); - - sortingHelper.sort(new long[] {}); - sortingHelper.sort(new long[] {}, 0, 0); - - sortingHelper.sort(new byte[] {}); - sortingHelper.sort(new byte[] {}, 0, 0); - - sortingHelper.sort(new char[] {}); - sortingHelper.sort(new char[] {}, 0, 0); - - sortingHelper.sort(new short[] {}); - sortingHelper.sort(new short[] {}, 0, 0); - - sortingHelper.sort(new float[] {}); - sortingHelper.sort(new float[] {}, 0, 0); - - sortingHelper.sort(new double[] {}); - sortingHelper.sort(new double[] {}, 0, 0); - } - - private void testSubArray(int length) { - if (fix || length < 4) { - return; - } - for (int m = 1; m < length / 2; m <<= 1) { - int toIndex = length - m; - - prepareSubArray((int[]) gold[0], m, toIndex); - convertData(length); - - 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) { - for (int m = 1; m < length; m <<= 1) { - 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", length, - ", m = " + m + ", " + getType(i)); - checkRange(test[i], m); - } - } - out.println(); - } - - private void testWithInsertionSort(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) { - printTestName("Test with insertion sort", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); - sortingHelper.sort(test[i]); - sortByInsertionSort(gold[i]); - checkSorted(gold[i]); - compare(test[i], gold[i]); - } - } - } - out.println(); - } - - private void testStructured(int length, int min) { - if (length < min) { - return; - } - 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 structured", length, - ", m = " + m + ", " + getType(i) + " " + builder); - sortingHelper.sort(test[i]); - checkSorted(test[i]); - } - } - } - out.println(); - } - - 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) { - printTestName("Test with check sum", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); - sortingHelper.sort(test[i]); - checkWithCheckSum(test[i], gold[i]); - } - } - } - out.println(); - } - - 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) { - printTestName("Test with scrambling", random, length, - ", m = " + m + ", " + getType(i) + " " + builder); - scramble(test[i], random); - sortingHelper.sort(test[i]); - compare(test[i], gold[i]); - } - } - } - out.println(); - } - - private void testNegativeZero(int length, TestRandom random) { - for (int i = 5; i < test.length; ++i) { - printTestName("Test negative zero -0.0", random, length, " " + getType(i)); - - NegativeZeroBuilder builder = NegativeZeroBuilder.values()[i - 5]; - builder.build(test[i], random); - - sortingHelper.sort(test[i]); - checkNegativeZero(test[i]); - } - out.println(); - } - - private void testFloatingPointSorting(int length, TestRandom random) { - if (length < 6) { - return; - } - 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 + s != length) { - continue; - } - 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); - copy(test[i], gold[i]); - scramble(test[i], random); - sortingHelper.sort(test[i]); - 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 i = 5; i < test.length; ++i) { - printTestName("Test float-pointing sorting", random, length, - ", 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, g + 2, g); - } - } - out.println(); - } - - private void prepareSubArray(int[] a, int fromIndex, int toIndex) { - 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) { - a[i] = k++; - } - - for (int i = middle; i < toIndex; ++i) { - a[i] = k--; - } - - for (int i = toIndex; i < a.length; ++i) { - a[i] = B747; - } - } - - private void scramble(Object a, Random random) { - if (a instanceof int[]) { - scramble((int[]) a, random); - } else if (a instanceof long[]) { - scramble((long[]) a, random); - } else if (a instanceof byte[]) { - scramble((byte[]) a, random); - } else if (a instanceof char[]) { - scramble((char[]) a, random); - } else if (a instanceof short[]) { - scramble((short[]) a, random); - } else if (a instanceof float[]) { - scramble((float[]) a, random); - } else if (a instanceof double[]) { - scramble((double[]) a, random); - } else { - fail(a); - } - } - - private void scramble(int[] a, Random random) { - 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) { - 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) { - 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) { - 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) { - 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) { - 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) { - swap(a, random.nextInt(a.length), random.nextInt(a.length)); - } - } - - private void swap(int[] a, int i, int j) { - int t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(long[] a, int i, int j) { - long t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(byte[] a, int i, int j) { - byte t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(char[] a, int i, int j) { - char t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(short[] a, int i, int j) { - short t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(float[] a, int i, int j) { - float t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void swap(double[] a, int i, int j) { - double t = a[i]; a[i] = a[j]; a[j] = t; - } - - private void checkWithCheckSum(Object test, Object gold) { - checkSorted(test); - 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"); - } - - private void checkNegativeZero(Object a) { - if (a instanceof float[]) { - checkNegativeZero((float[]) a); - } else if (a instanceof double[]) { - checkNegativeZero((double[]) a); - } else { - fail(a); - } - } - - private void checkNegativeZero(float[] a) { - 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); - } - } - } - - private void checkNegativeZero(double[] a) { - 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); - } - } - } - - private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) { - if (a instanceof float[]) { - compare((float[]) a, (float[]) b, numNaN, numNeg, numNegZero); - } else if (a instanceof double[]) { - compare((double[]) a, (double[]) b, numNaN, numNeg, numNegZero); - } else { - 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) { - 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) { - 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) { - if (a[i] != b[i]) { - fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); - } - } - } - - private void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) { - 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) { - 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) { - if (a[i] != b[i]) { - fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); - } - } - } - - private void compare(Object a, Object b) { - if (a instanceof int[]) { - compare((int[]) a, (int[]) b); - } else if (a instanceof long[]) { - compare((long[]) a, (long[]) b); - } else if (a instanceof byte[]) { - compare((byte[]) a, (byte[]) b); - } else if (a instanceof char[]) { - compare((char[]) a, (char[]) b); - } else if (a instanceof short[]) { - compare((short[]) a, (short[]) b); - } else if (a instanceof float[]) { - compare((float[]) a, (float[]) b); - } else if (a instanceof double[]) { - compare((double[]) a, (double[]) b); - } else { - fail(a); - } - } - - private void compare(int[] a, int[] b) { - 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); - } - } - } - - private void compare(long[] a, long[] b) { - 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); - } - } - } - - private void compare(byte[] a, byte[] b) { - 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); - } - } - } - - private void compare(char[] a, char[] b) { - 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); - } - } - } - - private void compare(short[] a, short[] b) { - 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); - } - } - } - - private void compare(float[] a, float[] b) { - 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); - } - } - } - - private void compare(double[] a, double[] b) { - 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); - } - } - } - - private String getType(int i) { - Object a = test[i]; - - if (a instanceof int[]) { - return "INT "; - } - if (a instanceof long[]) { - return "LONG "; - } - if (a instanceof byte[]) { - return "BYTE "; - } - if (a instanceof char[]) { - return "CHAR "; - } - if (a instanceof short[]) { - return "SHORT "; - } - if (a instanceof float[]) { - return "FLOAT "; - } - if (a instanceof double[]) { - return "DOUBLE"; - } - fail(a); - return null; - } - - private void checkSorted(Object a) { - if (a instanceof int[]) { - checkSorted((int[]) a); - } else if (a instanceof long[]) { - checkSorted((long[]) a); - } else if (a instanceof byte[]) { - checkSorted((byte[]) a); - } else if (a instanceof char[]) { - checkSorted((char[]) a); - } else if (a instanceof short[]) { - checkSorted((short[]) a); - } else if (a instanceof float[]) { - checkSorted((float[]) a); - } else if (a instanceof double[]) { - checkSorted((double[]) a); - } else { - fail(a); - } - } - - private void checkSorted(int[] a) { - 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]); - } - } - } - - private void checkSorted(long[] a) { - 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]); - } - } - } - - private void checkSorted(byte[] a) { - 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]); - } - } - } - - private void checkSorted(char[] a) { - 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]); - } - } - } - - private void checkSorted(short[] a) { - 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]); - } - } - } - - private void checkSorted(float[] a) { - 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]); - } - } - } - - private void checkSorted(double[] a) { - 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]); - } - } - } - - private void checkCheckSum(Object test, Object gold) { - if (checkSumXor(test) != checkSumXor(gold)) { - fail("Original and sorted arrays are not identical [^]"); - } - if (checkSumPlus(test) != checkSumPlus(gold)) { - fail("Original and sorted arrays are not identical [+]"); - } - } - - private int checkSumXor(Object a) { - if (a instanceof int[]) { - return checkSumXor((int[]) a); - } - if (a instanceof long[]) { - return checkSumXor((long[]) a); - } - if (a instanceof byte[]) { - return checkSumXor((byte[]) a); - } - if (a instanceof char[]) { - return checkSumXor((char[]) a); - } - if (a instanceof short[]) { - return checkSumXor((short[]) a); - } - if (a instanceof float[]) { - return checkSumXor((float[]) a); - } - if (a instanceof double[]) { - return checkSumXor((double[]) a); - } - fail(a); - return -1; - } - - private int checkSumXor(int[] a) { - int checkSum = 0; - - for (int e : a) { - checkSum ^= e; - } - return checkSum; - } - - private int checkSumXor(long[] a) { - long checkSum = 0; - - for (long e : a) { - checkSum ^= e; - } - return (int) checkSum; - } - - private int checkSumXor(byte[] a) { - byte checkSum = 0; - - for (byte e : a) { - checkSum ^= e; - } - return checkSum; - } - - private int checkSumXor(char[] a) { - char checkSum = 0; - - for (char e : a) { - checkSum ^= e; - } - return checkSum; - } - - private int checkSumXor(short[] a) { - short checkSum = 0; - - for (short e : a) { - checkSum ^= e; - } - return checkSum; - } - - private int checkSumXor(float[] a) { - int checkSum = 0; - - for (float e : a) { - checkSum ^= (int) e; - } - return checkSum; - } - - private int checkSumXor(double[] a) { - int checkSum = 0; - - for (double e : a) { - checkSum ^= (int) e; - } - return checkSum; - } - - private int checkSumPlus(Object a) { - if (a instanceof int[]) { - return checkSumPlus((int[]) a); - } - if (a instanceof long[]) { - return checkSumPlus((long[]) a); - } - if (a instanceof byte[]) { - return checkSumPlus((byte[]) a); - } - if (a instanceof char[]) { - return checkSumPlus((char[]) a); - } - if (a instanceof short[]) { - return checkSumPlus((short[]) a); - } - if (a instanceof float[]) { - return checkSumPlus((float[]) a); - } - if (a instanceof double[]) { - return checkSumPlus((double[]) a); - } - fail(a); - return -1; - } - - private int checkSumPlus(int[] a) { - int checkSum = 0; - - for (int e : a) { - checkSum += e; - } - return checkSum; - } - - private int checkSumPlus(long[] a) { - long checkSum = 0; - - for (long e : a) { - checkSum += e; - } - return (int) checkSum; - } - - private int checkSumPlus(byte[] a) { - byte checkSum = 0; - - for (byte e : a) { - checkSum += e; - } - return checkSum; - } - - private int checkSumPlus(char[] a) { - char checkSum = 0; - - for (char e : a) { - checkSum += e; - } - return checkSum; - } - - private int checkSumPlus(short[] a) { - short checkSum = 0; - - for (short e : a) { - checkSum += e; - } - return checkSum; - } - - private int checkSumPlus(float[] a) { - int checkSum = 0; - - for (float e : a) { - checkSum += (int) e; - } - return checkSum; - } - - private int checkSumPlus(double[] a) { - int checkSum = 0; - - for (double e : a) { - checkSum += (int) e; - } - return checkSum; - } - - private void sortByInsertionSort(Object a) { - SortingHelper.INSERTION_SORT.sort(a); - } - - private void checkSubArray(Object a, int fromIndex, int toIndex) { - if (a instanceof int[]) { - checkSubArray((int[]) a, fromIndex, toIndex); - } else if (a instanceof long[]) { - checkSubArray((long[]) a, fromIndex, toIndex); - } else if (a instanceof byte[]) { - checkSubArray((byte[]) a, fromIndex, toIndex); - } else if (a instanceof char[]) { - checkSubArray((char[]) a, fromIndex, toIndex); - } else if (a instanceof short[]) { - checkSubArray((short[]) a, fromIndex, toIndex); - } else if (a instanceof float[]) { - checkSubArray((float[]) a, fromIndex, toIndex); - } else if (a instanceof double[]) { - checkSubArray((double[]) a, fromIndex, toIndex); - } else { - fail(a); - } - } - - private void checkSubArray(int[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != B747) { - fail("Range sort changes right element at position " + i + hex(a[i], B747)); - } - } - } - - private void checkSubArray(long[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (long) B747) { - fail("Range sort changes right element at position " + i + hex(a[i], B747)); - } - } - } - - private void checkSubArray(byte[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (byte) B747) { - fail("Range sort changes right element at position " + i + hex(a[i], B747)); - } - } - } - - private void checkSubArray(char[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (char) B747) { - fail("Range sort changes right element at position " + i + hex(a[i], B747)); - } - } - } - - private void checkSubArray(short[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (short) B747) { - fail("Range sort changes right element at position " + i + hex(a[i], B747)); - } - } - } - - private void checkSubArray(float[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (float) B747) { - fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); - } - } - } - - private void checkSubArray(double[] a, int fromIndex, int toIndex) { - 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) { - 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) { - if (a[i] != (double) B747) { - fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); - } - } - } - - private void checkRange(Object a, int m) { - if (a instanceof int[]) { - checkRange((int[]) a, m); - } else if (a instanceof long[]) { - checkRange((long[]) a, m); - } else if (a instanceof byte[]) { - checkRange((byte[]) a, m); - } else if (a instanceof char[]) { - checkRange((char[]) a, m); - } else if (a instanceof short[]) { - checkRange((short[]) a, m); - } else if (a instanceof float[]) { - checkRange((float[]) a, m); - } else if (a instanceof double[]) { - checkRange((double[]) a, m); - } else { - fail(a); - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - 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); - } catch (IllegalArgumentException iae) { - try { - sortingHelper.sort(a, -m, a.length); - fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + - "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)); - } catch (ArrayIndexOutOfBoundsException expected) {} - } - } - } - - private void copy(Object dst, Object src) { - if (src instanceof float[]) { - copy((float[]) dst, (float[]) src); - } else if (src instanceof double[]) { - copy((double[]) dst, (double[]) src); - } else { - fail(src); - } - } - - private void copy(float[] dst, float[] src) { - System.arraycopy(src, 0, dst, 0, src.length); - } - - private void copy(double[] dst, double[] src) { - System.arraycopy(src, 0, dst, 0, src.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] - }; - - test = new Object[] { - 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 = 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) { - System.arraycopy(gold[i], 0, test[i], 0, length); - } - } - - 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); - } - - private enum TypeConverter { - - INT { - @Override - void convert(int[] src, Object dst, boolean fix) { - if (fix) { - src[0] = Integer.MIN_VALUE; - } - } - }, - - LONG { - @Override - void convert(int[] src, Object dst, boolean fix) { - long[] b = (long[]) dst; - - for (int i = 0; i < src.length; ++i) { - b[i] = src[i]; - } - if (fix) { - b[0] = Long.MIN_VALUE; - } - } - }, - - BYTE { - @Override - void convert(int[] src, Object dst, boolean fix) { - byte[] b = (byte[]) dst; - - for (int i = 0; i < src.length; ++i) { - b[i] = (byte) src[i]; - } - if (fix) { - b[0] = Byte.MIN_VALUE; - } - } - }, - - CHAR { - @Override - void convert(int[] src, Object dst, boolean fix) { - char[] b = (char[]) dst; - - for (int i = 0; i < src.length; ++i) { - b[i] = (char) src[i]; - } - if (fix) { - b[0] = Character.MIN_VALUE; - } - } - }, - - SHORT { - @Override - void convert(int[] src, Object dst, boolean fix) { - short[] b = (short[]) dst; - - for (int i = 0; i < src.length; ++i) { - b[i] = (short) src[i]; - } - if (fix) { - b[0] = Short.MIN_VALUE; - } - } - }, - - FLOAT { - @Override - void convert(int[] src, Object dst, boolean fix) { - float[] b = (float[]) dst; - - for (int i = 0; i < src.length; ++i) { - b[i] = (float) src[i]; - } - if (fix) { - b[0] = Float.NEGATIVE_INFINITY; - } - } - }, - - DOUBLE { - @Override - void convert(int[] src, Object dst, boolean fix) { - double[] b = (double[]) dst; - - 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, boolean fix); - } - - private enum SortedBuilder { - - STEPS { - @Override - void build(int[] a, int m) { - for (int i = 0; i < m; ++i) { - a[i] = 0; - } - - for (int i = m; i < a.length; ++i) { - a[i] = 1; - } - } - }; - - abstract void build(int[] a, int m); - } - - private enum UnsortedBuilder { - - RANDOM { - @Override - void build(int[] a, int m, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextInt(); - } - } - }, - - PERMUTATION { - @Override - void build(int[] a, int m, Random random) { - 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; - } - } - }, - - UNIFORM { - @Override - void build(int[] a, int m, Random random) { - int mask = (m << 15) - 1; - - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextInt() & mask; - } - } - }, - - REPEATED { - @Override - void build(int[] a, int m, Random random) { - 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); - } - } - }, - - SAWTOOTH { - @Override - void build(int[] a, int m, Random random) { - int incCount = 1; - int decCount = a.length; - int i = 0; - int period = m--; - - while (true) { - for (int k = 1; k <= period; ++k) { - if (i >= a.length) { - return; - } - a[i++] = incCount++; - } - period += m; - - for (int k = 1; k <= period; ++k) { - if (i >= a.length) { - return; - } - a[i++] = decCount--; - } - period += m; - } - } - }, - - SHUFFLE { - @Override - void build(int[] a, int m, Random random) { - 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; - } - } - }, - - 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 { - @Override - void build(int[] a, int m) { - int middle = a.length / (m + 1); - - for (int i = 0; i < middle; ++i) { - a[i] = i; - } - - for (int i = middle; i < a.length; ++i) { - a[i] = a.length - i - 1; - } - } - }, - - STAGGER { - @Override - void build(int[] a, int m) { - for (int i = 0; i < a.length; ++i) { - a[i] = (i * m + i) % a.length; - } - } - }, - - PLATEAU { - @Override - void build(int[] a, int m) { - for (int i = 0; i < a.length; ++i) { - a[i] = Math.min(i, m); - } - } - }, - - LATCH { - @Override - void build(int[] a, int m) { - int max = a.length / m; - max = Math.max(max, 2); - - for (int i = 0; i < a.length; ++i) { - a[i] = i % max; - } - } - }, - - POINT { - @Override - void build(int[] a, int m) { - 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) { - a[i] = i; - } - reverse(a, m, a.length - 1); - } - }, - - PEARL { - @Override - void build(int[] a, int m) { - for (int i = 0; i < a.length; ++i) { - a[i] = i; - } - 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) { - a[i] = k--; - } - - for (int i = k1; i < k2; ++i) { - a[i] = 0; - } - - for (int i = k2, k = level; i < a.length; ++i) { - a[i] = k--; - } - } - }; - - abstract void build(int[] a, int m); - - private static void reverse(int[] a, int lo, int hi) { - for (--hi; lo < hi; ) { - int tmp = a[lo]; - a[lo++] = a[hi]; - a[hi--] = tmp; - } - } - } - - private enum NegativeZeroBuilder { - - FLOAT { - @Override - void build(Object o, Random random) { - float[] a = (float[]) o; - - 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) { - a[i] = random.nextBoolean() ? -0.0d : 0.0d; - } - } - }; - - abstract void build(Object o, Random random); - } - - 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[] data = (float[]) o; - int fromIndex = 0; - - 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; - - fillWithValue(data, -0.0f, fromIndex, g); - fromIndex += g; - - fillWithValue(data, 0.0f, fromIndex, z); - fromIndex += z; - - fillWithValue(data, positiveValue, fromIndex, p); - fromIndex += p; - - 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[] data = (double[]) o; - int fromIndex = 0; - - fillWithValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); - fromIndex++; - - fillWithValue(data, -Double.MAX_VALUE, fromIndex, 1); - fromIndex++; - - fillWithValue(data, negativeValue, fromIndex, n); - fromIndex += n; - - fillWithValue(data, -0.0d, fromIndex, g); - fromIndex += g; - - fillWithValue(data, 0.0d, fromIndex, z); - fromIndex += z; - - fillWithValue(data, positiveValue, fromIndex, p); - fromIndex += p; - - 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 fillWithValue(float[] a, float value, int fromIndex, int count) { - for (int i = fromIndex; i < fromIndex + count; ++i) { - a[i] = value; - } - } - - 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 class TestRandom extends Random { - - 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) { - super(seed); - this.seed = Long.toHexString(seed).toUpperCase(); - } - - @Override - public String toString() { - return seed; - } - - private final String seed; - } -} +/* + * Copyright (c) 2009, 2022, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @compile/module=java.base java/util/SortingHelper.java + * @bug 6880672 6896573 6899694 6976036 7013585 7018258 8003981 8226297 8266431 + * @build Sorting + * @run main Sorting -shortrun + * @summary Exercise Arrays.sort, Arrays.parallelSort + * + * @author Vladimir Yaroslavskiy + * @author Jon Bentley + * @author Josh Bloch + */ + +import java.io.PrintStream; +import java.util.Arrays; +import java.util.Random; +import java.util.SortingHelper; + +public class Sorting { + + private static final PrintStream out = System.out; + private static final PrintStream err = System.err; + + // Lengths of arrays for short run + private static final int[] SHORT_RUN_LENGTHS = + { 1, 2, 14, 100, 500, 1_000, 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 }; + + // Initial random values for 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 }; + + // 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; + + public static void main(String[] args) { + long start = System.currentTimeMillis(); + boolean shortRun = args.length > 0 && args[0].equals("-shortrun"); + + 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.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) / 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 testBase() { + testStructured(0); + testEmptyArray(); + + for (int length : lengths) { + createData(length); + testSubArray(length); + + for (TestRandom random : randoms) { + testWithCheckSum(length, random); + testWithScrambling(length, random); + testWithInsertionSort(length, random); + } + } + } + + private void testCore() { + testBase(); + + for (int length : lengths) { + createData(length); + + for (TestRandom random : randoms) { + testNegativeZero(length, random); + testFloatingPointSorting(length, random); + } + } + } + + private void testAll() { + testCore(); + + for (int length : lengths) { + createData(length); + testRange(length); + } + } + + private void testStructured(int min) { + for (int length : lengths) { + createData(length); + testStructured(length, min); + } + } + + private void testEmptyArray() { + sortingHelper.sort(new int[] {}); + sortingHelper.sort(new int[] {}, 0, 0); + + sortingHelper.sort(new long[] {}); + sortingHelper.sort(new long[] {}, 0, 0); + + sortingHelper.sort(new byte[] {}); + sortingHelper.sort(new byte[] {}, 0, 0); + + sortingHelper.sort(new char[] {}); + sortingHelper.sort(new char[] {}, 0, 0); + + sortingHelper.sort(new short[] {}); + sortingHelper.sort(new short[] {}, 0, 0); + + sortingHelper.sort(new float[] {}); + sortingHelper.sort(new float[] {}, 0, 0); + + sortingHelper.sort(new double[] {}); + sortingHelper.sort(new double[] {}, 0, 0); + } + + private void testSubArray(int length) { + if (fix || length < 4) { + return; + } + for (int m = 1; m < length / 2; m <<= 1) { + int toIndex = length - m; + + prepareSubArray((int[]) gold[0], m, toIndex); + convertData(length); + + 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) { + for (int m = 1; m < length; m <<= 1) { + 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", length, + ", m = " + m + ", " + getType(i)); + checkRange(test[i], m); + } + } + out.println(); + } + + private void testWithInsertionSort(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) { + printTestName("Test with insertion sort", random, length, + ", m = " + m + ", " + getType(i) + " " + builder); + sortingHelper.sort(test[i]); + sortByInsertionSort(gold[i]); + checkSorted(gold[i]); + compare(test[i], gold[i]); + } + } + } + out.println(); + } + + private void testStructured(int length, int min) { + if (length < min) { + return; + } + 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 structured", length, + ", m = " + m + ", " + getType(i) + " " + builder); + sortingHelper.sort(test[i]); + checkSorted(test[i]); + } + } + } + out.println(); + } + + 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) { + printTestName("Test with check sum", random, length, + ", m = " + m + ", " + getType(i) + " " + builder); + sortingHelper.sort(test[i]); + checkWithCheckSum(test[i], gold[i]); + } + } + } + out.println(); + } + + 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) { + printTestName("Test with scrambling", random, length, + ", m = " + m + ", " + getType(i) + " " + builder); + scramble(test[i], random); + sortingHelper.sort(test[i]); + compare(test[i], gold[i]); + } + } + } + out.println(); + } + + private void testNegativeZero(int length, TestRandom random) { + for (int i = 5; i < test.length; ++i) { + printTestName("Test negative zero -0.0", random, length, " " + getType(i)); + + NegativeZeroBuilder builder = NegativeZeroBuilder.values()[i - 5]; + builder.build(test[i], random); + + sortingHelper.sort(test[i]); + checkNegativeZero(test[i]); + } + out.println(); + } + + private void testFloatingPointSorting(int length, TestRandom random) { + if (length < 6) { + return; + } + 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 + s != length) { + continue; + } + 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); + copy(test[i], gold[i]); + scramble(test[i], random); + sortingHelper.sort(test[i]); + 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 i = 5; i < test.length; ++i) { + printTestName("Test float-pointing sorting", random, length, + ", 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, g + 2, g); + } + } + out.println(); + } + + private void prepareSubArray(int[] a, int fromIndex, int toIndex) { + 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) { + a[i] = k++; + } + + for (int i = middle; i < toIndex; ++i) { + a[i] = k--; + } + + for (int i = toIndex; i < a.length; ++i) { + a[i] = B747; + } + } + + private void scramble(Object a, Random random) { + if (a instanceof int[]) { + scramble((int[]) a, random); + } else if (a instanceof long[]) { + scramble((long[]) a, random); + } else if (a instanceof byte[]) { + scramble((byte[]) a, random); + } else if (a instanceof char[]) { + scramble((char[]) a, random); + } else if (a instanceof short[]) { + scramble((short[]) a, random); + } else if (a instanceof float[]) { + scramble((float[]) a, random); + } else if (a instanceof double[]) { + scramble((double[]) a, random); + } else { + fail(a); + } + } + + private void scramble(int[] a, Random random) { + 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) { + 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) { + 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) { + 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) { + 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) { + 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) { + swap(a, random.nextInt(a.length), random.nextInt(a.length)); + } + } + + private void swap(int[] a, int i, int j) { + int t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(long[] a, int i, int j) { + long t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(byte[] a, int i, int j) { + byte t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(char[] a, int i, int j) { + char t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(short[] a, int i, int j) { + short t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(float[] a, int i, int j) { + float t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void swap(double[] a, int i, int j) { + double t = a[i]; a[i] = a[j]; a[j] = t; + } + + private void checkWithCheckSum(Object test, Object gold) { + checkSorted(test); + 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"); + } + + private void checkNegativeZero(Object a) { + if (a instanceof float[]) { + checkNegativeZero((float[]) a); + } else if (a instanceof double[]) { + checkNegativeZero((double[]) a); + } else { + fail(a); + } + } + + private void checkNegativeZero(float[] a) { + 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); + } + } + } + + private void checkNegativeZero(double[] a) { + 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); + } + } + } + + private void compare(Object a, Object b, int numNaN, int numNeg, int numNegZero) { + if (a instanceof float[]) { + compare((float[]) a, (float[]) b, numNaN, numNeg, numNegZero); + } else if (a instanceof double[]) { + compare((double[]) a, (double[]) b, numNaN, numNeg, numNegZero); + } else { + 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) { + 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) { + 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) { + if (a[i] != b[i]) { + fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); + } + } + } + + private void compare(double[] a, double[] b, int numNaN, int numNeg, int numNegZero) { + 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) { + 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) { + if (a[i] != b[i]) { + fail("There must be " + b[i] + " instead of " + a[i] + " at position " + i); + } + } + } + + private void compare(Object a, Object b) { + if (a instanceof int[]) { + compare((int[]) a, (int[]) b); + } else if (a instanceof long[]) { + compare((long[]) a, (long[]) b); + } else if (a instanceof byte[]) { + compare((byte[]) a, (byte[]) b); + } else if (a instanceof char[]) { + compare((char[]) a, (char[]) b); + } else if (a instanceof short[]) { + compare((short[]) a, (short[]) b); + } else if (a instanceof float[]) { + compare((float[]) a, (float[]) b); + } else if (a instanceof double[]) { + compare((double[]) a, (double[]) b); + } else { + fail(a); + } + } + + private void compare(int[] a, int[] b) { + 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); + } + } + } + + private void compare(long[] a, long[] b) { + 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); + } + } + } + + private void compare(byte[] a, byte[] b) { + 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); + } + } + } + + private void compare(char[] a, char[] b) { + 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); + } + } + } + + private void compare(short[] a, short[] b) { + 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); + } + } + } + + private void compare(float[] a, float[] b) { + 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); + } + } + } + + private void compare(double[] a, double[] b) { + 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); + } + } + } + + private String getType(int i) { + Object a = test[i]; + + if (a instanceof int[]) { + return "INT "; + } + if (a instanceof long[]) { + return "LONG "; + } + if (a instanceof byte[]) { + return "BYTE "; + } + if (a instanceof char[]) { + return "CHAR "; + } + if (a instanceof short[]) { + return "SHORT "; + } + if (a instanceof float[]) { + return "FLOAT "; + } + if (a instanceof double[]) { + return "DOUBLE"; + } + fail(a); + return null; + } + + private void checkSorted(Object a) { + if (a instanceof int[]) { + checkSorted((int[]) a); + } else if (a instanceof long[]) { + checkSorted((long[]) a); + } else if (a instanceof byte[]) { + checkSorted((byte[]) a); + } else if (a instanceof char[]) { + checkSorted((char[]) a); + } else if (a instanceof short[]) { + checkSorted((short[]) a); + } else if (a instanceof float[]) { + checkSorted((float[]) a); + } else if (a instanceof double[]) { + checkSorted((double[]) a); + } else { + fail(a); + } + } + + private void checkSorted(int[] a) { + 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]); + } + } + } + + private void checkSorted(long[] a) { + 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]); + } + } + } + + private void checkSorted(byte[] a) { + 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]); + } + } + } + + private void checkSorted(char[] a) { + 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]); + } + } + } + + private void checkSorted(short[] a) { + 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]); + } + } + } + + private void checkSorted(float[] a) { + 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]); + } + } + } + + private void checkSorted(double[] a) { + 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]); + } + } + } + + private void checkCheckSum(Object test, Object gold) { + if (checkSumXor(test) != checkSumXor(gold)) { + fail("Original and sorted arrays are not identical [^]"); + } + if (checkSumPlus(test) != checkSumPlus(gold)) { + fail("Original and sorted arrays are not identical [+]"); + } + } + + private int checkSumXor(Object a) { + if (a instanceof int[]) { + return checkSumXor((int[]) a); + } + if (a instanceof long[]) { + return checkSumXor((long[]) a); + } + if (a instanceof byte[]) { + return checkSumXor((byte[]) a); + } + if (a instanceof char[]) { + return checkSumXor((char[]) a); + } + if (a instanceof short[]) { + return checkSumXor((short[]) a); + } + if (a instanceof float[]) { + return checkSumXor((float[]) a); + } + if (a instanceof double[]) { + return checkSumXor((double[]) a); + } + fail(a); + return -1; + } + + private int checkSumXor(int[] a) { + int checkSum = 0; + + for (int e : a) { + checkSum ^= e; + } + return checkSum; + } + + private int checkSumXor(long[] a) { + long checkSum = 0; + + for (long e : a) { + checkSum ^= e; + } + return (int) checkSum; + } + + private int checkSumXor(byte[] a) { + byte checkSum = 0; + + for (byte e : a) { + checkSum ^= e; + } + return checkSum; + } + + private int checkSumXor(char[] a) { + char checkSum = 0; + + for (char e : a) { + checkSum ^= e; + } + return checkSum; + } + + private int checkSumXor(short[] a) { + short checkSum = 0; + + for (short e : a) { + checkSum ^= e; + } + return checkSum; + } + + private int checkSumXor(float[] a) { + int checkSum = 0; + + for (float e : a) { + checkSum ^= (int) e; + } + return checkSum; + } + + private int checkSumXor(double[] a) { + int checkSum = 0; + + for (double e : a) { + checkSum ^= (int) e; + } + return checkSum; + } + + private int checkSumPlus(Object a) { + if (a instanceof int[]) { + return checkSumPlus((int[]) a); + } + if (a instanceof long[]) { + return checkSumPlus((long[]) a); + } + if (a instanceof byte[]) { + return checkSumPlus((byte[]) a); + } + if (a instanceof char[]) { + return checkSumPlus((char[]) a); + } + if (a instanceof short[]) { + return checkSumPlus((short[]) a); + } + if (a instanceof float[]) { + return checkSumPlus((float[]) a); + } + if (a instanceof double[]) { + return checkSumPlus((double[]) a); + } + fail(a); + return -1; + } + + private int checkSumPlus(int[] a) { + int checkSum = 0; + + for (int e : a) { + checkSum += e; + } + return checkSum; + } + + private int checkSumPlus(long[] a) { + long checkSum = 0; + + for (long e : a) { + checkSum += e; + } + return (int) checkSum; + } + + private int checkSumPlus(byte[] a) { + byte checkSum = 0; + + for (byte e : a) { + checkSum += e; + } + return checkSum; + } + + private int checkSumPlus(char[] a) { + char checkSum = 0; + + for (char e : a) { + checkSum += e; + } + return checkSum; + } + + private int checkSumPlus(short[] a) { + short checkSum = 0; + + for (short e : a) { + checkSum += e; + } + return checkSum; + } + + private int checkSumPlus(float[] a) { + int checkSum = 0; + + for (float e : a) { + checkSum += (int) e; + } + return checkSum; + } + + private int checkSumPlus(double[] a) { + int checkSum = 0; + + for (double e : a) { + checkSum += (int) e; + } + return checkSum; + } + + private void sortByInsertionSort(Object a) { + SortingHelper.INSERTION_SORT.sort(a); + } + + private void checkSubArray(Object a, int fromIndex, int toIndex) { + if (a instanceof int[]) { + checkSubArray((int[]) a, fromIndex, toIndex); + } else if (a instanceof long[]) { + checkSubArray((long[]) a, fromIndex, toIndex); + } else if (a instanceof byte[]) { + checkSubArray((byte[]) a, fromIndex, toIndex); + } else if (a instanceof char[]) { + checkSubArray((char[]) a, fromIndex, toIndex); + } else if (a instanceof short[]) { + checkSubArray((short[]) a, fromIndex, toIndex); + } else if (a instanceof float[]) { + checkSubArray((float[]) a, fromIndex, toIndex); + } else if (a instanceof double[]) { + checkSubArray((double[]) a, fromIndex, toIndex); + } else { + fail(a); + } + } + + private void checkSubArray(int[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != B747) { + fail("Range sort changes right element at position " + i + hex(a[i], B747)); + } + } + } + + private void checkSubArray(long[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (long) B747) { + fail("Range sort changes right element at position " + i + hex(a[i], B747)); + } + } + } + + private void checkSubArray(byte[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (byte) B747) { + fail("Range sort changes right element at position " + i + hex(a[i], B747)); + } + } + } + + private void checkSubArray(char[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (char) B747) { + fail("Range sort changes right element at position " + i + hex(a[i], B747)); + } + } + } + + private void checkSubArray(short[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (short) B747) { + fail("Range sort changes right element at position " + i + hex(a[i], B747)); + } + } + } + + private void checkSubArray(float[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (float) B747) { + fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); + } + } + } + + private void checkSubArray(double[] a, int fromIndex, int toIndex) { + 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) { + 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) { + if (a[i] != (double) B747) { + fail("Range sort changes right element at position " + i + hex((long) a[i], B747)); + } + } + } + + private void checkRange(Object a, int m) { + if (a instanceof int[]) { + checkRange((int[]) a, m); + } else if (a instanceof long[]) { + checkRange((long[]) a, m); + } else if (a instanceof byte[]) { + checkRange((byte[]) a, m); + } else if (a instanceof char[]) { + checkRange((char[]) a, m); + } else if (a instanceof short[]) { + checkRange((short[]) a, m); + } else if (a instanceof float[]) { + checkRange((float[]) a, m); + } else if (a instanceof double[]) { + checkRange((double[]) a, m); + } else { + fail(a); + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + 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); + } catch (IllegalArgumentException iae) { + try { + sortingHelper.sort(a, -m, a.length); + fail(sortingHelper + " does not throw ArrayIndexOutOfBoundsException " + + "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)); + } catch (ArrayIndexOutOfBoundsException expected) {} + } + } + } + + private void copy(Object dst, Object src) { + if (src instanceof float[]) { + copy((float[]) dst, (float[]) src); + } else if (src instanceof double[]) { + copy((double[]) dst, (double[]) src); + } else { + fail(src); + } + } + + private void copy(float[] dst, float[] src) { + System.arraycopy(src, 0, dst, 0, src.length); + } + + private void copy(double[] dst, double[] src) { + System.arraycopy(src, 0, dst, 0, src.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] + }; + + test = new Object[] { + 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 = 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) { + System.arraycopy(gold[i], 0, test[i], 0, length); + } + } + + 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); + } + + private enum TypeConverter { + + INT { + @Override + void convert(int[] src, Object dst, boolean fix) { + if (fix) { + src[0] = Integer.MIN_VALUE; + } + } + }, + + LONG { + @Override + void convert(int[] src, Object dst, boolean fix) { + long[] b = (long[]) dst; + + for (int i = 0; i < src.length; ++i) { + b[i] = src[i]; + } + if (fix) { + b[0] = Long.MIN_VALUE; + } + } + }, + + BYTE { + @Override + void convert(int[] src, Object dst, boolean fix) { + byte[] b = (byte[]) dst; + + for (int i = 0; i < src.length; ++i) { + b[i] = (byte) src[i]; + } + if (fix) { + b[0] = Byte.MIN_VALUE; + } + } + }, + + CHAR { + @Override + void convert(int[] src, Object dst, boolean fix) { + char[] b = (char[]) dst; + + for (int i = 0; i < src.length; ++i) { + b[i] = (char) src[i]; + } + if (fix) { + b[0] = Character.MIN_VALUE; + } + } + }, + + SHORT { + @Override + void convert(int[] src, Object dst, boolean fix) { + short[] b = (short[]) dst; + + for (int i = 0; i < src.length; ++i) { + b[i] = (short) src[i]; + } + if (fix) { + b[0] = Short.MIN_VALUE; + } + } + }, + + FLOAT { + @Override + void convert(int[] src, Object dst, boolean fix) { + float[] b = (float[]) dst; + + for (int i = 0; i < src.length; ++i) { + b[i] = (float) src[i]; + } + if (fix) { + b[0] = Float.NEGATIVE_INFINITY; + } + } + }, + + DOUBLE { + @Override + void convert(int[] src, Object dst, boolean fix) { + double[] b = (double[]) dst; + + 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, boolean fix); + } + + private enum SortedBuilder { + + STEPS { + @Override + void build(int[] a, int m) { + for (int i = 0; i < m; ++i) { + a[i] = 0; + } + + for (int i = m; i < a.length; ++i) { + a[i] = 1; + } + } + }; + + abstract void build(int[] a, int m); + } + + private enum UnsortedBuilder { + + RANDOM { + @Override + void build(int[] a, int m, Random random) { + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt(); + } + } + }, + + PERMUTATION { + @Override + void build(int[] a, int m, Random random) { + 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; + } + } + }, + + UNIFORM { + @Override + void build(int[] a, int m, Random random) { + int mask = (m << 15) - 1; + + for (int i = 0; i < a.length; ++i) { + a[i] = random.nextInt() & mask; + } + } + }, + + REPEATED { + @Override + void build(int[] a, int m, Random random) { + 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); + } + } + }, + + SAWTOOTH { + @Override + void build(int[] a, int m, Random random) { + int incCount = 1; + int decCount = a.length; + int i = 0; + int period = m--; + + while (true) { + for (int k = 1; k <= period; ++k) { + if (i >= a.length) { + return; + } + a[i++] = incCount++; + } + period += m; + + for (int k = 1; k <= period; ++k) { + if (i >= a.length) { + return; + } + a[i++] = decCount--; + } + period += m; + } + } + }, + + SHUFFLE { + @Override + void build(int[] a, int m, Random random) { + 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; + } + } + }, + + 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 { + @Override + void build(int[] a, int m) { + int middle = a.length / (m + 1); + + for (int i = 0; i < middle; ++i) { + a[i] = i; + } + + for (int i = middle; i < a.length; ++i) { + a[i] = a.length - i - 1; + } + } + }, + + STAGGER { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { + a[i] = (i * m + i) % a.length; + } + } + }, + + PLATEAU { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { + a[i] = Math.min(i, m); + } + } + }, + + LATCH { + @Override + void build(int[] a, int m) { + int max = a.length / m; + max = Math.max(max, 2); + + for (int i = 0; i < a.length; ++i) { + a[i] = i % max; + } + } + }, + + POINT { + @Override + void build(int[] a, int m) { + 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) { + a[i] = i; + } + reverse(a, m, a.length - 1); + } + }, + + PEARL { + @Override + void build(int[] a, int m) { + for (int i = 0; i < a.length; ++i) { + a[i] = i; + } + 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) { + a[i] = k--; + } + + for (int i = k1; i < k2; ++i) { + a[i] = 0; + } + + for (int i = k2, k = level; i < a.length; ++i) { + a[i] = k--; + } + } + }; + + abstract void build(int[] a, int m); + + private static void reverse(int[] a, int lo, int hi) { + for (--hi; lo < hi; ) { + int tmp = a[lo]; + a[lo++] = a[hi]; + a[hi--] = tmp; + } + } + } + + private enum NegativeZeroBuilder { + + FLOAT { + @Override + void build(Object o, Random random) { + float[] a = (float[]) o; + + 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) { + a[i] = random.nextBoolean() ? -0.0d : 0.0d; + } + } + }; + + abstract void build(Object o, Random random); + } + + 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[] data = (float[]) o; + int fromIndex = 0; + + 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; + + fillWithValue(data, -0.0f, fromIndex, g); + fromIndex += g; + + fillWithValue(data, 0.0f, fromIndex, z); + fromIndex += z; + + fillWithValue(data, positiveValue, fromIndex, p); + fromIndex += p; + + 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[] data = (double[]) o; + int fromIndex = 0; + + fillWithValue(data, Double.NEGATIVE_INFINITY, fromIndex, 1); + fromIndex++; + + fillWithValue(data, -Double.MAX_VALUE, fromIndex, 1); + fromIndex++; + + fillWithValue(data, negativeValue, fromIndex, n); + fromIndex += n; + + fillWithValue(data, -0.0d, fromIndex, g); + fromIndex += g; + + fillWithValue(data, 0.0d, fromIndex, z); + fromIndex += z; + + fillWithValue(data, positiveValue, fromIndex, p); + fromIndex += p; + + 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 fillWithValue(float[] a, float value, int fromIndex, int count) { + for (int i = fromIndex; i < fromIndex + count; ++i) { + a[i] = value; + } + } + + 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 class TestRandom extends Random { + + 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) { + super(seed); + this.seed = Long.toHexString(seed).toUpperCase(); + } + + @Override + public String toString() { + return 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 afc5c3a324dc3..eb77bad6f22f8 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,333 +1,333 @@ -/* - * Copyright (c) 2019, 2022, 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 - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -package java.util; - -/** - * This class provides access to package-private - * methods of DualPivotQuicksort class. - * - * @author Vladimir Yaroslavskiy - * - * @version 2022.06.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, 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.mixedInsertionSort((int[]) a, low, high); - } else if (a instanceof long[]) { - 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, low, high); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, low, high); - } else if (a instanceof float[]) { - DualPivotQuicksort.mixedInsertionSort((float[]) a, low, high); - } else if (a instanceof double[]) { - DualPivotQuicksort.mixedInsertionSort((double[]) a, low, high); - } else { - fail(a); - } - } - }, - - INSERTION_SORT("Insertion sort") { - @Override - public void sort(Object a, int low, int high) { - if (a instanceof int[]) { - DualPivotQuicksort.insertionSort((int[]) a, low, high); - } else if (a instanceof long[]) { - DualPivotQuicksort.insertionSort((long[]) a, low, high); - } else if (a instanceof byte[]) { - DualPivotQuicksort.insertionSort((byte[]) a, low, high); - } else if (a instanceof char[]) { - DualPivotQuicksort.insertionSort((char[]) a, low, high); - } else if (a instanceof short[]) { - DualPivotQuicksort.insertionSort((short[]) a, low, high); - } else if (a instanceof float[]) { - DualPivotQuicksort.insertionSort((float[]) a, low, high); - } else if (a instanceof double[]) { - 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[]) { - check("Merging", DualPivotQuicksort.tryMergingSort(null, (int[]) a, low, high - low)); - } else if (a instanceof long[]) { - 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, low, high); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, low, high); - } else if (a instanceof float[]) { - check("Merging", DualPivotQuicksort.tryMergingSort(null, (float[]) a, low, high - low)); - } else if (a instanceof double[]) { - check("Merging", DualPivotQuicksort.tryMergingSort(null, (double[]) a, low, high - low)); - } else { - fail(a); - } - } - }, - - RADIX_SORT("Radix sort") { - @Override - public void sort(Object a, int low, int high) { - if (a instanceof int[]) { - check("Radix", DualPivotQuicksort.tryRadixSort(null, (int[]) a, low, high)); - } else if (a instanceof long[]) { - check("Radix", DualPivotQuicksort.tryRadixSort(null, (long[]) a, 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[]) { - check("Radix", DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high)); - } else if (a instanceof double[]) { - 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.heapSort((int[]) a, low, high); - } else if (a instanceof long[]) { - 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, low, high); - } else if (a instanceof short[]) { - DualPivotQuicksort.sort((short[]) a, low, high); - } else if (a instanceof float[]) { - DualPivotQuicksort.heapSort((float[]) a, low, high); - } else if (a instanceof double[]) { - DualPivotQuicksort.heapSort((double[]) a, low, high); - } else { - fail(a); - } - } - }, - - ARRAYS_SORT("Arrays.sort") { - @Override - public void sort(Object a) { - if (a instanceof int[]) { - Arrays.sort((int[]) a); - } else if (a instanceof long[]) { - Arrays.sort((long[]) a); - } else if (a instanceof byte[]) { - Arrays.sort((byte[]) a); - } else if (a instanceof char[]) { - Arrays.sort((char[]) a); - } else if (a instanceof short[]) { - Arrays.sort((short[]) a); - } else if (a instanceof float[]) { - Arrays.sort((float[]) a); - } else if (a instanceof double[]) { - Arrays.sort((double[]) a); - } else { - fail(a); - } - } - - @Override - public void sort(Object a, int low, int high) { - if (a instanceof int[]) { - Arrays.sort((int[]) a, low, high); - } else if (a instanceof long[]) { - Arrays.sort((long[]) a, low, high); - } else if (a instanceof byte[]) { - Arrays.sort((byte[]) a, low, high); - } else if (a instanceof char[]) { - Arrays.sort((char[]) a, low, high); - } else if (a instanceof short[]) { - Arrays.sort((short[]) a, low, high); - } else if (a instanceof float[]) { - Arrays.sort((float[]) a, low, high); - } else if (a instanceof double[]) { - Arrays.sort((double[]) a, low, high); - } else { - fail(a); - } - } - }, - - ARRAYS_PARALLEL_SORT("Arrays.parallelSort") { - @Override - public void sort(Object a) { - if (a instanceof int[]) { - Arrays.parallelSort((int[]) a); - } else if (a instanceof long[]) { - Arrays.parallelSort((long[]) a); - } else if (a instanceof byte[]) { - Arrays.parallelSort((byte[]) a); - } else if (a instanceof char[]) { - Arrays.parallelSort((char[]) a); - } else if (a instanceof short[]) { - Arrays.parallelSort((short[]) a); - } else if (a instanceof float[]) { - Arrays.parallelSort((float[]) a); - } else if (a instanceof double[]) { - Arrays.parallelSort((double[]) a); - } else { - fail(a); - } - } - - @Override - public void sort(Object a, int low, int high) { - if (a instanceof int[]) { - Arrays.parallelSort((int[]) a, low, high); - } else if (a instanceof long[]) { - Arrays.parallelSort((long[]) a, low, high); - } else if (a instanceof byte[]) { - Arrays.parallelSort((byte[]) a, low, high); - } else if (a instanceof char[]) { - Arrays.parallelSort((char[]) a, low, high); - } else if (a instanceof short[]) { - Arrays.parallelSort((short[]) a, low, high); - } else if (a instanceof float[]) { - Arrays.parallelSort((float[]) a, low, high); - } else if (a instanceof double[]) { - Arrays.parallelSort((double[]) a, low, high); - } else { - fail(a); - } - } - }; - - abstract public void sort(Object a, int low, int high); - - 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); - } - } - - SortingHelper(String name) { - this.name = name; - } - - @Override - 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) { - fail("Unknown array: " + a.getClass().getName()); - } - - private static void fail(String message) { - throw new RuntimeException(message); - } - - private final String name; - - /** - * Parallelism level for sequential sorting. - */ - private static final int SEQUENTIAL = 0; - - /** - * Parallelism level for parallel sorting. - */ - private static final int PARALLEL = 88; -} +/* + * Copyright (c) 2019, 2022, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package java.util; + +/** + * This class provides access to package-private + * methods of DualPivotQuicksort class. + * + * @author Vladimir Yaroslavskiy + * + * @version 2022.06.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, 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.mixedInsertionSort((int[]) a, low, high); + } else if (a instanceof long[]) { + 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, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, low, high); + } else if (a instanceof float[]) { + DualPivotQuicksort.mixedInsertionSort((float[]) a, low, high); + } else if (a instanceof double[]) { + DualPivotQuicksort.mixedInsertionSort((double[]) a, low, high); + } else { + fail(a); + } + } + }, + + INSERTION_SORT("Insertion sort") { + @Override + public void sort(Object a, int low, int high) { + if (a instanceof int[]) { + DualPivotQuicksort.insertionSort((int[]) a, low, high); + } else if (a instanceof long[]) { + DualPivotQuicksort.insertionSort((long[]) a, low, high); + } else if (a instanceof byte[]) { + DualPivotQuicksort.insertionSort((byte[]) a, low, high); + } else if (a instanceof char[]) { + DualPivotQuicksort.insertionSort((char[]) a, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.insertionSort((short[]) a, low, high); + } else if (a instanceof float[]) { + DualPivotQuicksort.insertionSort((float[]) a, low, high); + } else if (a instanceof double[]) { + 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[]) { + check("Merging", DualPivotQuicksort.tryMergingSort(null, (int[]) a, low, high - low)); + } else if (a instanceof long[]) { + 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, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, low, high); + } else if (a instanceof float[]) { + check("Merging", DualPivotQuicksort.tryMergingSort(null, (float[]) a, low, high - low)); + } else if (a instanceof double[]) { + check("Merging", DualPivotQuicksort.tryMergingSort(null, (double[]) a, low, high - low)); + } else { + fail(a); + } + } + }, + + RADIX_SORT("Radix sort") { + @Override + public void sort(Object a, int low, int high) { + if (a instanceof int[]) { + check("Radix", DualPivotQuicksort.tryRadixSort(null, (int[]) a, low, high)); + } else if (a instanceof long[]) { + check("Radix", DualPivotQuicksort.tryRadixSort(null, (long[]) a, 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[]) { + check("Radix", DualPivotQuicksort.tryRadixSort(null, (float[]) a, low, high)); + } else if (a instanceof double[]) { + 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.heapSort((int[]) a, low, high); + } else if (a instanceof long[]) { + 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, low, high); + } else if (a instanceof short[]) { + DualPivotQuicksort.sort((short[]) a, low, high); + } else if (a instanceof float[]) { + DualPivotQuicksort.heapSort((float[]) a, low, high); + } else if (a instanceof double[]) { + DualPivotQuicksort.heapSort((double[]) a, low, high); + } else { + fail(a); + } + } + }, + + ARRAYS_SORT("Arrays.sort") { + @Override + public void sort(Object a) { + if (a instanceof int[]) { + Arrays.sort((int[]) a); + } else if (a instanceof long[]) { + Arrays.sort((long[]) a); + } else if (a instanceof byte[]) { + Arrays.sort((byte[]) a); + } else if (a instanceof char[]) { + Arrays.sort((char[]) a); + } else if (a instanceof short[]) { + Arrays.sort((short[]) a); + } else if (a instanceof float[]) { + Arrays.sort((float[]) a); + } else if (a instanceof double[]) { + Arrays.sort((double[]) a); + } else { + fail(a); + } + } + + @Override + public void sort(Object a, int low, int high) { + if (a instanceof int[]) { + Arrays.sort((int[]) a, low, high); + } else if (a instanceof long[]) { + Arrays.sort((long[]) a, low, high); + } else if (a instanceof byte[]) { + Arrays.sort((byte[]) a, low, high); + } else if (a instanceof char[]) { + Arrays.sort((char[]) a, low, high); + } else if (a instanceof short[]) { + Arrays.sort((short[]) a, low, high); + } else if (a instanceof float[]) { + Arrays.sort((float[]) a, low, high); + } else if (a instanceof double[]) { + Arrays.sort((double[]) a, low, high); + } else { + fail(a); + } + } + }, + + ARRAYS_PARALLEL_SORT("Arrays.parallelSort") { + @Override + public void sort(Object a) { + if (a instanceof int[]) { + Arrays.parallelSort((int[]) a); + } else if (a instanceof long[]) { + Arrays.parallelSort((long[]) a); + } else if (a instanceof byte[]) { + Arrays.parallelSort((byte[]) a); + } else if (a instanceof char[]) { + Arrays.parallelSort((char[]) a); + } else if (a instanceof short[]) { + Arrays.parallelSort((short[]) a); + } else if (a instanceof float[]) { + Arrays.parallelSort((float[]) a); + } else if (a instanceof double[]) { + Arrays.parallelSort((double[]) a); + } else { + fail(a); + } + } + + @Override + public void sort(Object a, int low, int high) { + if (a instanceof int[]) { + Arrays.parallelSort((int[]) a, low, high); + } else if (a instanceof long[]) { + Arrays.parallelSort((long[]) a, low, high); + } else if (a instanceof byte[]) { + Arrays.parallelSort((byte[]) a, low, high); + } else if (a instanceof char[]) { + Arrays.parallelSort((char[]) a, low, high); + } else if (a instanceof short[]) { + Arrays.parallelSort((short[]) a, low, high); + } else if (a instanceof float[]) { + Arrays.parallelSort((float[]) a, low, high); + } else if (a instanceof double[]) { + Arrays.parallelSort((double[]) a, low, high); + } else { + fail(a); + } + } + }; + + abstract public void sort(Object a, int low, int high); + + 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); + } + } + + SortingHelper(String name) { + this.name = name; + } + + @Override + 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) { + fail("Unknown array: " + a.getClass().getName()); + } + + private static void fail(String message) { + throw new RuntimeException(message); + } + + private final String name; + + /** + * Parallelism level for sequential sorting. + */ + private static final int SEQUENTIAL = 0; + + /** + * Parallelism level for parallel sorting. + */ + 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 bf38cd8fc5c83..7f4290b34c347 100644 --- a/test/micro/org/openjdk/bench/java/util/ArraysSort.java +++ b/test/micro/org/openjdk/bench/java/util/ArraysSort.java @@ -52,501 +52,258 @@ @Fork(1) @State(Scope.Benchmark) @BenchmarkMode(Mode.AverageTime) -@OutputTimeUnit(TimeUnit.MILLISECONDS) +@OutputTimeUnit(TimeUnit.MICROSECONDS) @Warmup(iterations = 1, time = 5, timeUnit = TimeUnit.SECONDS) @Measurement(iterations = 4, time = 3, timeUnit = TimeUnit.SECONDS) public class ArraysSort { - @Param({ "100", "1000", "10000", "100000", "1000000" }) + @Param({ "400", "7000", "50000", "800000", "2000000" }) int size; - Random random; + @Param + Builder builder; - @Setup(Level.Iteration) - public void start() { - random = new Random(0x777); - } - - public static class Int extends ArraysSort { + int[] b; - @Param - private Type type; + @Setup + public void init() { + b = new int[size]; + } - int[] gold; + public enum Builder { - public enum Type { + RANDOM { + @Override + void build(int[] b) { + Random random = new Random(0x777); - RANDOM { - @Override - void build(int[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextInt(); - } + for (int i = 0; i < b.length; ++i) { + b[i] = random.nextInt(); } - }, - - REPEATED { - @Override - void build(int[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = i % 7; - } + } + }, + + REPEATED { + @Override + void build(int[] b) { + for (int i = 0; i < b.length; ++i) { + b[i] = i % 11; } - }, - - STAGGER { - @Override - void build(int[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (i * 5) % a.length; - } + } + }, + + STAGGER { + @Override + void build(int[] b) { + for (int i = 0; i < b.length; ++i) { + b[i] = (i * 3) % b.length; } - }, - - SHUFFLE { - @Override - void build(int[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); - } + } + }, + + SHUFFLE { + @Override + void build(int[] b) { + Random random = new Random(0x777); + + for (int i = 0, j = 0, k = 1; i < b.length; ++i) { + b[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); } - }; + } + }; - abstract void build(int[] a, Random random); - } + abstract void build(int[] b); + } - @Setup - public void setup() { - gold = new int[size]; - } + public static class Int extends ArraysSort { @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(b); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(b); } } public static class Long extends ArraysSort { - @Param - private Type type; - - long[] gold; - - public enum Type { - - RANDOM { - @Override - void build(long[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextLong(); - } - } - }, - - REPEATED { - @Override - void build(long[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = i % 7; - } - } - }, - - STAGGER { - @Override - void build(long[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (i * 5L) % a.length; - } - } - }, - - SHUFFLE { - @Override - void build(long[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); - } - } - }; - - abstract void build(long[] a, Random random); - } + long[] a; @Setup public void setup() { - gold = new long[size]; + a = new long[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } public static class Byte extends ArraysSort { - @Param - private Type type; - - byte[] gold; - - public enum Type { - - RANDOM { - @Override - void build(byte[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (byte) random.nextInt(); - } - } - }, - - REPEATED { - @Override - void build(byte[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (byte) (i % 7); - } - } - }, - - STAGGER { - @Override - void build(byte[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (byte) ((i * 5) % a.length); - } - } - }, - - SHUFFLE { - @Override - void build(byte[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = (byte) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); - } - } - }; - - abstract void build(byte[] a, Random random); - } + byte[] a; @Setup public void setup() { - gold = new byte[size]; + a = new byte[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (byte) b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } public static class Char extends ArraysSort { - @Param - private Type type; - - char[] gold; - - public enum Type { - - RANDOM { - @Override - void build(char[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (char) random.nextInt(); - } - } - }, - - REPEATED { - @Override - void build(char[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (char) (i % 7); - } - } - }, - - STAGGER { - @Override - void build(char[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (char) ((i * 5) % a.length); - } - } - }, - - SHUFFLE { - @Override - void build(char[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = (char) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); - } - } - }; - - abstract void build(char[] a, Random random); - } + char[] a; @Setup public void setup() { - gold = new char[size]; + a = new char[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (char) b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } public static class Short extends ArraysSort { - @Param - private Type type; - - short[] gold; - - public enum Type { - - RANDOM { - @Override - void build(short[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (short) random.nextInt(); - } - } - }, - - REPEATED { - @Override - void build(short[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (short) (i % 7); - } - } - }, - - STAGGER { - @Override - void build(short[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (short) ((i * 5) % a.length); - } - } - }, - - SHUFFLE { - @Override - void build(short[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = (short) (random.nextInt(6) > 0 ? (j += 2) : (k += 2)); - } - } - }; - - abstract void build(short[] a, Random random); - } + short[] a; @Setup public void setup() { - gold = new short[size]; + a = new short[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = (short) b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } public static class Float extends ArraysSort { - @Param - private Type type; - - float[] gold; - - public enum Type { - - RANDOM { - @Override - void build(float[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextFloat(); - } - } - }, - - REPEATED { - @Override - void build(float[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = i % 7; - } - } - }, - - STAGGER { - @Override - void build(float[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (i * 5) % a.length; - } - } - }, - - SHUFFLE { - @Override - void build(float[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); - } - } - }; - - abstract void build(float[] a, Random random); - } + float[] a; @Setup public void setup() { - gold = new float[size]; + a = new float[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } public static class Double extends ArraysSort { - @Param - private Type type; - - double[] gold; - - public enum Type { - - RANDOM { - @Override - void build(double[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = random.nextDouble(); - } - } - }, - - REPEATED { - @Override - void build(double[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = i % 7; - } - } - }, - - STAGGER { - @Override - void build(double[] a, Random random) { - for (int i = 0; i < a.length; ++i) { - a[i] = (i * 5) % a.length; - } - } - }, - - SHUFFLE { - @Override - void build(double[] a, Random random) { - for (int i = 0, j = 0, k = 1; i < a.length; ++i) { - a[i] = random.nextInt(6) > 0 ? (j += 2) : (k += 2); - } - } - }; - - abstract void build(double[] a, Random random); - } + double[] a; @Setup public void setup() { - gold = new double[size]; + a = new double[size]; } @Setup(Level.Invocation) - public void init() { - type.build(gold, random); + public void build() { + builder.build(b); + + for (int i = 0; i < size; ++i) { + a[i] = b[i]; + } } @Benchmark public void testSort() { - Arrays.sort(gold); + Arrays.sort(a); } @Benchmark public void testParallelSort() { - Arrays.parallelSort(gold); + Arrays.parallelSort(a); } } } From 203610a51162d49764fb2cf18d3cf7ae4db66540 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Thu, 10 Nov 2022 00:02:57 +0300 Subject: [PATCH 18/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Fixed microbenchmarking tests --- .../java/util/Arrays/java.base/java/util/SortingHelper.java | 4 +--- test/micro/org/openjdk/bench/java/util/ArraysSort.java | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) 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 eb77bad6f22f8..a6f6286da8054 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 @@ -4,9 +4,7 @@ * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. + * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or diff --git a/test/micro/org/openjdk/bench/java/util/ArraysSort.java b/test/micro/org/openjdk/bench/java/util/ArraysSort.java index 7f4290b34c347..6db2497eb8f24 100644 --- a/test/micro/org/openjdk/bench/java/util/ArraysSort.java +++ b/test/micro/org/openjdk/bench/java/util/ArraysSort.java @@ -57,7 +57,7 @@ @Measurement(iterations = 4, time = 3, timeUnit = TimeUnit.SECONDS) public class ArraysSort { - @Param({ "400", "7000", "50000", "800000", "2000000" }) + @Param({ "800", "7000", "50000", "300000", "2000000" }) int size; @Param @@ -86,8 +86,10 @@ void build(int[] b) { REPEATED { @Override void build(int[] b) { + Random random = new Random(0x777); + for (int i = 0; i < b.length; ++i) { - b[i] = i % 11; + b[i] = random.nextInt(4); } } }, From 4f6ece03f0acad76d7741ab28e696ed374e8b2d2 Mon Sep 17 00:00:00 2001 From: Vladimir Yaroslavskiy Date: Mon, 13 Mar 2023 00:24:30 +0300 Subject: [PATCH 19/19] JDK-8266431: Dual-Pivot Quicksort improvements (Radix sort) * Use uninitialized array for buffers --- .../classes/java/util/DualPivotQuicksort.java | 164 +++++++++--------- test/jdk/java/util/Arrays/Sorting.java | 2 +- .../java.base/java/util/SortingHelper.java | 4 +- .../openjdk/bench/java/util/ArraysSort.java | 4 +- 4 files changed, 84 insertions(+), 90 deletions(-) diff --git a/src/java.base/share/classes/java/util/DualPivotQuicksort.java b/src/java.base/share/classes/java/util/DualPivotQuicksort.java index bec5505c293ab..31ddc66e23607 100644 --- a/src/java.base/share/classes/java/util/DualPivotQuicksort.java +++ b/src/java.base/share/classes/java/util/DualPivotQuicksort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 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 @@ -26,6 +26,7 @@ package java.util; import java.util.concurrent.CountedCompleter; +import jdk.internal.misc.Unsafe; /** * This class implements powerful and fully optimized versions, both @@ -46,7 +47,7 @@ * * @version 2022.06.14 * - * @since 1.7 * 14 ^ 20 + * @since 1.7 * 14 ^ 22 */ final class DualPivotQuicksort { @@ -139,7 +140,7 @@ private DualPivotQuicksort() {} */ static void sort(int[] a, int parallelism, int low, int high) { if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -155,7 +156,7 @@ static void sort(int[] a, int parallelism, 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 */ - 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 size = high - low; @@ -560,7 +561,7 @@ static void insertionSort(int[] a, int low, int high) { * @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 tryMergingSort(Sorter sorter, int[] a, int low, int high) { + static boolean tryMergingSort(Sorter sorter, int[] a, int low, int high) { /* * The element run[i] holds the start index @@ -652,9 +653,9 @@ static boolean tryMergingSort(Sorter sorter, int[] a, int low, int high) { if (count > 1) { int[] b; int offset = low; - if (sorter != null && (b = (int[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, high - low)) == null) { + } else if ((b = tryAllocate(int[].class, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -709,7 +710,7 @@ private static int[] mergeRuns(int[] a, int[] b, int offset, * 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(); + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -729,7 +730,7 @@ 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, + private static void mergeParts(Merger merger, int[] dst, int k, int[] a1, int lo1, int hi1, int[] a2, int lo2, int hi2) { if (merger != null && a1 == a2) { @@ -816,15 +817,15 @@ private static void mergeParts(Merger merger, int[] dst, int k, * @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) { + 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 = (int[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (int[]) tryAllocate(a, size)) == null) { + } else if ((b = tryAllocate(int[].class, size)) == null) { return false; } @@ -995,7 +996,7 @@ private static void pushDown(int[] a, int p, int value, int low, int high) { */ static void sort(long[] a, int parallelism, int low, int high) { if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -1011,7 +1012,7 @@ static void sort(long[] a, int parallelism, 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 */ - 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 size = high - low; @@ -1416,7 +1417,7 @@ static void insertionSort(long[] a, int low, int high) { * @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 tryMergingSort(Sorter sorter, long[] a, int low, int high) { + static boolean tryMergingSort(Sorter sorter, long[] a, int low, int high) { /* * The element run[i] holds the start index @@ -1508,9 +1509,9 @@ static boolean tryMergingSort(Sorter sorter, long[] a, int low, int high) { if (count > 1) { long[] b; int offset = low; - if (sorter != null && (b = (long[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, high - low)) == null) { + } else if ((b = tryAllocate(long[].class, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -1565,7 +1566,7 @@ private static long[] mergeRuns(long[] a, long[] b, int offset, * 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(); + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -1585,7 +1586,7 @@ 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, + private static void mergeParts(Merger merger, long[] dst, int k, long[] a1, int lo1, int hi1, long[] a2, int lo2, int hi2) { if (merger != null && a1 == a2) { @@ -1672,15 +1673,15 @@ private static void mergeParts(Merger merger, long[] dst, int k, * @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, long[] a, int low, int 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 = (long[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (long[]) tryAllocate(a, size)) == null) { + } else if ((b = tryAllocate(long[].class, size)) == null) { return false; } @@ -2622,7 +2623,7 @@ static void sort(float[] a, int parallelism, int low, int high) { * which are already in place. */ if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -2667,7 +2668,7 @@ static void sort(float[] a, int parallelism, 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 */ - 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 size = high - low; @@ -3072,7 +3073,7 @@ static void insertionSort(float[] a, int low, int high) { * @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 tryMergingSort(Sorter sorter, float[] a, int low, int high) { + static boolean tryMergingSort(Sorter sorter, float[] a, int low, int high) { /* * The element run[i] holds the start index @@ -3164,9 +3165,9 @@ static boolean tryMergingSort(Sorter sorter, float[] a, int low, int high) { if (count > 1) { float[] b; int offset = low; - if (sorter != null && (b = (float[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, high - low)) == null) { + } else if ((b = tryAllocate(float[].class, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -3221,7 +3222,7 @@ private static float[] mergeRuns(float[] a, float[] b, int offset, * 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(); + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -3241,7 +3242,7 @@ 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, + private static void mergeParts(Merger merger, float[] dst, int k, float[] a1, int lo1, int hi1, float[] a2, int lo2, int hi2) { if (merger != null && a1 == a2) { @@ -3328,15 +3329,15 @@ private static void mergeParts(Merger merger, float[] dst, int k, * @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) { + 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 = (float[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (float[]) tryAllocate(a, size)) == null) { + } else if ((b = tryAllocate(float[].class, size)) == null) { return false; } @@ -3508,7 +3509,7 @@ static void sort(double[] a, int parallelism, int low, int high) { * which are already in place. */ if (parallelism > 1 && high - low > MIN_PARALLEL_SORT_SIZE) { - new Sorter(a, parallelism, low, high - low, 0).invoke(); + new Sorter<>(a, parallelism, low, high - low, 0).invoke(); } else { sort(null, a, 0, low, high); } @@ -3553,7 +3554,7 @@ static void sort(double[] a, int parallelism, 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 */ - 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 size = high - low; @@ -3958,7 +3959,7 @@ static void insertionSort(double[] a, int low, int high) { * @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 tryMergingSort(Sorter sorter, double[] a, int low, int high) { + static boolean tryMergingSort(Sorter sorter, double[] a, int low, int high) { /* * The element run[i] holds the start index @@ -4050,9 +4051,9 @@ static boolean tryMergingSort(Sorter sorter, double[] a, int low, int high) { if (count > 1) { double[] b; int offset = low; - if (sorter != null && (b = (double[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, high - low)) == null) { + } else if ((b = tryAllocate(double[].class, high - low)) == null) { return false; } mergeRuns(a, b, offset, 1, sorter != null, run, 0, count); @@ -4107,7 +4108,7 @@ private static double[] mergeRuns(double[] a, double[] b, int offset, * 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(); + new Merger<>(null, dst, k, a1, lo1, hi1, a2, lo2, hi2).invoke(); } else { mergeParts(null, dst, k, a1, lo1, hi1, a2, lo2, hi2); } @@ -4127,7 +4128,7 @@ 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, + private static void mergeParts(Merger merger, double[] dst, int k, double[] a1, int lo1, int hi1, double[] a2, int lo2, int hi2) { if (merger != null && a1 == a2) { @@ -4214,15 +4215,15 @@ private static void mergeParts(Merger merger, double[] dst, int k, * @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) { + 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 = (double[]) sorter.b) != null) { + if (sorter != null && (b = sorter.b) != null) { offset = sorter.offset; - } else if ((b = (double[]) tryAllocate(a, size)) == null) { + } else if ((b = tryAllocate(double[].class, size)) == null) { return false; } @@ -4411,15 +4412,16 @@ private static void pushDown(double[] a, int p, double value, int low, int high) /** * This class implements parallel sorting. */ - private static final class Sorter extends CountedCompleter { + 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; - private Sorter(Object a, int parallelism, int low, int size, int depth) { + @SuppressWarnings("unchecked") + private Sorter(T a, int parallelism, int low, int size, int depth) { this.a = a; this.low = low; this.size = size; @@ -4428,12 +4430,12 @@ private Sorter(Object a, int parallelism, int low, int size, int depth) { while ((parallelism >>= 2) > 0 && (size >>= 2) > 0) { depth -= 2; } - this.b = tryAllocate(a, this.size); + 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; @@ -4444,21 +4446,22 @@ private Sorter(CountedCompleter parent, } @Override + @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 array: " + a.getClass().getName()); } @@ -4467,12 +4470,12 @@ public void compute() { } @Override - public void onCompletion(CountedCompleter parent) { + public void onCompletion(CountedCompleter caller) { if (depth < 0) { int mi = low + (size >> 1); boolean src = (depth & 1) == 0; - new Merger(null, + new Merger<>(null, a, src ? low : low - offset, b, @@ -4487,23 +4490,23 @@ public void onCompletion(CountedCompleter parent) { private void fork(int depth, int low, int high) { addToPendingCount(1); - 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 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; @@ -4516,18 +4519,19 @@ private Merger(CountedCompleter parent, Object dst, int k, } @Override + @SuppressWarnings("unchecked") public void compute() { if (dst instanceof int[]) { - mergeParts(this, (int[]) dst, k, + mergeParts((Merger) this, (int[]) dst, k, (int[]) a1, lo1, hi1, (int[]) a2, lo2, hi2); } else if (dst instanceof long[]) { - mergeParts(this, (long[]) dst, k, + mergeParts((Merger) this, (long[]) dst, k, (long[]) a1, lo1, hi1, (long[]) a2, lo2, hi2); } else if (dst instanceof float[]) { - mergeParts(this, (float[]) dst, k, + mergeParts((Merger) this, (float[]) dst, k, (float[]) a1, lo1, hi1, (float[]) a2, lo2, hi2); } else if (dst instanceof double[]) { - mergeParts(this, (double[]) dst, k, + mergeParts((Merger) this, (double[]) dst, k, (double[]) a1, lo1, hi1, (double[]) a2, lo2, hi2); } else { throw new IllegalArgumentException("Unknown array: " + dst.getClass().getName()); @@ -4537,37 +4541,27 @@ public void compute() { 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(); } } /** * Tries to allocate additional buffer. * - * @param a the given array + * @param clazz the given array class * @param size the size of additional buffer - * @return {@code null} if requested size is too large, otherwise created buffer - */ - private static Object tryAllocate(Object a, int size) { + * @return {@code null} if requested size is too large or there is not enough memory, + * otherwise created buffer + */ + @SuppressWarnings("unchecked") + private static T tryAllocate(Class clazz, int size) { try { - if (size > MAX_BUFFER_SIZE) { - return null; - } - if (a instanceof int[]) { - return new int[size]; - } - if (a instanceof long[]) { - return new long[size]; - } - if (a instanceof float[]) { - return new float[size]; - } - if (a instanceof double[]) { - return new double[size]; - } - throw new IllegalArgumentException("Unknown array: " + a.getClass().getName()); + return size > MAX_BUFFER_SIZE ? null : + (T) U.allocateUninitializedArray(clazz.componentType(), size); } 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 4706ee7e24545..55ee8cd13736b 100644 --- a/test/jdk/java/util/Arrays/Sorting.java +++ b/test/jdk/java/util/Arrays/Sorting.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2009, 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 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 a6f6286da8054..dd2c3e153527d 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, 2022, 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 @@ -31,7 +31,7 @@ * * @version 2022.06.14 * - * @since 14 ^ 20 + * @since 14 ^ 22 */ public enum SortingHelper { diff --git a/test/micro/org/openjdk/bench/java/util/ArraysSort.java b/test/micro/org/openjdk/bench/java/util/ArraysSort.java index 6db2497eb8f24..e8de83e012f28 100644 --- a/test/micro/org/openjdk/bench/java/util/ArraysSort.java +++ b/test/micro/org/openjdk/bench/java/util/ArraysSort.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 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 @@ -47,7 +47,7 @@ * * @version 2022.06.14 * - * @since 20 + * @since 22 */ @Fork(1) @State(Scope.Benchmark)