1804N/A/*
3909N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1804N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1804N/A *
1804N/A * This code is free software; you can redistribute it and/or modify it
1804N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1804N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1804N/A *
1804N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1804N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1804N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1804N/A * version 2 for more details (a copy is included in the LICENSE file that
1804N/A * accompanied this code).
1804N/A *
1804N/A * You should have received a copy of the GNU General Public License version
1804N/A * 2 along with this work; if not, write to the Free Software Foundation,
1804N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1804N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1804N/A */
1804N/A
1804N/Apackage java.util;
1804N/A
1804N/A/**
1804N/A * This class implements the Dual-Pivot Quicksort algorithm by
2571N/A * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm
1804N/A * offers O(n log(n)) performance on many data sets that cause other
1804N/A * quicksorts to degrade to quadratic performance, and is typically
1804N/A * faster than traditional (one-pivot) Quicksort implementations.
1804N/A *
1804N/A * @author Vladimir Yaroslavskiy
1804N/A * @author Jon Bentley
1804N/A * @author Josh Bloch
1804N/A *
3506N/A * @version 2011.02.11 m765.827.12i:5\7pm
2571N/A * @since 1.7
1804N/A */
1804N/Afinal class DualPivotQuicksort {
1804N/A
1875N/A /**
1961N/A * Prevents instantiation.
1875N/A */
1804N/A private DualPivotQuicksort() {}
1804N/A
1804N/A /*
1875N/A * Tuning parameters.
1804N/A */
1804N/A
1804N/A /**
3501N/A * The maximum number of runs in merge sort.
3501N/A */
3501N/A private static final int MAX_RUN_COUNT = 67;
3501N/A
3501N/A /**
3501N/A * The maximum length of run in merge sort.
3501N/A */
3501N/A private static final int MAX_RUN_LENGTH = 33;
3501N/A
3501N/A /**
3501N/A * If the length of an array to be sorted is less than this
3501N/A * constant, Quicksort is used in preference to merge sort.
3501N/A */
3501N/A private static final int QUICKSORT_THRESHOLD = 286;
3501N/A
3501N/A /**
1804N/A * If the length of an array to be sorted is less than this
1804N/A * constant, insertion sort is used in preference to Quicksort.
1804N/A */
2930N/A private static final int INSERTION_SORT_THRESHOLD = 47;
1804N/A
1804N/A /**
2930N/A * If the length of a byte array to be sorted is greater than this
2930N/A * constant, counting sort is used in preference to insertion sort.
1804N/A */
2930N/A private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
1804N/A
1804N/A /**
1804N/A * If the length of a short or char array to be sorted is greater
1804N/A * than this constant, counting sort is used in preference to Quicksort.
1804N/A */
2930N/A private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
1804N/A
1804N/A /*
2571N/A * Sorting methods for seven primitive types.
1804N/A */
1804N/A
1804N/A /**
2930N/A * Sorts the specified array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A */
1867N/A public static void sort(int[] a) {
3501N/A sort(a, 0, a.length - 1);
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified range of the array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A */
2930N/A public static void sort(int[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A int[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new int[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new int[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A int[] t = a; a = b; b = t;
3501N/A }
2930N/A }
2930N/A
2930N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
2930N/A *
2930N/A * @param a the array to be sorted
2930N/A * @param left the index of the first element, inclusive, to be sorted
2930N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
1867N/A */
2571N/A private static void sort(int[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1804N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A int ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A int a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A int last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
2571N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
1804N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
1804N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
1875N/A
2571N/A if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A int pivot1 = a[e2];
3501N/A int pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A int ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A int ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = pivot1;
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A int pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A int ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
3501N/A a[k] = pivot;
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A */
1867N/A public static void sort(long[] a) {
3501N/A sort(a, 0, a.length - 1);
1804N/A }
1804N/A
1804N/A /**
2930N/A * Sorts the specified range of the array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A */
2930N/A public static void sort(long[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A long t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A long[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new long[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new long[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A long[] t = a; a = b; b = t;
3501N/A }
2930N/A }
2930N/A
2930N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
2930N/A *
2930N/A * @param a the array to be sorted
2930N/A * @param left the index of the first element, inclusive, to be sorted
2930N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
1867N/A */
2571N/A private static void sort(long[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1804N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A long ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A long a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A long last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
2571N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
1804N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
1804N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
1875N/A
2571N/A if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A long pivot1 = a[e2];
3501N/A long pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A long ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A long ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = pivot1;
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A long pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A long ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
3501N/A a[k] = pivot;
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A */
1867N/A public static void sort(short[] a) {
2930N/A sort(a, 0, a.length - 1);
1804N/A }
1804N/A
1867N/A /**
2930N/A * Sorts the specified range of the array.
1867N/A *
1867N/A * @param a the array to be sorted
2930N/A * @param left the index of the first element, inclusive, to be sorted
2930N/A * @param right the index of the last element, inclusive, to be sorted
1867N/A */
2930N/A public static void sort(short[] a, int left, int right) {
2930N/A // Use counting sort on large arrays
2930N/A if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
2930N/A int[] count = new int[NUM_SHORT_VALUES];
2571N/A
3501N/A for (int i = left - 1; ++i <= right;
3501N/A count[a[i] - Short.MIN_VALUE]++
3501N/A );
2930N/A for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
2930N/A while (count[--i] == 0);
2930N/A short value = (short) (i + Short.MIN_VALUE);
2930N/A int s = count[i];
2930N/A
2930N/A do {
2930N/A a[--k] = value;
2930N/A } while (--s > 0);
2930N/A }
2930N/A } else { // Use Dual-Pivot Quicksort on small arrays
3501N/A doSort(a, left, right);
2571N/A }
1867N/A }
1867N/A
1867N/A /** The number of distinct short values. */
1804N/A private static final int NUM_SHORT_VALUES = 1 << 16;
1804N/A
1804N/A /**
3501N/A * Sorts the specified range of the array.
3501N/A *
3501N/A * @param a the array to be sorted
3501N/A * @param left the index of the first element, inclusive, to be sorted
3501N/A * @param right the index of the last element, inclusive, to be sorted
3501N/A */
3501N/A private static void doSort(short[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A short t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A short[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new short[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new short[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A short[] t = a; a = b; b = t;
3501N/A }
3501N/A }
3501N/A
3501N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
2571N/A */
3501N/A private static void sort(short[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1875N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A short ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A short a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A short last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
1875N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
2571N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
2571N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { short t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2571N/A
2571N/A if (a[e3] < a[e2]) { short t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { short t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { short t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A short pivot1 = a[e2];
3501N/A short pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A short ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A short ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = pivot1;
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A short pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A short ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
3501N/A a[k] = pivot;
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1804N/A }
1804N/A
1804N/A /**
2930N/A * Sorts the specified array.
1804N/A *
1804N/A * @param a the array to be sorted
1804N/A */
1867N/A public static void sort(char[] a) {
2930N/A sort(a, 0, a.length - 1);
1804N/A }
1804N/A
1804N/A /**
2930N/A * Sorts the specified range of the array.
1804N/A *
1804N/A * @param a the array to be sorted
2930N/A * @param left the index of the first element, inclusive, to be sorted
2930N/A * @param right the index of the last element, inclusive, to be sorted
1804N/A */
2930N/A public static void sort(char[] a, int left, int right) {
2930N/A // Use counting sort on large arrays
2930N/A if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
2930N/A int[] count = new int[NUM_CHAR_VALUES];
2571N/A
3501N/A for (int i = left - 1; ++i <= right;
3501N/A count[a[i]]++
3501N/A );
2930N/A for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
2930N/A while (count[--i] == 0);
2930N/A char value = (char) i;
2930N/A int s = count[i];
2930N/A
2930N/A do {
2930N/A a[--k] = value;
2930N/A } while (--s > 0);
2930N/A }
2930N/A } else { // Use Dual-Pivot Quicksort on small arrays
3501N/A doSort(a, left, right);
2571N/A }
1804N/A }
1804N/A
1867N/A /** The number of distinct char values. */
1804N/A private static final int NUM_CHAR_VALUES = 1 << 16;
1804N/A
1804N/A /**
3501N/A * Sorts the specified range of the array.
3501N/A *
3501N/A * @param a the array to be sorted
3501N/A * @param left the index of the first element, inclusive, to be sorted
3501N/A * @param right the index of the last element, inclusive, to be sorted
3501N/A */
3501N/A private static void doSort(char[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A char t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A char[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new char[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new char[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A char[] t = a; a = b; b = t;
3501N/A }
3501N/A }
3501N/A
3501N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
2571N/A */
2571N/A private static void sort(char[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1875N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A char ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A char a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A char last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
1875N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
2571N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
2571N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { char t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2571N/A
2571N/A if (a[e3] < a[e2]) { char t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { char t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { char t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A char pivot1 = a[e2];
3501N/A char pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A char ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A char ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = pivot1;
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A char pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A char ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
3501N/A a[k] = pivot;
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1867N/A }
1867N/A
1867N/A /** The number of distinct byte values. */
1867N/A private static final int NUM_BYTE_VALUES = 1 << 8;
1867N/A
1867N/A /**
2930N/A * Sorts the specified array.
2930N/A *
2930N/A * @param a the array to be sorted
2930N/A */
2930N/A public static void sort(byte[] a) {
2930N/A sort(a, 0, a.length - 1);
2930N/A }
2930N/A
2930N/A /**
2930N/A * Sorts the specified range of the array.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
1804N/A */
2930N/A public static void sort(byte[] a, int left, int right) {
2930N/A // Use counting sort on large arrays
2930N/A if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
2930N/A int[] count = new int[NUM_BYTE_VALUES];
2930N/A
3501N/A for (int i = left - 1; ++i <= right;
3501N/A count[a[i] - Byte.MIN_VALUE]++
3501N/A );
2930N/A for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
2930N/A while (count[--i] == 0);
2930N/A byte value = (byte) (i + Byte.MIN_VALUE);
2930N/A int s = count[i];
2571N/A
2930N/A do {
2930N/A a[--k] = value;
2930N/A } while (--s > 0);
2571N/A }
2930N/A } else { // Use insertion sort on small arrays
2930N/A for (int i = left, j = i; i < right; j = ++i) {
2930N/A byte ai = a[i + 1];
2930N/A while (ai < a[j]) {
2930N/A a[j + 1] = a[j];
2930N/A if (j-- == left) {
2930N/A break;
2930N/A }
2930N/A }
2930N/A a[j + 1] = ai;
2930N/A }
2571N/A }
2571N/A }
2571N/A
2571N/A /**
2930N/A * Sorts the specified array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A */
1867N/A public static void sort(float[] a) {
2930N/A sort(a, 0, a.length - 1);
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified range of the array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
1867N/A */
2930N/A public static void sort(float[] a, int left, int right) {
1867N/A /*
2571N/A * Phase 1: Move NaNs to the end of the array.
1867N/A */
2571N/A while (left <= right && Float.isNaN(a[right])) {
3501N/A --right;
2571N/A }
2930N/A for (int k = right; --k >= left; ) {
1867N/A float ak = a[k];
2571N/A if (ak != ak) { // a[k] is NaN
2571N/A a[k] = a[right];
2571N/A a[right] = ak;
3501N/A --right;
1867N/A }
1867N/A }
1867N/A
1867N/A /*
2571N/A * Phase 2: Sort everything except NaNs (which are already in place).
1867N/A */
3501N/A doSort(a, left, right);
1867N/A
1867N/A /*
2571N/A * Phase 3: Place negative zeros before positive zeros.
1867N/A */
2571N/A int hi = right;
1867N/A
2571N/A /*
2930N/A * Find the first zero, or first positive, or last negative element.
2571N/A */
2571N/A while (left < hi) {
2571N/A int middle = (left + hi) >>> 1;
1867N/A float middleValue = a[middle];
1867N/A
1867N/A if (middleValue < 0.0f) {
2571N/A left = middle + 1;
2571N/A } else {
2571N/A hi = middle;
2571N/A }
2571N/A }
2571N/A
2571N/A /*
2571N/A * Skip the last negative value (if any) or all leading negative zeros.
2571N/A */
2571N/A while (left <= right && Float.floatToRawIntBits(a[left]) < 0) {
3501N/A ++left;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Move negative zeros to the beginning of the sub-range.
2571N/A *
2571N/A * Partitioning:
2571N/A *
2930N/A * +----------------------------------------------------+
2930N/A * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
2930N/A * +----------------------------------------------------+
2930N/A * ^ ^ ^
2930N/A * | | |
2930N/A * left p k
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, left) < 0.0
2571N/A * all in [left, p) == -0.0
2571N/A * all in [p, k) == 0.0
2571N/A * all in [k, right] >= 0.0
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2930N/A for (int k = left, p = left - 1; ++k <= right; ) {
2571N/A float ak = a[k];
2571N/A if (ak != 0.0f) {
2571N/A break;
2571N/A }
2571N/A if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f
2571N/A a[k] = 0.0f;
2930N/A a[++p] = -0.0f;
1867N/A }
1867N/A }
1867N/A }
1867N/A
1867N/A /**
3501N/A * Sorts the specified range of the array.
3501N/A *
3501N/A * @param a the array to be sorted
3501N/A * @param left the index of the first element, inclusive, to be sorted
3501N/A * @param right the index of the last element, inclusive, to be sorted
3501N/A */
3501N/A private static void doSort(float[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A float t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A float[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new float[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new float[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A float[] t = a; a = b; b = t;
3501N/A }
3501N/A }
3501N/A
3501N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
1804N/A */
3501N/A private static void sort(float[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1804N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A float ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A float a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A float last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
1875N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
2571N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
1875N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { float t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2571N/A
2571N/A if (a[e3] < a[e2]) { float t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { float t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { float t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A float pivot1 = a[e2];
3501N/A float pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A float ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A float ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = a[great];
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A float pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A float ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
2571N/A a[k] = a[great];
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified array.
1867N/A *
1867N/A * @param a the array to be sorted
1867N/A */
1867N/A public static void sort(double[] a) {
2930N/A sort(a, 0, a.length - 1);
1867N/A }
1867N/A
1867N/A /**
2930N/A * Sorts the specified range of the array.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
1804N/A */
2930N/A public static void sort(double[] a, int left, int right) {
1867N/A /*
2571N/A * Phase 1: Move NaNs to the end of the array.
1867N/A */
2571N/A while (left <= right && Double.isNaN(a[right])) {
3501N/A --right;
2571N/A }
2930N/A for (int k = right; --k >= left; ) {
1867N/A double ak = a[k];
2571N/A if (ak != ak) { // a[k] is NaN
2571N/A a[k] = a[right];
2571N/A a[right] = ak;
3501N/A --right;
1867N/A }
1867N/A }
1867N/A
1867N/A /*
2571N/A * Phase 2: Sort everything except NaNs (which are already in place).
1867N/A */
3501N/A doSort(a, left, right);
1867N/A
1867N/A /*
2571N/A * Phase 3: Place negative zeros before positive zeros.
1867N/A */
2571N/A int hi = right;
1867N/A
2571N/A /*
2930N/A * Find the first zero, or first positive, or last negative element.
2571N/A */
2571N/A while (left < hi) {
2571N/A int middle = (left + hi) >>> 1;
1867N/A double middleValue = a[middle];
1867N/A
1867N/A if (middleValue < 0.0d) {
2571N/A left = middle + 1;
2571N/A } else {
2571N/A hi = middle;
2571N/A }
2571N/A }
2571N/A
2571N/A /*
2571N/A * Skip the last negative value (if any) or all leading negative zeros.
2571N/A */
2571N/A while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) {
3501N/A ++left;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Move negative zeros to the beginning of the sub-range.
2571N/A *
2571N/A * Partitioning:
2571N/A *
2930N/A * +----------------------------------------------------+
2930N/A * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
2930N/A * +----------------------------------------------------+
2930N/A * ^ ^ ^
2930N/A * | | |
2930N/A * left p k
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, left) < 0.0
2571N/A * all in [left, p) == -0.0
2571N/A * all in [p, k) == 0.0
2571N/A * all in [k, right] >= 0.0
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2930N/A for (int k = left, p = left - 1; ++k <= right; ) {
2571N/A double ak = a[k];
2571N/A if (ak != 0.0d) {
2571N/A break;
2571N/A }
2571N/A if (Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d
2571N/A a[k] = 0.0d;
2930N/A a[++p] = -0.0d;
1867N/A }
1867N/A }
1867N/A }
1867N/A
1867N/A /**
3501N/A * Sorts the specified range of the array.
3501N/A *
3501N/A * @param a the array to be sorted
3501N/A * @param left the index of the first element, inclusive, to be sorted
3501N/A * @param right the index of the last element, inclusive, to be sorted
3501N/A */
3501N/A private static void doSort(double[] a, int left, int right) {
3501N/A // Use Quicksort on small arrays
3501N/A if (right - left < QUICKSORT_THRESHOLD) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Index run[i] is the start of i-th run
3501N/A * (ascending or descending sequence).
3501N/A */
3506N/A int[] run = new int[MAX_RUN_COUNT + 1];
3501N/A int count = 0; run[0] = left;
3501N/A
3501N/A // Check if the array is nearly sorted
3501N/A for (int k = left; k < right; run[count] = k) {
3501N/A if (a[k] < a[k + 1]) { // ascending
3501N/A while (++k <= right && a[k - 1] <= a[k]);
3501N/A } else if (a[k] > a[k + 1]) { // descending
3501N/A while (++k <= right && a[k - 1] >= a[k]);
3501N/A for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
3501N/A double t = a[lo]; a[lo] = a[hi]; a[hi] = t;
3501N/A }
3501N/A } else { // equal
3501N/A for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
3501N/A if (--m == 0) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A }
3501N/A
3501N/A /*
3501N/A * The array is not highly structured,
3501N/A * use Quicksort instead of merge sort.
3501N/A */
3501N/A if (++count == MAX_RUN_COUNT) {
3501N/A sort(a, left, right, true);
3501N/A return;
3501N/A }
3501N/A }
3501N/A
3501N/A // Check special cases
3501N/A if (run[count] == right++) { // The last run contains one element
3501N/A run[++count] = right;
3501N/A } else if (count == 1) { // The array is already sorted
3501N/A return;
3501N/A }
3501N/A
3501N/A /*
3501N/A * Create temporary array, which is used for merging.
3501N/A * Implementation note: variable "right" is increased by 1.
3501N/A */
3501N/A double[] b; byte odd = 0;
3501N/A for (int n = 1; (n <<= 1) < count; odd ^= 1);
3501N/A
3501N/A if (odd == 0) {
3501N/A b = a; a = new double[b.length];
3501N/A for (int i = left - 1; ++i < right; a[i] = b[i]);
3501N/A } else {
3501N/A b = new double[a.length];
3501N/A }
3501N/A
3501N/A // Merging
3501N/A for (int last; count > 1; count = last) {
3501N/A for (int k = (last = 0) + 2; k <= count; k += 2) {
3501N/A int hi = run[k], mi = run[k - 1];
3501N/A for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
3501N/A if (q >= hi || p < mi && a[p] <= a[q]) {
3501N/A b[i] = a[p++];
3501N/A } else {
3501N/A b[i] = a[q++];
3501N/A }
3501N/A }
3501N/A run[++last] = hi;
3501N/A }
3501N/A if ((count & 1) != 0) {
3501N/A for (int i = right, lo = run[count - 1]; --i >= lo;
3501N/A b[i] = a[i]
3501N/A );
3501N/A run[++last] = right;
3501N/A }
3501N/A double[] t = a; a = b; b = t;
3501N/A }
3501N/A }
3501N/A
3501N/A /**
2930N/A * Sorts the specified range of the array by Dual-Pivot Quicksort.
1804N/A *
1804N/A * @param a the array to be sorted
1867N/A * @param left the index of the first element, inclusive, to be sorted
1867N/A * @param right the index of the last element, inclusive, to be sorted
2930N/A * @param leftmost indicates if this part is the leftmost in the range
1804N/A */
3501N/A private static void sort(double[] a, int left, int right, boolean leftmost) {
2571N/A int length = right - left + 1;
1804N/A
3501N/A // Use insertion sort on tiny arrays
2571N/A if (length < INSERTION_SORT_THRESHOLD) {
2930N/A if (leftmost) {
2571N/A /*
2930N/A * Traditional (without sentinel) insertion sort,
2930N/A * optimized for server VM, is used in case of
2930N/A * the leftmost part.
2571N/A */
2571N/A for (int i = left, j = i; i < right; j = ++i) {
2571N/A double ai = a[i + 1];
2571N/A while (ai < a[j]) {
2571N/A a[j + 1] = a[j];
2571N/A if (j-- == left) {
2571N/A break;
2571N/A }
2571N/A }
2571N/A a[j + 1] = ai;
2571N/A }
2930N/A } else {
2930N/A /*
2930N/A * Skip the longest ascending sequence.
2930N/A */
2930N/A do {
3501N/A if (left >= right) {
2930N/A return;
2930N/A }
3501N/A } while (a[++left] >= a[left - 1]);
2930N/A
2930N/A /*
2930N/A * Every element from adjoining part plays the role
2930N/A * of sentinel, therefore this allows us to avoid the
2930N/A * left range check on each iteration. Moreover, we use
3501N/A * the more optimized algorithm, so called pair insertion
3501N/A * sort, which is faster (in the context of Quicksort)
3501N/A * than traditional implementation of insertion sort.
2930N/A */
3501N/A for (int k = left; ++left <= right; k = ++left) {
3501N/A double a1 = a[k], a2 = a[left];
2930N/A
3501N/A if (a1 < a2) {
3501N/A a2 = a1; a1 = a[left];
2930N/A }
2930N/A while (a1 < a[--k]) {
2930N/A a[k + 2] = a[k];
2930N/A }
2930N/A a[++k + 1] = a1;
2930N/A
2930N/A while (a2 < a[--k]) {
2930N/A a[k + 1] = a[k];
2930N/A }
2930N/A a[k + 1] = a2;
2930N/A }
2930N/A double last = a[right];
2930N/A
2930N/A while (last < a[--right]) {
2930N/A a[right + 1] = a[right];
2930N/A }
2930N/A a[right + 1] = last;
2571N/A }
2571N/A return;
2571N/A }
1875N/A
2571N/A // Inexpensive approximation of length / 7
2930N/A int seventh = (length >> 3) + (length >> 6) + 1;
2571N/A
2571N/A /*
2571N/A * Sort five evenly spaced elements around (and including) the
2571N/A * center element in the range. These elements will be used for
2571N/A * pivot selection as described below. The choice for spacing
2571N/A * these elements was empirically determined to work well on
2571N/A * a wide variety of inputs.
2571N/A */
2571N/A int e3 = (left + right) >>> 1; // The midpoint
2571N/A int e2 = e3 - seventh;
2571N/A int e1 = e2 - seventh;
2571N/A int e4 = e3 + seventh;
2571N/A int e5 = e4 + seventh;
1875N/A
2571N/A // Sort these elements using insertion sort
2571N/A if (a[e2] < a[e1]) { double t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2571N/A
2571N/A if (a[e3] < a[e2]) { double t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A if (a[e4] < a[e3]) { double t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A if (a[e5] < a[e4]) { double t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2571N/A if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2571N/A if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2571N/A if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2571N/A }
2571N/A }
2571N/A }
1804N/A
1804N/A // Pointers
2571N/A int less = left; // The index of the first element of center part
2571N/A int great = right; // The index before the first element of right part
1804N/A
3501N/A if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
3501N/A /*
3501N/A * Use the second and fourth of the five sorted elements as pivots.
3501N/A * These values are inexpensive approximations of the first and
3501N/A * second terciles of the array. Note that pivot1 <= pivot2.
3501N/A */
3501N/A double pivot1 = a[e2];
3501N/A double pivot2 = a[e4];
3501N/A
2571N/A /*
2571N/A * The first and the last elements to be sorted are moved to the
2571N/A * locations formerly occupied by the pivots. When partitioning
2571N/A * is complete, the pivots are swapped back into their final
2571N/A * positions, and excluded from subsequent sorting.
2571N/A */
2571N/A a[e2] = a[left];
2571N/A a[e4] = a[right];
1804N/A
2571N/A /*
2571N/A * Skip elements, which are less or greater than pivot values.
2571N/A */
2571N/A while (a[++less] < pivot1);
2571N/A while (a[--great] > pivot2);
2571N/A
1804N/A /*
1961N/A * Partitioning:
1961N/A *
2571N/A * left part center part right part
2571N/A * +--------------------------------------------------------------+
2571N/A * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2571N/A * +--------------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1961N/A *
1804N/A * Invariants:
1961N/A *
1804N/A * all in (left, less) < pivot1
1804N/A * pivot1 <= all in [less, k) <= pivot2
1804N/A * all in (great, right) > pivot2
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
1875N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
1804N/A double ak = a[k];
1961N/A if (ak < pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i++;" instead
2930N/A * of "a[i++] = b;" due to performance issue.
2930N/A */
2571N/A a[less] = ak;
3501N/A ++less;
1961N/A } else if (ak > pivot2) { // Move a[k] to right part
1875N/A while (a[great] > pivot2) {
1961N/A if (great-- == k) {
1875N/A break outer;
1875N/A }
1804N/A }
2930N/A if (a[great] < pivot1) { // a[great] <= pivot2
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
1961N/A } else { // pivot1 <= a[great] <= pivot2
1961N/A a[k] = a[great];
2571N/A }
2930N/A /*
2930N/A * Here and below we use "a[i] = b; i--;" instead
2930N/A * of "a[i--] = b;" due to performance issue.
2930N/A */
2571N/A a[great] = ak;
3501N/A --great;
2571N/A }
2571N/A }
2571N/A
2571N/A // Swap pivots into their final positions
2571N/A a[left] = a[less - 1]; a[less - 1] = pivot1;
2571N/A a[right] = a[great + 1]; a[great + 1] = pivot2;
2571N/A
2571N/A // Sort left and right parts recursively, excluding known pivots
2571N/A sort(a, left, less - 2, leftmost);
2571N/A sort(a, great + 2, right, false);
2571N/A
2571N/A /*
2930N/A * If center part is too large (comprises > 4/7 of the array),
2571N/A * swap internal pivot values to ends.
2571N/A */
2571N/A if (less < e1 && e5 < great) {
2571N/A /*
2571N/A * Skip elements, which are equal to pivot values.
2571N/A */
2571N/A while (a[less] == pivot1) {
3501N/A ++less;
2571N/A }
3501N/A
2571N/A while (a[great] == pivot2) {
3501N/A --great;
2571N/A }
2571N/A
2571N/A /*
2571N/A * Partitioning:
2571N/A *
2571N/A * left part center part right part
2571N/A * +----------------------------------------------------------+
2571N/A * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2571N/A * +----------------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
2571N/A *
2571N/A * Invariants:
2571N/A *
2571N/A * all in (*, less) == pivot1
2571N/A * pivot1 < all in [less, k) < pivot2
2571N/A * all in (great, *) == pivot2
2571N/A *
2571N/A * Pointer k is the first index of ?-part.
2571N/A */
2571N/A outer:
2930N/A for (int k = less - 1; ++k <= great; ) {
2571N/A double ak = a[k];
2571N/A if (ak == pivot1) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
2571N/A } else if (ak == pivot2) { // Move a[k] to right part
2571N/A while (a[great] == pivot2) {
2571N/A if (great-- == k) {
2571N/A break outer;
2571N/A }
2571N/A }
2930N/A if (a[great] == pivot1) { // a[great] < pivot2
2571N/A a[k] = a[less];
2571N/A /*
2571N/A * Even though a[great] equals to pivot1, the
2571N/A * assignment a[less] = pivot1 may be incorrect,
2571N/A * if a[great] and pivot1 are floating-point zeros
2571N/A * of different signs. Therefore in float and
2571N/A * double sorting methods we have to use more
2571N/A * accurate assignment a[less] = a[great].
2571N/A */
2571N/A a[less] = a[great];
3501N/A ++less;
2571N/A } else { // pivot1 < a[great] < pivot2
2571N/A a[k] = a[great];
2571N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1804N/A }
2571N/A
2571N/A // Sort center part recursively
2571N/A sort(a, less, great, false);
2571N/A
3501N/A } else { // Partitioning with one pivot
3501N/A /*
3501N/A * Use the third of the five sorted elements as pivot.
3501N/A * This value is inexpensive approximation of the median.
3501N/A */
3501N/A double pivot = a[e3];
3501N/A
1804N/A /*
2930N/A * Partitioning degenerates to the traditional 3-way
2571N/A * (or "Dutch National Flag") schema:
1804N/A *
2571N/A * left part center part right part
2571N/A * +-------------------------------------------------+
2571N/A * | < pivot | == pivot | ? | > pivot |
2571N/A * +-------------------------------------------------+
2571N/A * ^ ^ ^
2571N/A * | | |
2571N/A * less k great
1804N/A *
1804N/A * Invariants:
1804N/A *
1804N/A * all in (left, less) < pivot
1804N/A * all in [less, k) == pivot
1804N/A * all in (great, right) > pivot
1804N/A *
2571N/A * Pointer k is the first index of ?-part.
1804N/A */
2930N/A for (int k = less; k <= great; ++k) {
3501N/A if (a[k] == pivot) {
1875N/A continue;
1804N/A }
2571N/A double ak = a[k];
3501N/A if (ak < pivot) { // Move a[k] to left part
2571N/A a[k] = a[less];
2571N/A a[less] = ak;
3501N/A ++less;
3501N/A } else { // a[k] > pivot - Move a[k] to right part
3501N/A while (a[great] > pivot) {
3501N/A --great;
1804N/A }
3501N/A if (a[great] < pivot) { // a[great] <= pivot
1804N/A a[k] = a[less];
2571N/A a[less] = a[great];
3501N/A ++less;
3501N/A } else { // a[great] == pivot
2571N/A /*
3501N/A * Even though a[great] equals to pivot, the
3501N/A * assignment a[k] = pivot may be incorrect,
3501N/A * if a[great] and pivot are floating-point
2571N/A * zeros of different signs. Therefore in float
2571N/A * and double sorting methods we have to use
2571N/A * more accurate assignment a[k] = a[great].
2571N/A */
2571N/A a[k] = a[great];
1804N/A }
2571N/A a[great] = ak;
3501N/A --great;
1804N/A }
1804N/A }
1961N/A
2930N/A /*
2930N/A * Sort left and right parts recursively.
2930N/A * All elements from center part are equal
2930N/A * and, therefore, already sorted.
2930N/A */
2571N/A sort(a, left, less - 1, leftmost);
2571N/A sort(a, great + 1, right, false);
1804N/A }
1867N/A }
1804N/A}