Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
29 * This class implements the Dual-Pivot Quicksort algorithm by
30 * Vladimir Yaroslavskiy, Jon Bentley, and Josh Bloch. The algorithm
31 * offers O(n log(n)) performance on many data sets that cause other
32 * quicksorts to degrade to quadratic performance, and is typically
33 * faster than traditional (one-pivot) Quicksort implementations.
35 * @author Vladimir Yaroslavskiy
39 * @version 2011.02.11 m765.827.12i:5\7pm
42 final class DualPivotQuicksort {
45 * Prevents instantiation.
47 private DualPivotQuicksort() {}
54 * The maximum number of runs in merge sort.
56 private static final int MAX_RUN_COUNT = 67;
59 * The maximum length of run in merge sort.
61 private static final int MAX_RUN_LENGTH = 33;
64 * If the length of an array to be sorted is less than this
65 * constant, Quicksort is used in preference to merge sort.
67 private static final int QUICKSORT_THRESHOLD = 286;
70 * If the length of an array to be sorted is less than this
71 * constant, insertion sort is used in preference to Quicksort.
73 private static final int INSERTION_SORT_THRESHOLD = 47;
76 * If the length of a byte array to be sorted is greater than this
77 * constant, counting sort is used in preference to insertion sort.
79 private static final int COUNTING_SORT_THRESHOLD_FOR_BYTE = 29;
82 * If the length of a short or char array to be sorted is greater
83 * than this constant, counting sort is used in preference to Quicksort.
85 private static final int COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR = 3200;
88 * Sorting methods for seven primitive types.
92 * Sorts the specified array.
94 * @param a the array to be sorted
96 public static void sort(int[] a) {
97 sort(a, 0, a.length - 1);
101 * Sorts the specified range of the array.
103 * @param a the array to be sorted
104 * @param left the index of the first element, inclusive, to be sorted
105 * @param right the index of the last element, inclusive, to be sorted
107 public static void sort(int[] a, int left, int right) {
108 // Use Quicksort on small arrays
109 if (right - left < QUICKSORT_THRESHOLD) {
110 sort(a, left, right, true);
115 * Index run[i] is the start of i-th run
116 * (ascending or descending sequence).
118 int[] run = new int[MAX_RUN_COUNT + 1];
119 int count = 0; run[0] = left;
121 // Check if the array is nearly sorted
122 for (int k = left; k < right; run[count] = k) {
123 if (a[k] < a[k + 1]) { // ascending
124 while (++k <= right && a[k - 1] <= a[k]);
125 } else if (a[k] > a[k + 1]) { // descending
126 while (++k <= right && a[k - 1] >= a[k]);
127 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
128 int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
131 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
133 sort(a, left, right, true);
140 * The array is not highly structured,
141 * use Quicksort instead of merge sort.
143 if (++count == MAX_RUN_COUNT) {
144 sort(a, left, right, true);
149 // Check special cases
150 if (run[count] == right++) { // The last run contains one element
151 run[++count] = right;
152 } else if (count == 1) { // The array is already sorted
157 * Create temporary array, which is used for merging.
158 * Implementation note: variable "right" is increased by 1.
160 int[] b; byte odd = 0;
161 for (int n = 1; (n <<= 1) < count; odd ^= 1);
164 b = a; a = new int[b.length];
165 for (int i = left - 1; ++i < right; a[i] = b[i]);
167 b = new int[a.length];
171 for (int last; count > 1; count = last) {
172 for (int k = (last = 0) + 2; k <= count; k += 2) {
173 int hi = run[k], mi = run[k - 1];
174 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
175 if (q >= hi || p < mi && a[p] <= a[q]) {
183 if ((count & 1) != 0) {
184 for (int i = right, lo = run[count - 1]; --i >= lo;
189 int[] t = a; a = b; b = t;
194 * Sorts the specified range of the array by Dual-Pivot Quicksort.
196 * @param a the array to be sorted
197 * @param left the index of the first element, inclusive, to be sorted
198 * @param right the index of the last element, inclusive, to be sorted
199 * @param leftmost indicates if this part is the leftmost in the range
201 private static void sort(int[] a, int left, int right, boolean leftmost) {
202 int length = right - left + 1;
204 // Use insertion sort on tiny arrays
205 if (length < INSERTION_SORT_THRESHOLD) {
208 * Traditional (without sentinel) insertion sort,
209 * optimized for server VM, is used in case of
212 for (int i = left, j = i; i < right; j = ++i) {
224 * Skip the longest ascending sequence.
230 } while (a[++left] >= a[left - 1]);
233 * Every element from adjoining part plays the role
234 * of sentinel, therefore this allows us to avoid the
235 * left range check on each iteration. Moreover, we use
236 * the more optimized algorithm, so called pair insertion
237 * sort, which is faster (in the context of Quicksort)
238 * than traditional implementation of insertion sort.
240 for (int k = left; ++left <= right; k = ++left) {
241 int a1 = a[k], a2 = a[left];
244 a2 = a1; a1 = a[left];
246 while (a1 < a[--k]) {
251 while (a2 < a[--k]) {
258 while (last < a[--right]) {
259 a[right + 1] = a[right];
266 // Inexpensive approximation of length / 7
267 int seventh = (length >> 3) + (length >> 6) + 1;
270 * Sort five evenly spaced elements around (and including) the
271 * center element in the range. These elements will be used for
272 * pivot selection as described below. The choice for spacing
273 * these elements was empirically determined to work well on
274 * a wide variety of inputs.
276 int e3 = (left + right) >>> 1; // The midpoint
277 int e2 = e3 - seventh;
278 int e1 = e2 - seventh;
279 int e4 = e3 + seventh;
280 int e5 = e4 + seventh;
282 // Sort these elements using insertion sort
283 if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
285 if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
286 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
288 if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
289 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
290 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
293 if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
294 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
295 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
296 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
302 int less = left; // The index of the first element of center part
303 int great = right; // The index before the first element of right part
305 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
307 * Use the second and fourth of the five sorted elements as pivots.
308 * These values are inexpensive approximations of the first and
309 * second terciles of the array. Note that pivot1 <= pivot2.
315 * The first and the last elements to be sorted are moved to the
316 * locations formerly occupied by the pivots. When partitioning
317 * is complete, the pivots are swapped back into their final
318 * positions, and excluded from subsequent sorting.
324 * Skip elements, which are less or greater than pivot values.
326 while (a[++less] < pivot1);
327 while (a[--great] > pivot2);
332 * left part center part right part
333 * +--------------------------------------------------------------+
334 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
335 * +--------------------------------------------------------------+
342 * all in (left, less) < pivot1
343 * pivot1 <= all in [less, k) <= pivot2
344 * all in (great, right) > pivot2
346 * Pointer k is the first index of ?-part.
349 for (int k = less - 1; ++k <= great; ) {
351 if (ak < pivot1) { // Move a[k] to left part
354 * Here and below we use "a[i] = b; i++;" instead
355 * of "a[i++] = b;" due to performance issue.
359 } else if (ak > pivot2) { // Move a[k] to right part
360 while (a[great] > pivot2) {
365 if (a[great] < pivot1) { // a[great] <= pivot2
369 } else { // pivot1 <= a[great] <= pivot2
373 * Here and below we use "a[i] = b; i--;" instead
374 * of "a[i--] = b;" due to performance issue.
381 // Swap pivots into their final positions
382 a[left] = a[less - 1]; a[less - 1] = pivot1;
383 a[right] = a[great + 1]; a[great + 1] = pivot2;
385 // Sort left and right parts recursively, excluding known pivots
386 sort(a, left, less - 2, leftmost);
387 sort(a, great + 2, right, false);
390 * If center part is too large (comprises > 4/7 of the array),
391 * swap internal pivot values to ends.
393 if (less < e1 && e5 < great) {
395 * Skip elements, which are equal to pivot values.
397 while (a[less] == pivot1) {
401 while (a[great] == pivot2) {
408 * left part center part right part
409 * +----------------------------------------------------------+
410 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
411 * +----------------------------------------------------------+
418 * all in (*, less) == pivot1
419 * pivot1 < all in [less, k) < pivot2
420 * all in (great, *) == pivot2
422 * Pointer k is the first index of ?-part.
425 for (int k = less - 1; ++k <= great; ) {
427 if (ak == pivot1) { // Move a[k] to left part
431 } else if (ak == pivot2) { // Move a[k] to right part
432 while (a[great] == pivot2) {
437 if (a[great] == pivot1) { // a[great] < pivot2
440 * Even though a[great] equals to pivot1, the
441 * assignment a[less] = pivot1 may be incorrect,
442 * if a[great] and pivot1 are floating-point zeros
443 * of different signs. Therefore in float and
444 * double sorting methods we have to use more
445 * accurate assignment a[less] = a[great].
449 } else { // pivot1 < a[great] < pivot2
458 // Sort center part recursively
459 sort(a, less, great, false);
461 } else { // Partitioning with one pivot
463 * Use the third of the five sorted elements as pivot.
464 * This value is inexpensive approximation of the median.
469 * Partitioning degenerates to the traditional 3-way
470 * (or "Dutch National Flag") schema:
472 * left part center part right part
473 * +-------------------------------------------------+
474 * | < pivot | == pivot | ? | > pivot |
475 * +-------------------------------------------------+
482 * all in (left, less) < pivot
483 * all in [less, k) == pivot
484 * all in (great, right) > pivot
486 * Pointer k is the first index of ?-part.
488 for (int k = less; k <= great; ++k) {
493 if (ak < pivot) { // Move a[k] to left part
497 } else { // a[k] > pivot - Move a[k] to right part
498 while (a[great] > pivot) {
501 if (a[great] < pivot) { // a[great] <= pivot
505 } else { // a[great] == pivot
507 * Even though a[great] equals to pivot, the
508 * assignment a[k] = pivot may be incorrect,
509 * if a[great] and pivot are floating-point
510 * zeros of different signs. Therefore in float
511 * and double sorting methods we have to use
512 * more accurate assignment a[k] = a[great].
522 * Sort left and right parts recursively.
523 * All elements from center part are equal
524 * and, therefore, already sorted.
526 sort(a, left, less - 1, leftmost);
527 sort(a, great + 1, right, false);
532 * Sorts the specified array.
534 * @param a the array to be sorted
536 public static void sort(long[] a) {
537 sort(a, 0, a.length - 1);
541 * Sorts the specified range of the array.
543 * @param a the array to be sorted
544 * @param left the index of the first element, inclusive, to be sorted
545 * @param right the index of the last element, inclusive, to be sorted
547 public static void sort(long[] a, int left, int right) {
548 // Use Quicksort on small arrays
549 if (right - left < QUICKSORT_THRESHOLD) {
550 sort(a, left, right, true);
555 * Index run[i] is the start of i-th run
556 * (ascending or descending sequence).
558 int[] run = new int[MAX_RUN_COUNT + 1];
559 int count = 0; run[0] = left;
561 // Check if the array is nearly sorted
562 for (int k = left; k < right; run[count] = k) {
563 if (a[k] < a[k + 1]) { // ascending
564 while (++k <= right && a[k - 1] <= a[k]);
565 } else if (a[k] > a[k + 1]) { // descending
566 while (++k <= right && a[k - 1] >= a[k]);
567 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
568 long t = a[lo]; a[lo] = a[hi]; a[hi] = t;
571 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
573 sort(a, left, right, true);
580 * The array is not highly structured,
581 * use Quicksort instead of merge sort.
583 if (++count == MAX_RUN_COUNT) {
584 sort(a, left, right, true);
589 // Check special cases
590 if (run[count] == right++) { // The last run contains one element
591 run[++count] = right;
592 } else if (count == 1) { // The array is already sorted
597 * Create temporary array, which is used for merging.
598 * Implementation note: variable "right" is increased by 1.
600 long[] b; byte odd = 0;
601 for (int n = 1; (n <<= 1) < count; odd ^= 1);
604 b = a; a = new long[b.length];
605 for (int i = left - 1; ++i < right; a[i] = b[i]);
607 b = new long[a.length];
611 for (int last; count > 1; count = last) {
612 for (int k = (last = 0) + 2; k <= count; k += 2) {
613 int hi = run[k], mi = run[k - 1];
614 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
615 if (q >= hi || p < mi && a[p] <= a[q]) {
623 if ((count & 1) != 0) {
624 for (int i = right, lo = run[count - 1]; --i >= lo;
629 long[] t = a; a = b; b = t;
634 * Sorts the specified range of the array by Dual-Pivot Quicksort.
636 * @param a the array to be sorted
637 * @param left the index of the first element, inclusive, to be sorted
638 * @param right the index of the last element, inclusive, to be sorted
639 * @param leftmost indicates if this part is the leftmost in the range
641 private static void sort(long[] a, int left, int right, boolean leftmost) {
642 int length = right - left + 1;
644 // Use insertion sort on tiny arrays
645 if (length < INSERTION_SORT_THRESHOLD) {
648 * Traditional (without sentinel) insertion sort,
649 * optimized for server VM, is used in case of
652 for (int i = left, j = i; i < right; j = ++i) {
664 * Skip the longest ascending sequence.
670 } while (a[++left] >= a[left - 1]);
673 * Every element from adjoining part plays the role
674 * of sentinel, therefore this allows us to avoid the
675 * left range check on each iteration. Moreover, we use
676 * the more optimized algorithm, so called pair insertion
677 * sort, which is faster (in the context of Quicksort)
678 * than traditional implementation of insertion sort.
680 for (int k = left; ++left <= right; k = ++left) {
681 long a1 = a[k], a2 = a[left];
684 a2 = a1; a1 = a[left];
686 while (a1 < a[--k]) {
691 while (a2 < a[--k]) {
696 long last = a[right];
698 while (last < a[--right]) {
699 a[right + 1] = a[right];
706 // Inexpensive approximation of length / 7
707 int seventh = (length >> 3) + (length >> 6) + 1;
710 * Sort five evenly spaced elements around (and including) the
711 * center element in the range. These elements will be used for
712 * pivot selection as described below. The choice for spacing
713 * these elements was empirically determined to work well on
714 * a wide variety of inputs.
716 int e3 = (left + right) >>> 1; // The midpoint
717 int e2 = e3 - seventh;
718 int e1 = e2 - seventh;
719 int e4 = e3 + seventh;
720 int e5 = e4 + seventh;
722 // Sort these elements using insertion sort
723 if (a[e2] < a[e1]) { long t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
725 if (a[e3] < a[e2]) { long t = a[e3]; a[e3] = a[e2]; a[e2] = t;
726 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
728 if (a[e4] < a[e3]) { long t = a[e4]; a[e4] = a[e3]; a[e3] = t;
729 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
730 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
733 if (a[e5] < a[e4]) { long t = a[e5]; a[e5] = a[e4]; a[e4] = t;
734 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
735 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
736 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
742 int less = left; // The index of the first element of center part
743 int great = right; // The index before the first element of right part
745 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
747 * Use the second and fourth of the five sorted elements as pivots.
748 * These values are inexpensive approximations of the first and
749 * second terciles of the array. Note that pivot1 <= pivot2.
755 * The first and the last elements to be sorted are moved to the
756 * locations formerly occupied by the pivots. When partitioning
757 * is complete, the pivots are swapped back into their final
758 * positions, and excluded from subsequent sorting.
764 * Skip elements, which are less or greater than pivot values.
766 while (a[++less] < pivot1);
767 while (a[--great] > pivot2);
772 * left part center part right part
773 * +--------------------------------------------------------------+
774 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
775 * +--------------------------------------------------------------+
782 * all in (left, less) < pivot1
783 * pivot1 <= all in [less, k) <= pivot2
784 * all in (great, right) > pivot2
786 * Pointer k is the first index of ?-part.
789 for (int k = less - 1; ++k <= great; ) {
791 if (ak < pivot1) { // Move a[k] to left part
794 * Here and below we use "a[i] = b; i++;" instead
795 * of "a[i++] = b;" due to performance issue.
799 } else if (ak > pivot2) { // Move a[k] to right part
800 while (a[great] > pivot2) {
805 if (a[great] < pivot1) { // a[great] <= pivot2
809 } else { // pivot1 <= a[great] <= pivot2
813 * Here and below we use "a[i] = b; i--;" instead
814 * of "a[i--] = b;" due to performance issue.
821 // Swap pivots into their final positions
822 a[left] = a[less - 1]; a[less - 1] = pivot1;
823 a[right] = a[great + 1]; a[great + 1] = pivot2;
825 // Sort left and right parts recursively, excluding known pivots
826 sort(a, left, less - 2, leftmost);
827 sort(a, great + 2, right, false);
830 * If center part is too large (comprises > 4/7 of the array),
831 * swap internal pivot values to ends.
833 if (less < e1 && e5 < great) {
835 * Skip elements, which are equal to pivot values.
837 while (a[less] == pivot1) {
841 while (a[great] == pivot2) {
848 * left part center part right part
849 * +----------------------------------------------------------+
850 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
851 * +----------------------------------------------------------+
858 * all in (*, less) == pivot1
859 * pivot1 < all in [less, k) < pivot2
860 * all in (great, *) == pivot2
862 * Pointer k is the first index of ?-part.
865 for (int k = less - 1; ++k <= great; ) {
867 if (ak == pivot1) { // Move a[k] to left part
871 } else if (ak == pivot2) { // Move a[k] to right part
872 while (a[great] == pivot2) {
877 if (a[great] == pivot1) { // a[great] < pivot2
880 * Even though a[great] equals to pivot1, the
881 * assignment a[less] = pivot1 may be incorrect,
882 * if a[great] and pivot1 are floating-point zeros
883 * of different signs. Therefore in float and
884 * double sorting methods we have to use more
885 * accurate assignment a[less] = a[great].
889 } else { // pivot1 < a[great] < pivot2
898 // Sort center part recursively
899 sort(a, less, great, false);
901 } else { // Partitioning with one pivot
903 * Use the third of the five sorted elements as pivot.
904 * This value is inexpensive approximation of the median.
909 * Partitioning degenerates to the traditional 3-way
910 * (or "Dutch National Flag") schema:
912 * left part center part right part
913 * +-------------------------------------------------+
914 * | < pivot | == pivot | ? | > pivot |
915 * +-------------------------------------------------+
922 * all in (left, less) < pivot
923 * all in [less, k) == pivot
924 * all in (great, right) > pivot
926 * Pointer k is the first index of ?-part.
928 for (int k = less; k <= great; ++k) {
933 if (ak < pivot) { // Move a[k] to left part
937 } else { // a[k] > pivot - Move a[k] to right part
938 while (a[great] > pivot) {
941 if (a[great] < pivot) { // a[great] <= pivot
945 } else { // a[great] == pivot
947 * Even though a[great] equals to pivot, the
948 * assignment a[k] = pivot may be incorrect,
949 * if a[great] and pivot are floating-point
950 * zeros of different signs. Therefore in float
951 * and double sorting methods we have to use
952 * more accurate assignment a[k] = a[great].
962 * Sort left and right parts recursively.
963 * All elements from center part are equal
964 * and, therefore, already sorted.
966 sort(a, left, less - 1, leftmost);
967 sort(a, great + 1, right, false);
972 * Sorts the specified array.
974 * @param a the array to be sorted
976 public static void sort(short[] a) {
977 sort(a, 0, a.length - 1);
981 * Sorts the specified range of the array.
983 * @param a the array to be sorted
984 * @param left the index of the first element, inclusive, to be sorted
985 * @param right the index of the last element, inclusive, to be sorted
987 public static void sort(short[] a, int left, int right) {
988 // Use counting sort on large arrays
989 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
990 int[] count = new int[NUM_SHORT_VALUES];
992 for (int i = left - 1; ++i <= right;
993 count[a[i] - Short.MIN_VALUE]++
995 for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
996 while (count[--i] == 0);
997 short value = (short) (i + Short.MIN_VALUE);
1004 } else { // Use Dual-Pivot Quicksort on small arrays
1005 doSort(a, left, right);
1009 /** The number of distinct short values. */
1010 private static final int NUM_SHORT_VALUES = 1 << 16;
1013 * Sorts the specified range of the array.
1015 * @param a the array to be sorted
1016 * @param left the index of the first element, inclusive, to be sorted
1017 * @param right the index of the last element, inclusive, to be sorted
1019 private static void doSort(short[] a, int left, int right) {
1020 // Use Quicksort on small arrays
1021 if (right - left < QUICKSORT_THRESHOLD) {
1022 sort(a, left, right, true);
1027 * Index run[i] is the start of i-th run
1028 * (ascending or descending sequence).
1030 int[] run = new int[MAX_RUN_COUNT + 1];
1031 int count = 0; run[0] = left;
1033 // Check if the array is nearly sorted
1034 for (int k = left; k < right; run[count] = k) {
1035 if (a[k] < a[k + 1]) { // ascending
1036 while (++k <= right && a[k - 1] <= a[k]);
1037 } else if (a[k] > a[k + 1]) { // descending
1038 while (++k <= right && a[k - 1] >= a[k]);
1039 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
1040 short t = a[lo]; a[lo] = a[hi]; a[hi] = t;
1043 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
1045 sort(a, left, right, true);
1052 * The array is not highly structured,
1053 * use Quicksort instead of merge sort.
1055 if (++count == MAX_RUN_COUNT) {
1056 sort(a, left, right, true);
1061 // Check special cases
1062 if (run[count] == right++) { // The last run contains one element
1063 run[++count] = right;
1064 } else if (count == 1) { // The array is already sorted
1069 * Create temporary array, which is used for merging.
1070 * Implementation note: variable "right" is increased by 1.
1072 short[] b; byte odd = 0;
1073 for (int n = 1; (n <<= 1) < count; odd ^= 1);
1076 b = a; a = new short[b.length];
1077 for (int i = left - 1; ++i < right; a[i] = b[i]);
1079 b = new short[a.length];
1083 for (int last; count > 1; count = last) {
1084 for (int k = (last = 0) + 2; k <= count; k += 2) {
1085 int hi = run[k], mi = run[k - 1];
1086 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
1087 if (q >= hi || p < mi && a[p] <= a[q]) {
1095 if ((count & 1) != 0) {
1096 for (int i = right, lo = run[count - 1]; --i >= lo;
1099 run[++last] = right;
1101 short[] t = a; a = b; b = t;
1106 * Sorts the specified range of the array by Dual-Pivot Quicksort.
1108 * @param a the array to be sorted
1109 * @param left the index of the first element, inclusive, to be sorted
1110 * @param right the index of the last element, inclusive, to be sorted
1111 * @param leftmost indicates if this part is the leftmost in the range
1113 private static void sort(short[] a, int left, int right, boolean leftmost) {
1114 int length = right - left + 1;
1116 // Use insertion sort on tiny arrays
1117 if (length < INSERTION_SORT_THRESHOLD) {
1120 * Traditional (without sentinel) insertion sort,
1121 * optimized for server VM, is used in case of
1122 * the leftmost part.
1124 for (int i = left, j = i; i < right; j = ++i) {
1125 short ai = a[i + 1];
1136 * Skip the longest ascending sequence.
1139 if (left >= right) {
1142 } while (a[++left] >= a[left - 1]);
1145 * Every element from adjoining part plays the role
1146 * of sentinel, therefore this allows us to avoid the
1147 * left range check on each iteration. Moreover, we use
1148 * the more optimized algorithm, so called pair insertion
1149 * sort, which is faster (in the context of Quicksort)
1150 * than traditional implementation of insertion sort.
1152 for (int k = left; ++left <= right; k = ++left) {
1153 short a1 = a[k], a2 = a[left];
1156 a2 = a1; a1 = a[left];
1158 while (a1 < a[--k]) {
1163 while (a2 < a[--k]) {
1168 short last = a[right];
1170 while (last < a[--right]) {
1171 a[right + 1] = a[right];
1173 a[right + 1] = last;
1178 // Inexpensive approximation of length / 7
1179 int seventh = (length >> 3) + (length >> 6) + 1;
1182 * Sort five evenly spaced elements around (and including) the
1183 * center element in the range. These elements will be used for
1184 * pivot selection as described below. The choice for spacing
1185 * these elements was empirically determined to work well on
1186 * a wide variety of inputs.
1188 int e3 = (left + right) >>> 1; // The midpoint
1189 int e2 = e3 - seventh;
1190 int e1 = e2 - seventh;
1191 int e4 = e3 + seventh;
1192 int e5 = e4 + seventh;
1194 // Sort these elements using insertion sort
1195 if (a[e2] < a[e1]) { short t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
1197 if (a[e3] < a[e2]) { short t = a[e3]; a[e3] = a[e2]; a[e2] = t;
1198 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1200 if (a[e4] < a[e3]) { short t = a[e4]; a[e4] = a[e3]; a[e3] = t;
1201 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
1202 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1205 if (a[e5] < a[e4]) { short t = a[e5]; a[e5] = a[e4]; a[e4] = t;
1206 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
1207 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
1208 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1214 int less = left; // The index of the first element of center part
1215 int great = right; // The index before the first element of right part
1217 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
1219 * Use the second and fourth of the five sorted elements as pivots.
1220 * These values are inexpensive approximations of the first and
1221 * second terciles of the array. Note that pivot1 <= pivot2.
1223 short pivot1 = a[e2];
1224 short pivot2 = a[e4];
1227 * The first and the last elements to be sorted are moved to the
1228 * locations formerly occupied by the pivots. When partitioning
1229 * is complete, the pivots are swapped back into their final
1230 * positions, and excluded from subsequent sorting.
1236 * Skip elements, which are less or greater than pivot values.
1238 while (a[++less] < pivot1);
1239 while (a[--great] > pivot2);
1244 * left part center part right part
1245 * +--------------------------------------------------------------+
1246 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
1247 * +--------------------------------------------------------------+
1254 * all in (left, less) < pivot1
1255 * pivot1 <= all in [less, k) <= pivot2
1256 * all in (great, right) > pivot2
1258 * Pointer k is the first index of ?-part.
1261 for (int k = less - 1; ++k <= great; ) {
1263 if (ak < pivot1) { // Move a[k] to left part
1266 * Here and below we use "a[i] = b; i++;" instead
1267 * of "a[i++] = b;" due to performance issue.
1271 } else if (ak > pivot2) { // Move a[k] to right part
1272 while (a[great] > pivot2) {
1277 if (a[great] < pivot1) { // a[great] <= pivot2
1281 } else { // pivot1 <= a[great] <= pivot2
1285 * Here and below we use "a[i] = b; i--;" instead
1286 * of "a[i--] = b;" due to performance issue.
1293 // Swap pivots into their final positions
1294 a[left] = a[less - 1]; a[less - 1] = pivot1;
1295 a[right] = a[great + 1]; a[great + 1] = pivot2;
1297 // Sort left and right parts recursively, excluding known pivots
1298 sort(a, left, less - 2, leftmost);
1299 sort(a, great + 2, right, false);
1302 * If center part is too large (comprises > 4/7 of the array),
1303 * swap internal pivot values to ends.
1305 if (less < e1 && e5 < great) {
1307 * Skip elements, which are equal to pivot values.
1309 while (a[less] == pivot1) {
1313 while (a[great] == pivot2) {
1320 * left part center part right part
1321 * +----------------------------------------------------------+
1322 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
1323 * +----------------------------------------------------------+
1330 * all in (*, less) == pivot1
1331 * pivot1 < all in [less, k) < pivot2
1332 * all in (great, *) == pivot2
1334 * Pointer k is the first index of ?-part.
1337 for (int k = less - 1; ++k <= great; ) {
1339 if (ak == pivot1) { // Move a[k] to left part
1343 } else if (ak == pivot2) { // Move a[k] to right part
1344 while (a[great] == pivot2) {
1349 if (a[great] == pivot1) { // a[great] < pivot2
1352 * Even though a[great] equals to pivot1, the
1353 * assignment a[less] = pivot1 may be incorrect,
1354 * if a[great] and pivot1 are floating-point zeros
1355 * of different signs. Therefore in float and
1356 * double sorting methods we have to use more
1357 * accurate assignment a[less] = a[great].
1361 } else { // pivot1 < a[great] < pivot2
1370 // Sort center part recursively
1371 sort(a, less, great, false);
1373 } else { // Partitioning with one pivot
1375 * Use the third of the five sorted elements as pivot.
1376 * This value is inexpensive approximation of the median.
1378 short pivot = a[e3];
1381 * Partitioning degenerates to the traditional 3-way
1382 * (or "Dutch National Flag") schema:
1384 * left part center part right part
1385 * +-------------------------------------------------+
1386 * | < pivot | == pivot | ? | > pivot |
1387 * +-------------------------------------------------+
1394 * all in (left, less) < pivot
1395 * all in [less, k) == pivot
1396 * all in (great, right) > pivot
1398 * Pointer k is the first index of ?-part.
1400 for (int k = less; k <= great; ++k) {
1401 if (a[k] == pivot) {
1405 if (ak < pivot) { // Move a[k] to left part
1409 } else { // a[k] > pivot - Move a[k] to right part
1410 while (a[great] > pivot) {
1413 if (a[great] < pivot) { // a[great] <= pivot
1417 } else { // a[great] == pivot
1419 * Even though a[great] equals to pivot, the
1420 * assignment a[k] = pivot may be incorrect,
1421 * if a[great] and pivot are floating-point
1422 * zeros of different signs. Therefore in float
1423 * and double sorting methods we have to use
1424 * more accurate assignment a[k] = a[great].
1434 * Sort left and right parts recursively.
1435 * All elements from center part are equal
1436 * and, therefore, already sorted.
1438 sort(a, left, less - 1, leftmost);
1439 sort(a, great + 1, right, false);
1444 * Sorts the specified array.
1446 * @param a the array to be sorted
1448 public static void sort(char[] a) {
1449 sort(a, 0, a.length - 1);
1453 * Sorts the specified range of the array.
1455 * @param a the array to be sorted
1456 * @param left the index of the first element, inclusive, to be sorted
1457 * @param right the index of the last element, inclusive, to be sorted
1459 public static void sort(char[] a, int left, int right) {
1460 // Use counting sort on large arrays
1461 if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
1462 int[] count = new int[NUM_CHAR_VALUES];
1464 for (int i = left - 1; ++i <= right;
1467 for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
1468 while (count[--i] == 0);
1469 char value = (char) i;
1476 } else { // Use Dual-Pivot Quicksort on small arrays
1477 doSort(a, left, right);
1481 /** The number of distinct char values. */
1482 private static final int NUM_CHAR_VALUES = 1 << 16;
1485 * Sorts the specified range of the array.
1487 * @param a the array to be sorted
1488 * @param left the index of the first element, inclusive, to be sorted
1489 * @param right the index of the last element, inclusive, to be sorted
1491 private static void doSort(char[] a, int left, int right) {
1492 // Use Quicksort on small arrays
1493 if (right - left < QUICKSORT_THRESHOLD) {
1494 sort(a, left, right, true);
1499 * Index run[i] is the start of i-th run
1500 * (ascending or descending sequence).
1502 int[] run = new int[MAX_RUN_COUNT + 1];
1503 int count = 0; run[0] = left;
1505 // Check if the array is nearly sorted
1506 for (int k = left; k < right; run[count] = k) {
1507 if (a[k] < a[k + 1]) { // ascending
1508 while (++k <= right && a[k - 1] <= a[k]);
1509 } else if (a[k] > a[k + 1]) { // descending
1510 while (++k <= right && a[k - 1] >= a[k]);
1511 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
1512 char t = a[lo]; a[lo] = a[hi]; a[hi] = t;
1515 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
1517 sort(a, left, right, true);
1524 * The array is not highly structured,
1525 * use Quicksort instead of merge sort.
1527 if (++count == MAX_RUN_COUNT) {
1528 sort(a, left, right, true);
1533 // Check special cases
1534 if (run[count] == right++) { // The last run contains one element
1535 run[++count] = right;
1536 } else if (count == 1) { // The array is already sorted
1541 * Create temporary array, which is used for merging.
1542 * Implementation note: variable "right" is increased by 1.
1544 char[] b; byte odd = 0;
1545 for (int n = 1; (n <<= 1) < count; odd ^= 1);
1548 b = a; a = new char[b.length];
1549 for (int i = left - 1; ++i < right; a[i] = b[i]);
1551 b = new char[a.length];
1555 for (int last; count > 1; count = last) {
1556 for (int k = (last = 0) + 2; k <= count; k += 2) {
1557 int hi = run[k], mi = run[k - 1];
1558 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
1559 if (q >= hi || p < mi && a[p] <= a[q]) {
1567 if ((count & 1) != 0) {
1568 for (int i = right, lo = run[count - 1]; --i >= lo;
1571 run[++last] = right;
1573 char[] t = a; a = b; b = t;
1578 * Sorts the specified range of the array by Dual-Pivot Quicksort.
1580 * @param a the array to be sorted
1581 * @param left the index of the first element, inclusive, to be sorted
1582 * @param right the index of the last element, inclusive, to be sorted
1583 * @param leftmost indicates if this part is the leftmost in the range
1585 private static void sort(char[] a, int left, int right, boolean leftmost) {
1586 int length = right - left + 1;
1588 // Use insertion sort on tiny arrays
1589 if (length < INSERTION_SORT_THRESHOLD) {
1592 * Traditional (without sentinel) insertion sort,
1593 * optimized for server VM, is used in case of
1594 * the leftmost part.
1596 for (int i = left, j = i; i < right; j = ++i) {
1608 * Skip the longest ascending sequence.
1611 if (left >= right) {
1614 } while (a[++left] >= a[left - 1]);
1617 * Every element from adjoining part plays the role
1618 * of sentinel, therefore this allows us to avoid the
1619 * left range check on each iteration. Moreover, we use
1620 * the more optimized algorithm, so called pair insertion
1621 * sort, which is faster (in the context of Quicksort)
1622 * than traditional implementation of insertion sort.
1624 for (int k = left; ++left <= right; k = ++left) {
1625 char a1 = a[k], a2 = a[left];
1628 a2 = a1; a1 = a[left];
1630 while (a1 < a[--k]) {
1635 while (a2 < a[--k]) {
1640 char last = a[right];
1642 while (last < a[--right]) {
1643 a[right + 1] = a[right];
1645 a[right + 1] = last;
1650 // Inexpensive approximation of length / 7
1651 int seventh = (length >> 3) + (length >> 6) + 1;
1654 * Sort five evenly spaced elements around (and including) the
1655 * center element in the range. These elements will be used for
1656 * pivot selection as described below. The choice for spacing
1657 * these elements was empirically determined to work well on
1658 * a wide variety of inputs.
1660 int e3 = (left + right) >>> 1; // The midpoint
1661 int e2 = e3 - seventh;
1662 int e1 = e2 - seventh;
1663 int e4 = e3 + seventh;
1664 int e5 = e4 + seventh;
1666 // Sort these elements using insertion sort
1667 if (a[e2] < a[e1]) { char t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
1669 if (a[e3] < a[e2]) { char t = a[e3]; a[e3] = a[e2]; a[e2] = t;
1670 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1672 if (a[e4] < a[e3]) { char t = a[e4]; a[e4] = a[e3]; a[e3] = t;
1673 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
1674 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1677 if (a[e5] < a[e4]) { char t = a[e5]; a[e5] = a[e4]; a[e4] = t;
1678 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
1679 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
1680 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
1686 int less = left; // The index of the first element of center part
1687 int great = right; // The index before the first element of right part
1689 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
1691 * Use the second and fourth of the five sorted elements as pivots.
1692 * These values are inexpensive approximations of the first and
1693 * second terciles of the array. Note that pivot1 <= pivot2.
1695 char pivot1 = a[e2];
1696 char pivot2 = a[e4];
1699 * The first and the last elements to be sorted are moved to the
1700 * locations formerly occupied by the pivots. When partitioning
1701 * is complete, the pivots are swapped back into their final
1702 * positions, and excluded from subsequent sorting.
1708 * Skip elements, which are less or greater than pivot values.
1710 while (a[++less] < pivot1);
1711 while (a[--great] > pivot2);
1716 * left part center part right part
1717 * +--------------------------------------------------------------+
1718 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
1719 * +--------------------------------------------------------------+
1726 * all in (left, less) < pivot1
1727 * pivot1 <= all in [less, k) <= pivot2
1728 * all in (great, right) > pivot2
1730 * Pointer k is the first index of ?-part.
1733 for (int k = less - 1; ++k <= great; ) {
1735 if (ak < pivot1) { // Move a[k] to left part
1738 * Here and below we use "a[i] = b; i++;" instead
1739 * of "a[i++] = b;" due to performance issue.
1743 } else if (ak > pivot2) { // Move a[k] to right part
1744 while (a[great] > pivot2) {
1749 if (a[great] < pivot1) { // a[great] <= pivot2
1753 } else { // pivot1 <= a[great] <= pivot2
1757 * Here and below we use "a[i] = b; i--;" instead
1758 * of "a[i--] = b;" due to performance issue.
1765 // Swap pivots into their final positions
1766 a[left] = a[less - 1]; a[less - 1] = pivot1;
1767 a[right] = a[great + 1]; a[great + 1] = pivot2;
1769 // Sort left and right parts recursively, excluding known pivots
1770 sort(a, left, less - 2, leftmost);
1771 sort(a, great + 2, right, false);
1774 * If center part is too large (comprises > 4/7 of the array),
1775 * swap internal pivot values to ends.
1777 if (less < e1 && e5 < great) {
1779 * Skip elements, which are equal to pivot values.
1781 while (a[less] == pivot1) {
1785 while (a[great] == pivot2) {
1792 * left part center part right part
1793 * +----------------------------------------------------------+
1794 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
1795 * +----------------------------------------------------------+
1802 * all in (*, less) == pivot1
1803 * pivot1 < all in [less, k) < pivot2
1804 * all in (great, *) == pivot2
1806 * Pointer k is the first index of ?-part.
1809 for (int k = less - 1; ++k <= great; ) {
1811 if (ak == pivot1) { // Move a[k] to left part
1815 } else if (ak == pivot2) { // Move a[k] to right part
1816 while (a[great] == pivot2) {
1821 if (a[great] == pivot1) { // a[great] < pivot2
1824 * Even though a[great] equals to pivot1, the
1825 * assignment a[less] = pivot1 may be incorrect,
1826 * if a[great] and pivot1 are floating-point zeros
1827 * of different signs. Therefore in float and
1828 * double sorting methods we have to use more
1829 * accurate assignment a[less] = a[great].
1833 } else { // pivot1 < a[great] < pivot2
1842 // Sort center part recursively
1843 sort(a, less, great, false);
1845 } else { // Partitioning with one pivot
1847 * Use the third of the five sorted elements as pivot.
1848 * This value is inexpensive approximation of the median.
1853 * Partitioning degenerates to the traditional 3-way
1854 * (or "Dutch National Flag") schema:
1856 * left part center part right part
1857 * +-------------------------------------------------+
1858 * | < pivot | == pivot | ? | > pivot |
1859 * +-------------------------------------------------+
1866 * all in (left, less) < pivot
1867 * all in [less, k) == pivot
1868 * all in (great, right) > pivot
1870 * Pointer k is the first index of ?-part.
1872 for (int k = less; k <= great; ++k) {
1873 if (a[k] == pivot) {
1877 if (ak < pivot) { // Move a[k] to left part
1881 } else { // a[k] > pivot - Move a[k] to right part
1882 while (a[great] > pivot) {
1885 if (a[great] < pivot) { // a[great] <= pivot
1889 } else { // a[great] == pivot
1891 * Even though a[great] equals to pivot, the
1892 * assignment a[k] = pivot may be incorrect,
1893 * if a[great] and pivot are floating-point
1894 * zeros of different signs. Therefore in float
1895 * and double sorting methods we have to use
1896 * more accurate assignment a[k] = a[great].
1906 * Sort left and right parts recursively.
1907 * All elements from center part are equal
1908 * and, therefore, already sorted.
1910 sort(a, left, less - 1, leftmost);
1911 sort(a, great + 1, right, false);
1915 /** The number of distinct byte values. */
1916 private static final int NUM_BYTE_VALUES = 1 << 8;
1919 * Sorts the specified array.
1921 * @param a the array to be sorted
1923 public static void sort(byte[] a) {
1924 sort(a, 0, a.length - 1);
1928 * Sorts the specified range of the array.
1930 * @param a the array to be sorted
1931 * @param left the index of the first element, inclusive, to be sorted
1932 * @param right the index of the last element, inclusive, to be sorted
1934 public static void sort(byte[] a, int left, int right) {
1935 // Use counting sort on large arrays
1936 if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
1937 int[] count = new int[NUM_BYTE_VALUES];
1939 for (int i = left - 1; ++i <= right;
1940 count[a[i] - Byte.MIN_VALUE]++
1942 for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
1943 while (count[--i] == 0);
1944 byte value = (byte) (i + Byte.MIN_VALUE);
1951 } else { // Use insertion sort on small arrays
1952 for (int i = left, j = i; i < right; j = ++i) {
1966 * Sorts the specified array.
1968 * @param a the array to be sorted
1970 public static void sort(float[] a) {
1971 sort(a, 0, a.length - 1);
1975 * Sorts the specified range of the array.
1977 * @param a the array to be sorted
1978 * @param left the index of the first element, inclusive, to be sorted
1979 * @param right the index of the last element, inclusive, to be sorted
1981 public static void sort(float[] a, int left, int right) {
1983 * Phase 1: Move NaNs to the end of the array.
1985 while (left <= right && Float.isNaN(a[right])) {
1988 for (int k = right; --k >= left; ) {
1990 if (ak != ak) { // a[k] is NaN
1998 * Phase 2: Sort everything except NaNs (which are already in place).
2000 doSort(a, left, right);
2003 * Phase 3: Place negative zeros before positive zeros.
2008 * Find the first zero, or first positive, or last negative element.
2011 int middle = (left + hi) >>> 1;
2012 float middleValue = a[middle];
2014 if (middleValue < 0.0f) {
2022 * Skip the last negative value (if any) or all leading negative zeros.
2024 while (left <= right && Float.floatToRawIntBits(a[left]) < 0) {
2029 * Move negative zeros to the beginning of the sub-range.
2033 * +----------------------------------------------------+
2034 * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
2035 * +----------------------------------------------------+
2042 * all in (*, left) < 0.0
2043 * all in [left, p) == -0.0
2044 * all in [p, k) == 0.0
2045 * all in [k, right] >= 0.0
2047 * Pointer k is the first index of ?-part.
2049 for (int k = left, p = left - 1; ++k <= right; ) {
2054 if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f
2062 * Sorts the specified range of the array.
2064 * @param a the array to be sorted
2065 * @param left the index of the first element, inclusive, to be sorted
2066 * @param right the index of the last element, inclusive, to be sorted
2068 private static void doSort(float[] a, int left, int right) {
2069 // Use Quicksort on small arrays
2070 if (right - left < QUICKSORT_THRESHOLD) {
2071 sort(a, left, right, true);
2076 * Index run[i] is the start of i-th run
2077 * (ascending or descending sequence).
2079 int[] run = new int[MAX_RUN_COUNT + 1];
2080 int count = 0; run[0] = left;
2082 // Check if the array is nearly sorted
2083 for (int k = left; k < right; run[count] = k) {
2084 if (a[k] < a[k + 1]) { // ascending
2085 while (++k <= right && a[k - 1] <= a[k]);
2086 } else if (a[k] > a[k + 1]) { // descending
2087 while (++k <= right && a[k - 1] >= a[k]);
2088 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
2089 float t = a[lo]; a[lo] = a[hi]; a[hi] = t;
2092 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
2094 sort(a, left, right, true);
2101 * The array is not highly structured,
2102 * use Quicksort instead of merge sort.
2104 if (++count == MAX_RUN_COUNT) {
2105 sort(a, left, right, true);
2110 // Check special cases
2111 if (run[count] == right++) { // The last run contains one element
2112 run[++count] = right;
2113 } else if (count == 1) { // The array is already sorted
2118 * Create temporary array, which is used for merging.
2119 * Implementation note: variable "right" is increased by 1.
2121 float[] b; byte odd = 0;
2122 for (int n = 1; (n <<= 1) < count; odd ^= 1);
2125 b = a; a = new float[b.length];
2126 for (int i = left - 1; ++i < right; a[i] = b[i]);
2128 b = new float[a.length];
2132 for (int last; count > 1; count = last) {
2133 for (int k = (last = 0) + 2; k <= count; k += 2) {
2134 int hi = run[k], mi = run[k - 1];
2135 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
2136 if (q >= hi || p < mi && a[p] <= a[q]) {
2144 if ((count & 1) != 0) {
2145 for (int i = right, lo = run[count - 1]; --i >= lo;
2148 run[++last] = right;
2150 float[] t = a; a = b; b = t;
2155 * Sorts the specified range of the array by Dual-Pivot Quicksort.
2157 * @param a the array to be sorted
2158 * @param left the index of the first element, inclusive, to be sorted
2159 * @param right the index of the last element, inclusive, to be sorted
2160 * @param leftmost indicates if this part is the leftmost in the range
2162 private static void sort(float[] a, int left, int right, boolean leftmost) {
2163 int length = right - left + 1;
2165 // Use insertion sort on tiny arrays
2166 if (length < INSERTION_SORT_THRESHOLD) {
2169 * Traditional (without sentinel) insertion sort,
2170 * optimized for server VM, is used in case of
2171 * the leftmost part.
2173 for (int i = left, j = i; i < right; j = ++i) {
2174 float ai = a[i + 1];
2185 * Skip the longest ascending sequence.
2188 if (left >= right) {
2191 } while (a[++left] >= a[left - 1]);
2194 * Every element from adjoining part plays the role
2195 * of sentinel, therefore this allows us to avoid the
2196 * left range check on each iteration. Moreover, we use
2197 * the more optimized algorithm, so called pair insertion
2198 * sort, which is faster (in the context of Quicksort)
2199 * than traditional implementation of insertion sort.
2201 for (int k = left; ++left <= right; k = ++left) {
2202 float a1 = a[k], a2 = a[left];
2205 a2 = a1; a1 = a[left];
2207 while (a1 < a[--k]) {
2212 while (a2 < a[--k]) {
2217 float last = a[right];
2219 while (last < a[--right]) {
2220 a[right + 1] = a[right];
2222 a[right + 1] = last;
2227 // Inexpensive approximation of length / 7
2228 int seventh = (length >> 3) + (length >> 6) + 1;
2231 * Sort five evenly spaced elements around (and including) the
2232 * center element in the range. These elements will be used for
2233 * pivot selection as described below. The choice for spacing
2234 * these elements was empirically determined to work well on
2235 * a wide variety of inputs.
2237 int e3 = (left + right) >>> 1; // The midpoint
2238 int e2 = e3 - seventh;
2239 int e1 = e2 - seventh;
2240 int e4 = e3 + seventh;
2241 int e5 = e4 + seventh;
2243 // Sort these elements using insertion sort
2244 if (a[e2] < a[e1]) { float t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2246 if (a[e3] < a[e2]) { float t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2247 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2249 if (a[e4] < a[e3]) { float t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2250 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2251 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2254 if (a[e5] < a[e4]) { float t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2255 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2256 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2257 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2263 int less = left; // The index of the first element of center part
2264 int great = right; // The index before the first element of right part
2266 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
2268 * Use the second and fourth of the five sorted elements as pivots.
2269 * These values are inexpensive approximations of the first and
2270 * second terciles of the array. Note that pivot1 <= pivot2.
2272 float pivot1 = a[e2];
2273 float pivot2 = a[e4];
2276 * The first and the last elements to be sorted are moved to the
2277 * locations formerly occupied by the pivots. When partitioning
2278 * is complete, the pivots are swapped back into their final
2279 * positions, and excluded from subsequent sorting.
2285 * Skip elements, which are less or greater than pivot values.
2287 while (a[++less] < pivot1);
2288 while (a[--great] > pivot2);
2293 * left part center part right part
2294 * +--------------------------------------------------------------+
2295 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2296 * +--------------------------------------------------------------+
2303 * all in (left, less) < pivot1
2304 * pivot1 <= all in [less, k) <= pivot2
2305 * all in (great, right) > pivot2
2307 * Pointer k is the first index of ?-part.
2310 for (int k = less - 1; ++k <= great; ) {
2312 if (ak < pivot1) { // Move a[k] to left part
2315 * Here and below we use "a[i] = b; i++;" instead
2316 * of "a[i++] = b;" due to performance issue.
2320 } else if (ak > pivot2) { // Move a[k] to right part
2321 while (a[great] > pivot2) {
2326 if (a[great] < pivot1) { // a[great] <= pivot2
2330 } else { // pivot1 <= a[great] <= pivot2
2334 * Here and below we use "a[i] = b; i--;" instead
2335 * of "a[i--] = b;" due to performance issue.
2342 // Swap pivots into their final positions
2343 a[left] = a[less - 1]; a[less - 1] = pivot1;
2344 a[right] = a[great + 1]; a[great + 1] = pivot2;
2346 // Sort left and right parts recursively, excluding known pivots
2347 sort(a, left, less - 2, leftmost);
2348 sort(a, great + 2, right, false);
2351 * If center part is too large (comprises > 4/7 of the array),
2352 * swap internal pivot values to ends.
2354 if (less < e1 && e5 < great) {
2356 * Skip elements, which are equal to pivot values.
2358 while (a[less] == pivot1) {
2362 while (a[great] == pivot2) {
2369 * left part center part right part
2370 * +----------------------------------------------------------+
2371 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2372 * +----------------------------------------------------------+
2379 * all in (*, less) == pivot1
2380 * pivot1 < all in [less, k) < pivot2
2381 * all in (great, *) == pivot2
2383 * Pointer k is the first index of ?-part.
2386 for (int k = less - 1; ++k <= great; ) {
2388 if (ak == pivot1) { // Move a[k] to left part
2392 } else if (ak == pivot2) { // Move a[k] to right part
2393 while (a[great] == pivot2) {
2398 if (a[great] == pivot1) { // a[great] < pivot2
2401 * Even though a[great] equals to pivot1, the
2402 * assignment a[less] = pivot1 may be incorrect,
2403 * if a[great] and pivot1 are floating-point zeros
2404 * of different signs. Therefore in float and
2405 * double sorting methods we have to use more
2406 * accurate assignment a[less] = a[great].
2410 } else { // pivot1 < a[great] < pivot2
2419 // Sort center part recursively
2420 sort(a, less, great, false);
2422 } else { // Partitioning with one pivot
2424 * Use the third of the five sorted elements as pivot.
2425 * This value is inexpensive approximation of the median.
2427 float pivot = a[e3];
2430 * Partitioning degenerates to the traditional 3-way
2431 * (or "Dutch National Flag") schema:
2433 * left part center part right part
2434 * +-------------------------------------------------+
2435 * | < pivot | == pivot | ? | > pivot |
2436 * +-------------------------------------------------+
2443 * all in (left, less) < pivot
2444 * all in [less, k) == pivot
2445 * all in (great, right) > pivot
2447 * Pointer k is the first index of ?-part.
2449 for (int k = less; k <= great; ++k) {
2450 if (a[k] == pivot) {
2454 if (ak < pivot) { // Move a[k] to left part
2458 } else { // a[k] > pivot - Move a[k] to right part
2459 while (a[great] > pivot) {
2462 if (a[great] < pivot) { // a[great] <= pivot
2466 } else { // a[great] == pivot
2468 * Even though a[great] equals to pivot, the
2469 * assignment a[k] = pivot may be incorrect,
2470 * if a[great] and pivot are floating-point
2471 * zeros of different signs. Therefore in float
2472 * and double sorting methods we have to use
2473 * more accurate assignment a[k] = a[great].
2483 * Sort left and right parts recursively.
2484 * All elements from center part are equal
2485 * and, therefore, already sorted.
2487 sort(a, left, less - 1, leftmost);
2488 sort(a, great + 1, right, false);
2493 * Sorts the specified array.
2495 * @param a the array to be sorted
2497 public static void sort(double[] a) {
2498 sort(a, 0, a.length - 1);
2502 * Sorts the specified range of the array.
2504 * @param a the array to be sorted
2505 * @param left the index of the first element, inclusive, to be sorted
2506 * @param right the index of the last element, inclusive, to be sorted
2508 public static void sort(double[] a, int left, int right) {
2510 * Phase 1: Move NaNs to the end of the array.
2512 while (left <= right && Double.isNaN(a[right])) {
2515 for (int k = right; --k >= left; ) {
2517 if (ak != ak) { // a[k] is NaN
2525 * Phase 2: Sort everything except NaNs (which are already in place).
2527 doSort(a, left, right);
2530 * Phase 3: Place negative zeros before positive zeros.
2535 * Find the first zero, or first positive, or last negative element.
2538 int middle = (left + hi) >>> 1;
2539 double middleValue = a[middle];
2541 if (middleValue < 0.0d) {
2549 * Skip the last negative value (if any) or all leading negative zeros.
2551 while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) {
2556 * Move negative zeros to the beginning of the sub-range.
2560 * +----------------------------------------------------+
2561 * | < 0.0 | -0.0 | 0.0 | ? ( >= 0.0 ) |
2562 * +----------------------------------------------------+
2569 * all in (*, left) < 0.0
2570 * all in [left, p) == -0.0
2571 * all in [p, k) == 0.0
2572 * all in [k, right] >= 0.0
2574 * Pointer k is the first index of ?-part.
2576 for (int k = left, p = left - 1; ++k <= right; ) {
2581 if (Double.doubleToRawLongBits(ak) < 0) { // ak is -0.0d
2589 * Sorts the specified range of the array.
2591 * @param a the array to be sorted
2592 * @param left the index of the first element, inclusive, to be sorted
2593 * @param right the index of the last element, inclusive, to be sorted
2595 private static void doSort(double[] a, int left, int right) {
2596 // Use Quicksort on small arrays
2597 if (right - left < QUICKSORT_THRESHOLD) {
2598 sort(a, left, right, true);
2603 * Index run[i] is the start of i-th run
2604 * (ascending or descending sequence).
2606 int[] run = new int[MAX_RUN_COUNT + 1];
2607 int count = 0; run[0] = left;
2609 // Check if the array is nearly sorted
2610 for (int k = left; k < right; run[count] = k) {
2611 if (a[k] < a[k + 1]) { // ascending
2612 while (++k <= right && a[k - 1] <= a[k]);
2613 } else if (a[k] > a[k + 1]) { // descending
2614 while (++k <= right && a[k - 1] >= a[k]);
2615 for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
2616 double t = a[lo]; a[lo] = a[hi]; a[hi] = t;
2619 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
2621 sort(a, left, right, true);
2628 * The array is not highly structured,
2629 * use Quicksort instead of merge sort.
2631 if (++count == MAX_RUN_COUNT) {
2632 sort(a, left, right, true);
2637 // Check special cases
2638 if (run[count] == right++) { // The last run contains one element
2639 run[++count] = right;
2640 } else if (count == 1) { // The array is already sorted
2645 * Create temporary array, which is used for merging.
2646 * Implementation note: variable "right" is increased by 1.
2648 double[] b; byte odd = 0;
2649 for (int n = 1; (n <<= 1) < count; odd ^= 1);
2652 b = a; a = new double[b.length];
2653 for (int i = left - 1; ++i < right; a[i] = b[i]);
2655 b = new double[a.length];
2659 for (int last; count > 1; count = last) {
2660 for (int k = (last = 0) + 2; k <= count; k += 2) {
2661 int hi = run[k], mi = run[k - 1];
2662 for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
2663 if (q >= hi || p < mi && a[p] <= a[q]) {
2671 if ((count & 1) != 0) {
2672 for (int i = right, lo = run[count - 1]; --i >= lo;
2675 run[++last] = right;
2677 double[] t = a; a = b; b = t;
2682 * Sorts the specified range of the array by Dual-Pivot Quicksort.
2684 * @param a the array to be sorted
2685 * @param left the index of the first element, inclusive, to be sorted
2686 * @param right the index of the last element, inclusive, to be sorted
2687 * @param leftmost indicates if this part is the leftmost in the range
2689 private static void sort(double[] a, int left, int right, boolean leftmost) {
2690 int length = right - left + 1;
2692 // Use insertion sort on tiny arrays
2693 if (length < INSERTION_SORT_THRESHOLD) {
2696 * Traditional (without sentinel) insertion sort,
2697 * optimized for server VM, is used in case of
2698 * the leftmost part.
2700 for (int i = left, j = i; i < right; j = ++i) {
2701 double ai = a[i + 1];
2712 * Skip the longest ascending sequence.
2715 if (left >= right) {
2718 } while (a[++left] >= a[left - 1]);
2721 * Every element from adjoining part plays the role
2722 * of sentinel, therefore this allows us to avoid the
2723 * left range check on each iteration. Moreover, we use
2724 * the more optimized algorithm, so called pair insertion
2725 * sort, which is faster (in the context of Quicksort)
2726 * than traditional implementation of insertion sort.
2728 for (int k = left; ++left <= right; k = ++left) {
2729 double a1 = a[k], a2 = a[left];
2732 a2 = a1; a1 = a[left];
2734 while (a1 < a[--k]) {
2739 while (a2 < a[--k]) {
2744 double last = a[right];
2746 while (last < a[--right]) {
2747 a[right + 1] = a[right];
2749 a[right + 1] = last;
2754 // Inexpensive approximation of length / 7
2755 int seventh = (length >> 3) + (length >> 6) + 1;
2758 * Sort five evenly spaced elements around (and including) the
2759 * center element in the range. These elements will be used for
2760 * pivot selection as described below. The choice for spacing
2761 * these elements was empirically determined to work well on
2762 * a wide variety of inputs.
2764 int e3 = (left + right) >>> 1; // The midpoint
2765 int e2 = e3 - seventh;
2766 int e1 = e2 - seventh;
2767 int e4 = e3 + seventh;
2768 int e5 = e4 + seventh;
2770 // Sort these elements using insertion sort
2771 if (a[e2] < a[e1]) { double t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
2773 if (a[e3] < a[e2]) { double t = a[e3]; a[e3] = a[e2]; a[e2] = t;
2774 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2776 if (a[e4] < a[e3]) { double t = a[e4]; a[e4] = a[e3]; a[e3] = t;
2777 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2778 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2781 if (a[e5] < a[e4]) { double t = a[e5]; a[e5] = a[e4]; a[e4] = t;
2782 if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
2783 if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
2784 if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
2790 int less = left; // The index of the first element of center part
2791 int great = right; // The index before the first element of right part
2793 if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
2795 * Use the second and fourth of the five sorted elements as pivots.
2796 * These values are inexpensive approximations of the first and
2797 * second terciles of the array. Note that pivot1 <= pivot2.
2799 double pivot1 = a[e2];
2800 double pivot2 = a[e4];
2803 * The first and the last elements to be sorted are moved to the
2804 * locations formerly occupied by the pivots. When partitioning
2805 * is complete, the pivots are swapped back into their final
2806 * positions, and excluded from subsequent sorting.
2812 * Skip elements, which are less or greater than pivot values.
2814 while (a[++less] < pivot1);
2815 while (a[--great] > pivot2);
2820 * left part center part right part
2821 * +--------------------------------------------------------------+
2822 * | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
2823 * +--------------------------------------------------------------+
2830 * all in (left, less) < pivot1
2831 * pivot1 <= all in [less, k) <= pivot2
2832 * all in (great, right) > pivot2
2834 * Pointer k is the first index of ?-part.
2837 for (int k = less - 1; ++k <= great; ) {
2839 if (ak < pivot1) { // Move a[k] to left part
2842 * Here and below we use "a[i] = b; i++;" instead
2843 * of "a[i++] = b;" due to performance issue.
2847 } else if (ak > pivot2) { // Move a[k] to right part
2848 while (a[great] > pivot2) {
2853 if (a[great] < pivot1) { // a[great] <= pivot2
2857 } else { // pivot1 <= a[great] <= pivot2
2861 * Here and below we use "a[i] = b; i--;" instead
2862 * of "a[i--] = b;" due to performance issue.
2869 // Swap pivots into their final positions
2870 a[left] = a[less - 1]; a[less - 1] = pivot1;
2871 a[right] = a[great + 1]; a[great + 1] = pivot2;
2873 // Sort left and right parts recursively, excluding known pivots
2874 sort(a, left, less - 2, leftmost);
2875 sort(a, great + 2, right, false);
2878 * If center part is too large (comprises > 4/7 of the array),
2879 * swap internal pivot values to ends.
2881 if (less < e1 && e5 < great) {
2883 * Skip elements, which are equal to pivot values.
2885 while (a[less] == pivot1) {
2889 while (a[great] == pivot2) {
2896 * left part center part right part
2897 * +----------------------------------------------------------+
2898 * | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
2899 * +----------------------------------------------------------+
2906 * all in (*, less) == pivot1
2907 * pivot1 < all in [less, k) < pivot2
2908 * all in (great, *) == pivot2
2910 * Pointer k is the first index of ?-part.
2913 for (int k = less - 1; ++k <= great; ) {
2915 if (ak == pivot1) { // Move a[k] to left part
2919 } else if (ak == pivot2) { // Move a[k] to right part
2920 while (a[great] == pivot2) {
2925 if (a[great] == pivot1) { // a[great] < pivot2
2928 * Even though a[great] equals to pivot1, the
2929 * assignment a[less] = pivot1 may be incorrect,
2930 * if a[great] and pivot1 are floating-point zeros
2931 * of different signs. Therefore in float and
2932 * double sorting methods we have to use more
2933 * accurate assignment a[less] = a[great].
2937 } else { // pivot1 < a[great] < pivot2
2946 // Sort center part recursively
2947 sort(a, less, great, false);
2949 } else { // Partitioning with one pivot
2951 * Use the third of the five sorted elements as pivot.
2952 * This value is inexpensive approximation of the median.
2954 double pivot = a[e3];
2957 * Partitioning degenerates to the traditional 3-way
2958 * (or "Dutch National Flag") schema:
2960 * left part center part right part
2961 * +-------------------------------------------------+
2962 * | < pivot | == pivot | ? | > pivot |
2963 * +-------------------------------------------------+
2970 * all in (left, less) < pivot
2971 * all in [less, k) == pivot
2972 * all in (great, right) > pivot
2974 * Pointer k is the first index of ?-part.
2976 for (int k = less; k <= great; ++k) {
2977 if (a[k] == pivot) {
2981 if (ak < pivot) { // Move a[k] to left part
2985 } else { // a[k] > pivot - Move a[k] to right part
2986 while (a[great] > pivot) {
2989 if (a[great] < pivot) { // a[great] <= pivot
2993 } else { // a[great] == pivot
2995 * Even though a[great] equals to pivot, the
2996 * assignment a[k] = pivot may be incorrect,
2997 * if a[great] and pivot are floating-point
2998 * zeros of different signs. Therefore in float
2999 * and double sorting methods we have to use
3000 * more accurate assignment a[k] = a[great].
3010 * Sort left and right parts recursively.
3011 * All elements from center part are equal
3012 * and, therefore, already sorted.
3014 sort(a, left, less - 1, leftmost);
3015 sort(a, great + 1, right, false);