jdk/src/share/classes/java/util/DualPivotQuicksort.java
changeset 8188 b90884cf34f5
parent 6896 d229d56fd918
child 8193 626b859aabb2
equal deleted inserted replaced
8187:cbd5448015d8 8188:b90884cf34f5
    34  *
    34  *
    35  * @author Vladimir Yaroslavskiy
    35  * @author Vladimir Yaroslavskiy
    36  * @author Jon Bentley
    36  * @author Jon Bentley
    37  * @author Josh Bloch
    37  * @author Josh Bloch
    38  *
    38  *
    39  * @version 2010.10.13 m765.827.12i:5\7p
    39  * @version 2011.01.21 m765.827.12i:5\7pm
    40  * @since 1.7
    40  * @since 1.7
    41  */
    41  */
    42 final class DualPivotQuicksort {
    42 final class DualPivotQuicksort {
    43 
    43 
    44     /**
    44     /**
    47     private DualPivotQuicksort() {}
    47     private DualPivotQuicksort() {}
    48 
    48 
    49     /*
    49     /*
    50      * Tuning parameters.
    50      * Tuning parameters.
    51      */
    51      */
       
    52 
       
    53     /**
       
    54      * The maximum number of runs in merge sort.
       
    55      */
       
    56     private static final int MAX_RUN_COUNT = 67;
       
    57 
       
    58     /**
       
    59      * The maximum length of run in merge sort.
       
    60      */
       
    61     private static final int MAX_RUN_LENGTH = 33;
       
    62 
       
    63     /**
       
    64      * If the length of an array to be sorted is less than this
       
    65      * constant, Quicksort is used in preference to merge sort.
       
    66      */
       
    67     private static final int QUICKSORT_THRESHOLD = 286;
    52 
    68 
    53     /**
    69     /**
    54      * If the length of an array to be sorted is less than this
    70      * If the length of an array to be sorted is less than this
    55      * constant, insertion sort is used in preference to Quicksort.
    71      * constant, insertion sort is used in preference to Quicksort.
    56      */
    72      */
    76      * Sorts the specified array.
    92      * Sorts the specified array.
    77      *
    93      *
    78      * @param a the array to be sorted
    94      * @param a the array to be sorted
    79      */
    95      */
    80     public static void sort(int[] a) {
    96     public static void sort(int[] a) {
    81         sort(a, 0, a.length - 1, true);
    97         sort(a, 0, a.length - 1);
    82     }
    98     }
    83 
    99 
    84     /**
   100     /**
    85      * Sorts the specified range of the array.
   101      * Sorts the specified range of the array.
    86      *
   102      *
    87      * @param a the array to be sorted
   103      * @param a the array to be sorted
    88      * @param left the index of the first element, inclusive, to be sorted
   104      * @param left the index of the first element, inclusive, to be sorted
    89      * @param right the index of the last element, inclusive, to be sorted
   105      * @param right the index of the last element, inclusive, to be sorted
    90      */
   106      */
    91     public static void sort(int[] a, int left, int right) {
   107     public static void sort(int[] a, int left, int right) {
    92         sort(a, left, right, true);
   108         // Use Quicksort on small arrays
       
   109         if (right - left < QUICKSORT_THRESHOLD) {
       
   110             sort(a, left, right, true);
       
   111             return;
       
   112         }
       
   113 
       
   114         /*
       
   115          * Index run[i] is the start of i-th run
       
   116          * (ascending or descending sequence).
       
   117          */
       
   118         int[] run = new int[MAX_RUN_COUNT];
       
   119         int count = 0; run[0] = left;
       
   120 
       
   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;
       
   129                 }
       
   130             } else { // equal
       
   131                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
   132                     if (--m == 0) {
       
   133                         sort(a, left, right, true);
       
   134                         return;
       
   135                     }
       
   136                 }
       
   137             }
       
   138 
       
   139             /*
       
   140              * The array is not highly structured,
       
   141              * use Quicksort instead of merge sort.
       
   142              */
       
   143             if (++count == MAX_RUN_COUNT) {
       
   144                 sort(a, left, right, true);
       
   145                 return;
       
   146             }
       
   147         }
       
   148 
       
   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
       
   153             return;
       
   154         }
       
   155 
       
   156         /*
       
   157          * Create temporary array, which is used for merging.
       
   158          * Implementation note: variable "right" is increased by 1.
       
   159          */
       
   160         int[] b; byte odd = 0;
       
   161         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
   162 
       
   163         if (odd == 0) {
       
   164             b = a; a = new int[b.length];
       
   165             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
   166         } else {
       
   167             b = new int[a.length];
       
   168         }
       
   169 
       
   170         // Merging
       
   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]) {
       
   176                         b[i] = a[p++];
       
   177                     } else {
       
   178                         b[i] = a[q++];
       
   179                     }
       
   180                 }
       
   181                 run[++last] = hi;
       
   182             }
       
   183             if ((count & 1) != 0) {
       
   184                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
   185                     b[i] = a[i]
       
   186                 );
       
   187                 run[++last] = right;
       
   188             }
       
   189             int[] t = a; a = b; b = t;
       
   190         }
    93     }
   191     }
    94 
   192 
    95     /**
   193     /**
    96      * Sorts the specified range of the array by Dual-Pivot Quicksort.
   194      * Sorts the specified range of the array by Dual-Pivot Quicksort.
    97      *
   195      *
   101      * @param leftmost indicates if this part is the leftmost in the range
   199      * @param leftmost indicates if this part is the leftmost in the range
   102      */
   200      */
   103     private static void sort(int[] a, int left, int right, boolean leftmost) {
   201     private static void sort(int[] a, int left, int right, boolean leftmost) {
   104         int length = right - left + 1;
   202         int length = right - left + 1;
   105 
   203 
   106         // Use insertion sort on small arrays
   204         // Use insertion sort on tiny arrays
   107         if (length < INSERTION_SORT_THRESHOLD) {
   205         if (length < INSERTION_SORT_THRESHOLD) {
   108             if (leftmost) {
   206             if (leftmost) {
   109                 /*
   207                 /*
   110                  * Traditional (without sentinel) insertion sort,
   208                  * Traditional (without sentinel) insertion sort,
   111                  * optimized for server VM, is used in case of
   209                  * optimized for server VM, is used in case of
   124             } else {
   222             } else {
   125                 /*
   223                 /*
   126                  * Skip the longest ascending sequence.
   224                  * Skip the longest ascending sequence.
   127                  */
   225                  */
   128                 do {
   226                 do {
   129                     if (left++ >= right) {
   227                     if (left >= right) {
   130                         return;
   228                         return;
   131                     }
   229                     }
   132                 } while (a[left - 1] <= a[left]);
   230                 } while (a[++left] >= a[left - 1]);
   133 
   231 
   134                 /*
   232                 /*
   135                  * Every element from adjoining part plays the role
   233                  * Every element from adjoining part plays the role
   136                  * of sentinel, therefore this allows us to avoid the
   234                  * of sentinel, therefore this allows us to avoid the
   137                  * left range check on each iteration. Moreover, we use
   235                  * left range check on each iteration. Moreover, we use
   138                  * the best improved algorithm, so called pair insertion
   236                  * the more optimized algorithm, so called pair insertion
   139                  * sort, which is faster than traditional implementation
   237                  * sort, which is faster (in the context of Quicksort)
   140                  * in the context of Dual-Pivot Quicksort.
   238                  * than traditional implementation of insertion sort.
   141                  */
   239                  */
   142                 for (int k = left--; (left += 2) <= right; ) {
   240                 for (int k = left; ++left <= right; k = ++left) {
   143                     int a1, a2; k = left - 1;
   241                     int a1 = a[k], a2 = a[left];
   144 
   242 
   145                     if (a[k] < a[left]) {
   243                     if (a1 < a2) {
   146                         a2 = a[k]; a1 = a[left];
   244                         a2 = a1; a1 = a[left];
   147                     } else {
       
   148                         a1 = a[k]; a2 = a[left];
       
   149                     }
   245                     }
   150                     while (a1 < a[--k]) {
   246                     while (a1 < a[--k]) {
   151                         a[k + 2] = a[k];
   247                         a[k + 2] = a[k];
   152                     }
   248                     }
   153                     a[++k + 1] = a1;
   249                     a[++k + 1] = a1;
   200                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
   296                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
   201                 }
   297                 }
   202             }
   298             }
   203         }
   299         }
   204 
   300 
   205         /*
       
   206          * Use the second and fourth of the five sorted elements as pivots.
       
   207          * These values are inexpensive approximations of the first and
       
   208          * second terciles of the array. Note that pivot1 <= pivot2.
       
   209          */
       
   210         int pivot1 = a[e2];
       
   211         int pivot2 = a[e4];
       
   212 
       
   213         // Pointers
   301         // Pointers
   214         int less  = left;  // The index of the first element of center part
   302         int less  = left;  // The index of the first element of center part
   215         int great = right; // The index before the first element of right part
   303         int great = right; // The index before the first element of right part
   216 
   304 
   217         if (pivot1 != pivot2) {
   305         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
   306             /*
       
   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.
       
   310              */
       
   311             int pivot1 = a[e2];
       
   312             int pivot2 = a[e4];
       
   313 
   218             /*
   314             /*
   219              * The first and the last elements to be sorted are moved to the
   315              * The first and the last elements to be sorted are moved to the
   220              * locations formerly occupied by the pivots. When partitioning
   316              * locations formerly occupied by the pivots. When partitioning
   221              * is complete, the pivots are swapped back into their final
   317              * is complete, the pivots are swapped back into their final
   222              * positions, and excluded from subsequent sorting.
   318              * positions, and excluded from subsequent sorting.
   257                     /*
   353                     /*
   258                      * Here and below we use "a[i] = b; i++;" instead
   354                      * Here and below we use "a[i] = b; i++;" instead
   259                      * of "a[i++] = b;" due to performance issue.
   355                      * of "a[i++] = b;" due to performance issue.
   260                      */
   356                      */
   261                     a[less] = ak;
   357                     a[less] = ak;
   262                     less++;
   358                     ++less;
   263                 } else if (ak > pivot2) { // Move a[k] to right part
   359                 } else if (ak > pivot2) { // Move a[k] to right part
   264                     while (a[great] > pivot2) {
   360                     while (a[great] > pivot2) {
   265                         if (great-- == k) {
   361                         if (great-- == k) {
   266                             break outer;
   362                             break outer;
   267                         }
   363                         }
   268                     }
   364                     }
   269                     if (a[great] < pivot1) { // a[great] <= pivot2
   365                     if (a[great] < pivot1) { // a[great] <= pivot2
   270                         a[k] = a[less];
   366                         a[k] = a[less];
   271                         a[less] = a[great];
   367                         a[less] = a[great];
   272                         less++;
   368                         ++less;
   273                     } else { // pivot1 <= a[great] <= pivot2
   369                     } else { // pivot1 <= a[great] <= pivot2
   274                         a[k] = a[great];
   370                         a[k] = a[great];
   275                     }
   371                     }
   276                     /*
   372                     /*
   277                      * Here and below we use "a[i] = b; i--;" instead
   373                      * Here and below we use "a[i] = b; i--;" instead
   278                      * of "a[i--] = b;" due to performance issue.
   374                      * of "a[i--] = b;" due to performance issue.
   279                      */
   375                      */
   280                     a[great] = ak;
   376                     a[great] = ak;
   281                     great--;
   377                     --great;
   282                 }
   378                 }
   283             }
   379             }
   284 
   380 
   285             // Swap pivots into their final positions
   381             // Swap pivots into their final positions
   286             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
   382             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
   297             if (less < e1 && e5 < great) {
   393             if (less < e1 && e5 < great) {
   298                 /*
   394                 /*
   299                  * Skip elements, which are equal to pivot values.
   395                  * Skip elements, which are equal to pivot values.
   300                  */
   396                  */
   301                 while (a[less] == pivot1) {
   397                 while (a[less] == pivot1) {
   302                     less++;
   398                     ++less;
   303                 }
   399                 }
       
   400 
   304                 while (a[great] == pivot2) {
   401                 while (a[great] == pivot2) {
   305                     great--;
   402                     --great;
   306                 }
   403                 }
   307 
   404 
   308                 /*
   405                 /*
   309                  * Partitioning:
   406                  * Partitioning:
   310                  *
   407                  *
   328                 for (int k = less - 1; ++k <= great; ) {
   425                 for (int k = less - 1; ++k <= great; ) {
   329                     int ak = a[k];
   426                     int ak = a[k];
   330                     if (ak == pivot1) { // Move a[k] to left part
   427                     if (ak == pivot1) { // Move a[k] to left part
   331                         a[k] = a[less];
   428                         a[k] = a[less];
   332                         a[less] = ak;
   429                         a[less] = ak;
   333                         less++;
   430                         ++less;
   334                     } else if (ak == pivot2) { // Move a[k] to right part
   431                     } else if (ak == pivot2) { // Move a[k] to right part
   335                         while (a[great] == pivot2) {
   432                         while (a[great] == pivot2) {
   336                             if (great-- == k) {
   433                             if (great-- == k) {
   337                                 break outer;
   434                                 break outer;
   338                             }
   435                             }
   346                              * of different signs. Therefore in float and
   443                              * of different signs. Therefore in float and
   347                              * double sorting methods we have to use more
   444                              * double sorting methods we have to use more
   348                              * accurate assignment a[less] = a[great].
   445                              * accurate assignment a[less] = a[great].
   349                              */
   446                              */
   350                             a[less] = pivot1;
   447                             a[less] = pivot1;
   351                             less++;
   448                             ++less;
   352                         } else { // pivot1 < a[great] < pivot2
   449                         } else { // pivot1 < a[great] < pivot2
   353                             a[k] = a[great];
   450                             a[k] = a[great];
   354                         }
   451                         }
   355                         a[great] = ak;
   452                         a[great] = ak;
   356                         great--;
   453                         --great;
   357                     }
   454                     }
   358                 }
   455                 }
   359             }
   456             }
   360 
   457 
   361             // Sort center part recursively
   458             // Sort center part recursively
   362             sort(a, less, great, false);
   459             sort(a, less, great, false);
   363 
   460 
   364         } else { // Pivots are equal
   461         } else { // Partitioning with one pivot
       
   462             /*
       
   463              * Use the third of the five sorted elements as pivot.
       
   464              * This value is inexpensive approximation of the median.
       
   465              */
       
   466             int pivot = a[e3];
       
   467 
   365             /*
   468             /*
   366              * Partitioning degenerates to the traditional 3-way
   469              * Partitioning degenerates to the traditional 3-way
   367              * (or "Dutch National Flag") schema:
   470              * (or "Dutch National Flag") schema:
   368              *
   471              *
   369              *   left part    center part              right part
   472              *   left part    center part              right part
   381              *   all in (great, right) > pivot
   484              *   all in (great, right) > pivot
   382              *
   485              *
   383              * Pointer k is the first index of ?-part.
   486              * Pointer k is the first index of ?-part.
   384              */
   487              */
   385             for (int k = less; k <= great; ++k) {
   488             for (int k = less; k <= great; ++k) {
   386                 if (a[k] == pivot1) {
   489                 if (a[k] == pivot) {
   387                     continue;
   490                     continue;
   388                 }
   491                 }
   389                 int ak = a[k];
   492                 int ak = a[k];
   390                 if (ak < pivot1) { // Move a[k] to left part
   493                 if (ak < pivot) { // Move a[k] to left part
   391                     a[k] = a[less];
   494                     a[k] = a[less];
   392                     a[less] = ak;
   495                     a[less] = ak;
   393                     less++;
   496                     ++less;
   394                 } else { // a[k] > pivot1 - Move a[k] to right part
   497                 } else { // a[k] > pivot - Move a[k] to right part
   395                     while (a[great] > pivot1) {
   498                     while (a[great] > pivot) {
   396                         great--;
   499                         --great;
   397                     }
   500                     }
   398                     if (a[great] < pivot1) { // a[great] <= pivot1
   501                     if (a[great] < pivot) { // a[great] <= pivot
   399                         a[k] = a[less];
   502                         a[k] = a[less];
   400                         a[less] = a[great];
   503                         a[less] = a[great];
   401                         less++;
   504                         ++less;
   402                     } else { // a[great] == pivot1
   505                     } else { // a[great] == pivot
   403                         /*
   506                         /*
   404                          * Even though a[great] equals to pivot1, the
   507                          * Even though a[great] equals to pivot, the
   405                          * assignment a[k] = pivot1 may be incorrect,
   508                          * assignment a[k] = pivot may be incorrect,
   406                          * if a[great] and pivot1 are floating-point
   509                          * if a[great] and pivot are floating-point
   407                          * zeros of different signs. Therefore in float
   510                          * zeros of different signs. Therefore in float
   408                          * and double sorting methods we have to use
   511                          * and double sorting methods we have to use
   409                          * more accurate assignment a[k] = a[great].
   512                          * more accurate assignment a[k] = a[great].
   410                          */
   513                          */
   411                         a[k] = pivot1;
   514                         a[k] = pivot;
   412                     }
   515                     }
   413                     a[great] = ak;
   516                     a[great] = ak;
   414                     great--;
   517                     --great;
   415                 }
   518                 }
   416             }
   519             }
   417 
   520 
   418             /*
   521             /*
   419              * Sort left and right parts recursively.
   522              * Sort left and right parts recursively.
   429      * Sorts the specified array.
   532      * Sorts the specified array.
   430      *
   533      *
   431      * @param a the array to be sorted
   534      * @param a the array to be sorted
   432      */
   535      */
   433     public static void sort(long[] a) {
   536     public static void sort(long[] a) {
   434         sort(a, 0, a.length - 1, true);
   537         sort(a, 0, a.length - 1);
   435     }
   538     }
   436 
   539 
   437     /**
   540     /**
   438      * Sorts the specified range of the array.
   541      * Sorts the specified range of the array.
   439      *
   542      *
   440      * @param a the array to be sorted
   543      * @param a the array to be sorted
   441      * @param left the index of the first element, inclusive, to be sorted
   544      * @param left the index of the first element, inclusive, to be sorted
   442      * @param right the index of the last element, inclusive, to be sorted
   545      * @param right the index of the last element, inclusive, to be sorted
   443      */
   546      */
   444     public static void sort(long[] a, int left, int right) {
   547     public static void sort(long[] a, int left, int right) {
   445         sort(a, left, right, true);
   548         // Use Quicksort on small arrays
       
   549         if (right - left < QUICKSORT_THRESHOLD) {
       
   550             sort(a, left, right, true);
       
   551             return;
       
   552         }
       
   553 
       
   554         /*
       
   555          * Index run[i] is the start of i-th run
       
   556          * (ascending or descending sequence).
       
   557          */
       
   558         int[] run = new int[MAX_RUN_COUNT];
       
   559         int count = 0; run[0] = left;
       
   560 
       
   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;
       
   569                 }
       
   570             } else { // equal
       
   571                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
   572                     if (--m == 0) {
       
   573                         sort(a, left, right, true);
       
   574                         return;
       
   575                     }
       
   576                 }
       
   577             }
       
   578 
       
   579             /*
       
   580              * The array is not highly structured,
       
   581              * use Quicksort instead of merge sort.
       
   582              */
       
   583             if (++count == MAX_RUN_COUNT) {
       
   584                 sort(a, left, right, true);
       
   585                 return;
       
   586             }
       
   587         }
       
   588 
       
   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
       
   593             return;
       
   594         }
       
   595 
       
   596         /*
       
   597          * Create temporary array, which is used for merging.
       
   598          * Implementation note: variable "right" is increased by 1.
       
   599          */
       
   600         long[] b; byte odd = 0;
       
   601         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
   602 
       
   603         if (odd == 0) {
       
   604             b = a; a = new long[b.length];
       
   605             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
   606         } else {
       
   607             b = new long[a.length];
       
   608         }
       
   609 
       
   610         // Merging
       
   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]) {
       
   616                         b[i] = a[p++];
       
   617                     } else {
       
   618                         b[i] = a[q++];
       
   619                     }
       
   620                 }
       
   621                 run[++last] = hi;
       
   622             }
       
   623             if ((count & 1) != 0) {
       
   624                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
   625                     b[i] = a[i]
       
   626                 );
       
   627                 run[++last] = right;
       
   628             }
       
   629             long[] t = a; a = b; b = t;
       
   630         }
   446     }
   631     }
   447 
   632 
   448     /**
   633     /**
   449      * Sorts the specified range of the array by Dual-Pivot Quicksort.
   634      * Sorts the specified range of the array by Dual-Pivot Quicksort.
   450      *
   635      *
   454      * @param leftmost indicates if this part is the leftmost in the range
   639      * @param leftmost indicates if this part is the leftmost in the range
   455      */
   640      */
   456     private static void sort(long[] a, int left, int right, boolean leftmost) {
   641     private static void sort(long[] a, int left, int right, boolean leftmost) {
   457         int length = right - left + 1;
   642         int length = right - left + 1;
   458 
   643 
   459         // Use insertion sort on small arrays
   644         // Use insertion sort on tiny arrays
   460         if (length < INSERTION_SORT_THRESHOLD) {
   645         if (length < INSERTION_SORT_THRESHOLD) {
   461             if (leftmost) {
   646             if (leftmost) {
   462                 /*
   647                 /*
   463                  * Traditional (without sentinel) insertion sort,
   648                  * Traditional (without sentinel) insertion sort,
   464                  * optimized for server VM, is used in case of
   649                  * optimized for server VM, is used in case of
   477             } else {
   662             } else {
   478                 /*
   663                 /*
   479                  * Skip the longest ascending sequence.
   664                  * Skip the longest ascending sequence.
   480                  */
   665                  */
   481                 do {
   666                 do {
   482                     if (left++ >= right) {
   667                     if (left >= right) {
   483                         return;
   668                         return;
   484                     }
   669                     }
   485                 } while (a[left - 1] <= a[left]);
   670                 } while (a[++left] >= a[left - 1]);
   486 
   671 
   487                 /*
   672                 /*
   488                  * Every element from adjoining part plays the role
   673                  * Every element from adjoining part plays the role
   489                  * of sentinel, therefore this allows us to avoid the
   674                  * of sentinel, therefore this allows us to avoid the
   490                  * left range check on each iteration. Moreover, we use
   675                  * left range check on each iteration. Moreover, we use
   491                  * the best improved algorithm, so called pair insertion
   676                  * the more optimized algorithm, so called pair insertion
   492                  * sort, which is faster than traditional implementation
   677                  * sort, which is faster (in the context of Quicksort)
   493                  * in the context of Dual-Pivot Quicksort.
   678                  * than traditional implementation of insertion sort.
   494                  */
   679                  */
   495                 for (int k = left--; (left += 2) <= right; ) {
   680                 for (int k = left; ++left <= right; k = ++left) {
   496                     long a1, a2; k = left - 1;
   681                     long a1 = a[k], a2 = a[left];
   497 
   682 
   498                     if (a[k] < a[left]) {
   683                     if (a1 < a2) {
   499                         a2 = a[k]; a1 = a[left];
   684                         a2 = a1; a1 = a[left];
   500                     } else {
       
   501                         a1 = a[k]; a2 = a[left];
       
   502                     }
   685                     }
   503                     while (a1 < a[--k]) {
   686                     while (a1 < a[--k]) {
   504                         a[k + 2] = a[k];
   687                         a[k + 2] = a[k];
   505                     }
   688                     }
   506                     a[++k + 1] = a1;
   689                     a[++k + 1] = a1;
   553                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
   736                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
   554                 }
   737                 }
   555             }
   738             }
   556         }
   739         }
   557 
   740 
   558         /*
       
   559          * Use the second and fourth of the five sorted elements as pivots.
       
   560          * These values are inexpensive approximations of the first and
       
   561          * second terciles of the array. Note that pivot1 <= pivot2.
       
   562          */
       
   563         long pivot1 = a[e2];
       
   564         long pivot2 = a[e4];
       
   565 
       
   566         // Pointers
   741         // Pointers
   567         int less  = left;  // The index of the first element of center part
   742         int less  = left;  // The index of the first element of center part
   568         int great = right; // The index before the first element of right part
   743         int great = right; // The index before the first element of right part
   569 
   744 
   570         if (pivot1 != pivot2) {
   745         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
   746             /*
       
   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.
       
   750              */
       
   751             long pivot1 = a[e2];
       
   752             long pivot2 = a[e4];
       
   753 
   571             /*
   754             /*
   572              * The first and the last elements to be sorted are moved to the
   755              * The first and the last elements to be sorted are moved to the
   573              * locations formerly occupied by the pivots. When partitioning
   756              * locations formerly occupied by the pivots. When partitioning
   574              * is complete, the pivots are swapped back into their final
   757              * is complete, the pivots are swapped back into their final
   575              * positions, and excluded from subsequent sorting.
   758              * positions, and excluded from subsequent sorting.
   610                     /*
   793                     /*
   611                      * Here and below we use "a[i] = b; i++;" instead
   794                      * Here and below we use "a[i] = b; i++;" instead
   612                      * of "a[i++] = b;" due to performance issue.
   795                      * of "a[i++] = b;" due to performance issue.
   613                      */
   796                      */
   614                     a[less] = ak;
   797                     a[less] = ak;
   615                     less++;
   798                     ++less;
   616                 } else if (ak > pivot2) { // Move a[k] to right part
   799                 } else if (ak > pivot2) { // Move a[k] to right part
   617                     while (a[great] > pivot2) {
   800                     while (a[great] > pivot2) {
   618                         if (great-- == k) {
   801                         if (great-- == k) {
   619                             break outer;
   802                             break outer;
   620                         }
   803                         }
   621                     }
   804                     }
   622                     if (a[great] < pivot1) { // a[great] <= pivot2
   805                     if (a[great] < pivot1) { // a[great] <= pivot2
   623                         a[k] = a[less];
   806                         a[k] = a[less];
   624                         a[less] = a[great];
   807                         a[less] = a[great];
   625                         less++;
   808                         ++less;
   626                     } else { // pivot1 <= a[great] <= pivot2
   809                     } else { // pivot1 <= a[great] <= pivot2
   627                         a[k] = a[great];
   810                         a[k] = a[great];
   628                     }
   811                     }
   629                     /*
   812                     /*
   630                      * Here and below we use "a[i] = b; i--;" instead
   813                      * Here and below we use "a[i] = b; i--;" instead
   631                      * of "a[i--] = b;" due to performance issue.
   814                      * of "a[i--] = b;" due to performance issue.
   632                      */
   815                      */
   633                     a[great] = ak;
   816                     a[great] = ak;
   634                     great--;
   817                     --great;
   635                 }
   818                 }
   636             }
   819             }
   637 
   820 
   638             // Swap pivots into their final positions
   821             // Swap pivots into their final positions
   639             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
   822             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
   650             if (less < e1 && e5 < great) {
   833             if (less < e1 && e5 < great) {
   651                 /*
   834                 /*
   652                  * Skip elements, which are equal to pivot values.
   835                  * Skip elements, which are equal to pivot values.
   653                  */
   836                  */
   654                 while (a[less] == pivot1) {
   837                 while (a[less] == pivot1) {
   655                     less++;
   838                     ++less;
   656                 }
   839                 }
       
   840 
   657                 while (a[great] == pivot2) {
   841                 while (a[great] == pivot2) {
   658                     great--;
   842                     --great;
   659                 }
   843                 }
   660 
   844 
   661                 /*
   845                 /*
   662                  * Partitioning:
   846                  * Partitioning:
   663                  *
   847                  *
   681                 for (int k = less - 1; ++k <= great; ) {
   865                 for (int k = less - 1; ++k <= great; ) {
   682                     long ak = a[k];
   866                     long ak = a[k];
   683                     if (ak == pivot1) { // Move a[k] to left part
   867                     if (ak == pivot1) { // Move a[k] to left part
   684                         a[k] = a[less];
   868                         a[k] = a[less];
   685                         a[less] = ak;
   869                         a[less] = ak;
   686                         less++;
   870                         ++less;
   687                     } else if (ak == pivot2) { // Move a[k] to right part
   871                     } else if (ak == pivot2) { // Move a[k] to right part
   688                         while (a[great] == pivot2) {
   872                         while (a[great] == pivot2) {
   689                             if (great-- == k) {
   873                             if (great-- == k) {
   690                                 break outer;
   874                                 break outer;
   691                             }
   875                             }
   699                              * of different signs. Therefore in float and
   883                              * of different signs. Therefore in float and
   700                              * double sorting methods we have to use more
   884                              * double sorting methods we have to use more
   701                              * accurate assignment a[less] = a[great].
   885                              * accurate assignment a[less] = a[great].
   702                              */
   886                              */
   703                             a[less] = pivot1;
   887                             a[less] = pivot1;
   704                             less++;
   888                             ++less;
   705                         } else { // pivot1 < a[great] < pivot2
   889                         } else { // pivot1 < a[great] < pivot2
   706                             a[k] = a[great];
   890                             a[k] = a[great];
   707                         }
   891                         }
   708                         a[great] = ak;
   892                         a[great] = ak;
   709                         great--;
   893                         --great;
   710                     }
   894                     }
   711                 }
   895                 }
   712             }
   896             }
   713 
   897 
   714             // Sort center part recursively
   898             // Sort center part recursively
   715             sort(a, less, great, false);
   899             sort(a, less, great, false);
   716 
   900 
   717         } else { // Pivots are equal
   901         } else { // Partitioning with one pivot
       
   902             /*
       
   903              * Use the third of the five sorted elements as pivot.
       
   904              * This value is inexpensive approximation of the median.
       
   905              */
       
   906             long pivot = a[e3];
       
   907 
   718             /*
   908             /*
   719              * Partitioning degenerates to the traditional 3-way
   909              * Partitioning degenerates to the traditional 3-way
   720              * (or "Dutch National Flag") schema:
   910              * (or "Dutch National Flag") schema:
   721              *
   911              *
   722              *   left part    center part              right part
   912              *   left part    center part              right part
   734              *   all in (great, right) > pivot
   924              *   all in (great, right) > pivot
   735              *
   925              *
   736              * Pointer k is the first index of ?-part.
   926              * Pointer k is the first index of ?-part.
   737              */
   927              */
   738             for (int k = less; k <= great; ++k) {
   928             for (int k = less; k <= great; ++k) {
   739                 if (a[k] == pivot1) {
   929                 if (a[k] == pivot) {
   740                     continue;
   930                     continue;
   741                 }
   931                 }
   742                 long ak = a[k];
   932                 long ak = a[k];
   743                 if (ak < pivot1) { // Move a[k] to left part
   933                 if (ak < pivot) { // Move a[k] to left part
   744                     a[k] = a[less];
   934                     a[k] = a[less];
   745                     a[less] = ak;
   935                     a[less] = ak;
   746                     less++;
   936                     ++less;
   747                 } else { // a[k] > pivot1 - Move a[k] to right part
   937                 } else { // a[k] > pivot - Move a[k] to right part
   748                     while (a[great] > pivot1) {
   938                     while (a[great] > pivot) {
   749                         great--;
   939                         --great;
   750                     }
   940                     }
   751                     if (a[great] < pivot1) { // a[great] <= pivot1
   941                     if (a[great] < pivot) { // a[great] <= pivot
   752                         a[k] = a[less];
   942                         a[k] = a[less];
   753                         a[less] = a[great];
   943                         a[less] = a[great];
   754                         less++;
   944                         ++less;
   755                     } else { // a[great] == pivot1
   945                     } else { // a[great] == pivot
   756                         /*
   946                         /*
   757                          * Even though a[great] equals to pivot1, the
   947                          * Even though a[great] equals to pivot, the
   758                          * assignment a[k] = pivot1 may be incorrect,
   948                          * assignment a[k] = pivot may be incorrect,
   759                          * if a[great] and pivot1 are floating-point
   949                          * if a[great] and pivot are floating-point
   760                          * zeros of different signs. Therefore in float
   950                          * zeros of different signs. Therefore in float
   761                          * and double sorting methods we have to use
   951                          * and double sorting methods we have to use
   762                          * more accurate assignment a[k] = a[great].
   952                          * more accurate assignment a[k] = a[great].
   763                          */
   953                          */
   764                         a[k] = pivot1;
   954                         a[k] = pivot;
   765                     }
   955                     }
   766                     a[great] = ak;
   956                     a[great] = ak;
   767                     great--;
   957                     --great;
   768                 }
   958                 }
   769             }
   959             }
   770 
   960 
   771             /*
   961             /*
   772              * Sort left and right parts recursively.
   962              * Sort left and right parts recursively.
   797     public static void sort(short[] a, int left, int right) {
   987     public static void sort(short[] a, int left, int right) {
   798         // Use counting sort on large arrays
   988         // Use counting sort on large arrays
   799         if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
   989         if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
   800             int[] count = new int[NUM_SHORT_VALUES];
   990             int[] count = new int[NUM_SHORT_VALUES];
   801 
   991 
   802             for (int i = left - 1; ++i <= right; ) {
   992             for (int i = left - 1; ++i <= right;
   803                 count[a[i] - Short.MIN_VALUE]++;
   993                 count[a[i] - Short.MIN_VALUE]++
   804             }
   994             );
   805             for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
   995             for (int i = NUM_SHORT_VALUES, k = right + 1; k > left; ) {
   806                 while (count[--i] == 0);
   996                 while (count[--i] == 0);
   807                 short value = (short) (i + Short.MIN_VALUE);
   997                 short value = (short) (i + Short.MIN_VALUE);
   808                 int s = count[i];
   998                 int s = count[i];
   809 
   999 
   810                 do {
  1000                 do {
   811                     a[--k] = value;
  1001                     a[--k] = value;
   812                 } while (--s > 0);
  1002                 } while (--s > 0);
   813             }
  1003             }
   814         } else { // Use Dual-Pivot Quicksort on small arrays
  1004         } else { // Use Dual-Pivot Quicksort on small arrays
   815             sort(a, left, right, true);
  1005             doSort(a, left, right);
   816         }
  1006         }
   817     }
  1007     }
   818 
  1008 
   819     /** The number of distinct short values. */
  1009     /** The number of distinct short values. */
   820     private static final int NUM_SHORT_VALUES = 1 << 16;
  1010     private static final int NUM_SHORT_VALUES = 1 << 16;
       
  1011 
       
  1012     /**
       
  1013      * Sorts the specified range of the array.
       
  1014      *
       
  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
       
  1018      */
       
  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);
       
  1023             return;
       
  1024         }
       
  1025 
       
  1026         /*
       
  1027          * Index run[i] is the start of i-th run
       
  1028          * (ascending or descending sequence).
       
  1029          */
       
  1030         int[] run = new int[MAX_RUN_COUNT];
       
  1031         int count = 0; run[0] = left;
       
  1032 
       
  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;
       
  1041                 }
       
  1042             } else { // equal
       
  1043                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
  1044                     if (--m == 0) {
       
  1045                         sort(a, left, right, true);
       
  1046                         return;
       
  1047                     }
       
  1048                 }
       
  1049             }
       
  1050 
       
  1051             /*
       
  1052              * The array is not highly structured,
       
  1053              * use Quicksort instead of merge sort.
       
  1054              */
       
  1055             if (++count == MAX_RUN_COUNT) {
       
  1056                 sort(a, left, right, true);
       
  1057                 return;
       
  1058             }
       
  1059         }
       
  1060 
       
  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
       
  1065             return;
       
  1066         }
       
  1067 
       
  1068         /*
       
  1069          * Create temporary array, which is used for merging.
       
  1070          * Implementation note: variable "right" is increased by 1.
       
  1071          */
       
  1072         short[] b; byte odd = 0;
       
  1073         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
  1074 
       
  1075         if (odd == 0) {
       
  1076             b = a; a = new short[b.length];
       
  1077             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
  1078         } else {
       
  1079             b = new short[a.length];
       
  1080         }
       
  1081 
       
  1082         // Merging
       
  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]) {
       
  1088                         b[i] = a[p++];
       
  1089                     } else {
       
  1090                         b[i] = a[q++];
       
  1091                     }
       
  1092                 }
       
  1093                 run[++last] = hi;
       
  1094             }
       
  1095             if ((count & 1) != 0) {
       
  1096                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
  1097                     b[i] = a[i]
       
  1098                 );
       
  1099                 run[++last] = right;
       
  1100             }
       
  1101             short[] t = a; a = b; b = t;
       
  1102         }
       
  1103     }
   821 
  1104 
   822     /**
  1105     /**
   823      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  1106      * Sorts the specified range of the array by Dual-Pivot Quicksort.
   824      *
  1107      *
   825      * @param a the array to be sorted
  1108      * @param a the array to be sorted
   826      * @param left the index of the first element, inclusive, to be sorted
  1109      * @param left the index of the first element, inclusive, to be sorted
   827      * @param right the index of the last element, inclusive, to be sorted
  1110      * @param right the index of the last element, inclusive, to be sorted
   828      * @param leftmost indicates if this part is the leftmost in the range
  1111      * @param leftmost indicates if this part is the leftmost in the range
   829      */
  1112      */
   830     private static void sort(short[] a, int left, int right,boolean leftmost) {
  1113     private static void sort(short[] a, int left, int right, boolean leftmost) {
   831         int length = right - left + 1;
  1114         int length = right - left + 1;
   832 
  1115 
   833         // Use insertion sort on small arrays
  1116         // Use insertion sort on tiny arrays
   834         if (length < INSERTION_SORT_THRESHOLD) {
  1117         if (length < INSERTION_SORT_THRESHOLD) {
   835             if (leftmost) {
  1118             if (leftmost) {
   836                 /*
  1119                 /*
   837                  * Traditional (without sentinel) insertion sort,
  1120                  * Traditional (without sentinel) insertion sort,
   838                  * optimized for server VM, is used in case of
  1121                  * optimized for server VM, is used in case of
   851             } else {
  1134             } else {
   852                 /*
  1135                 /*
   853                  * Skip the longest ascending sequence.
  1136                  * Skip the longest ascending sequence.
   854                  */
  1137                  */
   855                 do {
  1138                 do {
   856                     if (left++ >= right) {
  1139                     if (left >= right) {
   857                         return;
  1140                         return;
   858                     }
  1141                     }
   859                 } while (a[left - 1] <= a[left]);
  1142                 } while (a[++left] >= a[left - 1]);
   860 
  1143 
   861                 /*
  1144                 /*
   862                  * Every element from adjoining part plays the role
  1145                  * Every element from adjoining part plays the role
   863                  * of sentinel, therefore this allows us to avoid the
  1146                  * of sentinel, therefore this allows us to avoid the
   864                  * left range check on each iteration. Moreover, we use
  1147                  * left range check on each iteration. Moreover, we use
   865                  * the best improved algorithm, so called pair insertion
  1148                  * the more optimized algorithm, so called pair insertion
   866                  * sort, which is faster than traditional implementation
  1149                  * sort, which is faster (in the context of Quicksort)
   867                  * in the context of Dual-Pivot Quicksort.
  1150                  * than traditional implementation of insertion sort.
   868                  */
  1151                  */
   869                 for (int k = left--; (left += 2) <= right; ) {
  1152                 for (int k = left; ++left <= right; k = ++left) {
   870                     short a1, a2; k = left - 1;
  1153                     short a1 = a[k], a2 = a[left];
   871 
  1154 
   872                     if (a[k] < a[left]) {
  1155                     if (a1 < a2) {
   873                         a2 = a[k]; a1 = a[left];
  1156                         a2 = a1; a1 = a[left];
   874                     } else {
       
   875                         a1 = a[k]; a2 = a[left];
       
   876                     }
  1157                     }
   877                     while (a1 < a[--k]) {
  1158                     while (a1 < a[--k]) {
   878                         a[k + 2] = a[k];
  1159                         a[k + 2] = a[k];
   879                     }
  1160                     }
   880                     a[++k + 1] = a1;
  1161                     a[++k + 1] = a1;
   927                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1208                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
   928                 }
  1209                 }
   929             }
  1210             }
   930         }
  1211         }
   931 
  1212 
   932         /*
       
   933          * Use the second and fourth of the five sorted elements as pivots.
       
   934          * These values are inexpensive approximations of the first and
       
   935          * second terciles of the array. Note that pivot1 <= pivot2.
       
   936          */
       
   937         short pivot1 = a[e2];
       
   938         short pivot2 = a[e4];
       
   939 
       
   940         // Pointers
  1213         // Pointers
   941         int less  = left;  // The index of the first element of center part
  1214         int less  = left;  // The index of the first element of center part
   942         int great = right; // The index before the first element of right part
  1215         int great = right; // The index before the first element of right part
   943 
  1216 
   944         if (pivot1 != pivot2) {
  1217         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
  1218             /*
       
  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.
       
  1222              */
       
  1223             short pivot1 = a[e2];
       
  1224             short pivot2 = a[e4];
       
  1225 
   945             /*
  1226             /*
   946              * The first and the last elements to be sorted are moved to the
  1227              * The first and the last elements to be sorted are moved to the
   947              * locations formerly occupied by the pivots. When partitioning
  1228              * locations formerly occupied by the pivots. When partitioning
   948              * is complete, the pivots are swapped back into their final
  1229              * is complete, the pivots are swapped back into their final
   949              * positions, and excluded from subsequent sorting.
  1230              * positions, and excluded from subsequent sorting.
   984                     /*
  1265                     /*
   985                      * Here and below we use "a[i] = b; i++;" instead
  1266                      * Here and below we use "a[i] = b; i++;" instead
   986                      * of "a[i++] = b;" due to performance issue.
  1267                      * of "a[i++] = b;" due to performance issue.
   987                      */
  1268                      */
   988                     a[less] = ak;
  1269                     a[less] = ak;
   989                     less++;
  1270                     ++less;
   990                 } else if (ak > pivot2) { // Move a[k] to right part
  1271                 } else if (ak > pivot2) { // Move a[k] to right part
   991                     while (a[great] > pivot2) {
  1272                     while (a[great] > pivot2) {
   992                         if (great-- == k) {
  1273                         if (great-- == k) {
   993                             break outer;
  1274                             break outer;
   994                         }
  1275                         }
   995                     }
  1276                     }
   996                     if (a[great] < pivot1) { // a[great] <= pivot2
  1277                     if (a[great] < pivot1) { // a[great] <= pivot2
   997                         a[k] = a[less];
  1278                         a[k] = a[less];
   998                         a[less] = a[great];
  1279                         a[less] = a[great];
   999                         less++;
  1280                         ++less;
  1000                     } else { // pivot1 <= a[great] <= pivot2
  1281                     } else { // pivot1 <= a[great] <= pivot2
  1001                         a[k] = a[great];
  1282                         a[k] = a[great];
  1002                     }
  1283                     }
  1003                     /*
  1284                     /*
  1004                      * Here and below we use "a[i] = b; i--;" instead
  1285                      * Here and below we use "a[i] = b; i--;" instead
  1005                      * of "a[i--] = b;" due to performance issue.
  1286                      * of "a[i--] = b;" due to performance issue.
  1006                      */
  1287                      */
  1007                     a[great] = ak;
  1288                     a[great] = ak;
  1008                     great--;
  1289                     --great;
  1009                 }
  1290                 }
  1010             }
  1291             }
  1011 
  1292 
  1012             // Swap pivots into their final positions
  1293             // Swap pivots into their final positions
  1013             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  1294             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  1024             if (less < e1 && e5 < great) {
  1305             if (less < e1 && e5 < great) {
  1025                 /*
  1306                 /*
  1026                  * Skip elements, which are equal to pivot values.
  1307                  * Skip elements, which are equal to pivot values.
  1027                  */
  1308                  */
  1028                 while (a[less] == pivot1) {
  1309                 while (a[less] == pivot1) {
  1029                     less++;
  1310                     ++less;
  1030                 }
  1311                 }
       
  1312 
  1031                 while (a[great] == pivot2) {
  1313                 while (a[great] == pivot2) {
  1032                     great--;
  1314                     --great;
  1033                 }
  1315                 }
  1034 
  1316 
  1035                 /*
  1317                 /*
  1036                  * Partitioning:
  1318                  * Partitioning:
  1037                  *
  1319                  *
  1055                 for (int k = less - 1; ++k <= great; ) {
  1337                 for (int k = less - 1; ++k <= great; ) {
  1056                     short ak = a[k];
  1338                     short ak = a[k];
  1057                     if (ak == pivot1) { // Move a[k] to left part
  1339                     if (ak == pivot1) { // Move a[k] to left part
  1058                         a[k] = a[less];
  1340                         a[k] = a[less];
  1059                         a[less] = ak;
  1341                         a[less] = ak;
  1060                         less++;
  1342                         ++less;
  1061                     } else if (ak == pivot2) { // Move a[k] to right part
  1343                     } else if (ak == pivot2) { // Move a[k] to right part
  1062                         while (a[great] == pivot2) {
  1344                         while (a[great] == pivot2) {
  1063                             if (great-- == k) {
  1345                             if (great-- == k) {
  1064                                 break outer;
  1346                                 break outer;
  1065                             }
  1347                             }
  1073                              * of different signs. Therefore in float and
  1355                              * of different signs. Therefore in float and
  1074                              * double sorting methods we have to use more
  1356                              * double sorting methods we have to use more
  1075                              * accurate assignment a[less] = a[great].
  1357                              * accurate assignment a[less] = a[great].
  1076                              */
  1358                              */
  1077                             a[less] = pivot1;
  1359                             a[less] = pivot1;
  1078                             less++;
  1360                             ++less;
  1079                         } else { // pivot1 < a[great] < pivot2
  1361                         } else { // pivot1 < a[great] < pivot2
  1080                             a[k] = a[great];
  1362                             a[k] = a[great];
  1081                         }
  1363                         }
  1082                         a[great] = ak;
  1364                         a[great] = ak;
  1083                         great--;
  1365                         --great;
  1084                     }
  1366                     }
  1085                 }
  1367                 }
  1086             }
  1368             }
  1087 
  1369 
  1088             // Sort center part recursively
  1370             // Sort center part recursively
  1089             sort(a, less, great, false);
  1371             sort(a, less, great, false);
  1090 
  1372 
  1091         } else { // Pivots are equal
  1373         } else { // Partitioning with one pivot
       
  1374             /*
       
  1375              * Use the third of the five sorted elements as pivot.
       
  1376              * This value is inexpensive approximation of the median.
       
  1377              */
       
  1378             short pivot = a[e3];
       
  1379 
  1092             /*
  1380             /*
  1093              * Partitioning degenerates to the traditional 3-way
  1381              * Partitioning degenerates to the traditional 3-way
  1094              * (or "Dutch National Flag") schema:
  1382              * (or "Dutch National Flag") schema:
  1095              *
  1383              *
  1096              *   left part    center part              right part
  1384              *   left part    center part              right part
  1108              *   all in (great, right) > pivot
  1396              *   all in (great, right) > pivot
  1109              *
  1397              *
  1110              * Pointer k is the first index of ?-part.
  1398              * Pointer k is the first index of ?-part.
  1111              */
  1399              */
  1112             for (int k = less; k <= great; ++k) {
  1400             for (int k = less; k <= great; ++k) {
  1113                 if (a[k] == pivot1) {
  1401                 if (a[k] == pivot) {
  1114                     continue;
  1402                     continue;
  1115                 }
  1403                 }
  1116                 short ak = a[k];
  1404                 short ak = a[k];
  1117                 if (ak < pivot1) { // Move a[k] to left part
  1405                 if (ak < pivot) { // Move a[k] to left part
  1118                     a[k] = a[less];
  1406                     a[k] = a[less];
  1119                     a[less] = ak;
  1407                     a[less] = ak;
  1120                     less++;
  1408                     ++less;
  1121                 } else { // a[k] > pivot1 - Move a[k] to right part
  1409                 } else { // a[k] > pivot - Move a[k] to right part
  1122                     while (a[great] > pivot1) {
  1410                     while (a[great] > pivot) {
  1123                         great--;
  1411                         --great;
  1124                     }
  1412                     }
  1125                     if (a[great] < pivot1) { // a[great] <= pivot1
  1413                     if (a[great] < pivot) { // a[great] <= pivot
  1126                         a[k] = a[less];
  1414                         a[k] = a[less];
  1127                         a[less] = a[great];
  1415                         a[less] = a[great];
  1128                         less++;
  1416                         ++less;
  1129                     } else { // a[great] == pivot1
  1417                     } else { // a[great] == pivot
  1130                         /*
  1418                         /*
  1131                          * Even though a[great] equals to pivot1, the
  1419                          * Even though a[great] equals to pivot, the
  1132                          * assignment a[k] = pivot1 may be incorrect,
  1420                          * assignment a[k] = pivot may be incorrect,
  1133                          * if a[great] and pivot1 are floating-point
  1421                          * if a[great] and pivot are floating-point
  1134                          * zeros of different signs. Therefore in float
  1422                          * zeros of different signs. Therefore in float
  1135                          * and double sorting methods we have to use
  1423                          * and double sorting methods we have to use
  1136                          * more accurate assignment a[k] = a[great].
  1424                          * more accurate assignment a[k] = a[great].
  1137                          */
  1425                          */
  1138                         a[k] = pivot1;
  1426                         a[k] = pivot;
  1139                     }
  1427                     }
  1140                     a[great] = ak;
  1428                     a[great] = ak;
  1141                     great--;
  1429                     --great;
  1142                 }
  1430                 }
  1143             }
  1431             }
  1144 
  1432 
  1145             /*
  1433             /*
  1146              * Sort left and right parts recursively.
  1434              * Sort left and right parts recursively.
  1171     public static void sort(char[] a, int left, int right) {
  1459     public static void sort(char[] a, int left, int right) {
  1172         // Use counting sort on large arrays
  1460         // Use counting sort on large arrays
  1173         if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
  1461         if (right - left > COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR) {
  1174             int[] count = new int[NUM_CHAR_VALUES];
  1462             int[] count = new int[NUM_CHAR_VALUES];
  1175 
  1463 
  1176             for (int i = left - 1; ++i <= right; ) {
  1464             for (int i = left - 1; ++i <= right;
  1177                 count[a[i]]++;
  1465                 count[a[i]]++
  1178             }
  1466             );
  1179             for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
  1467             for (int i = NUM_CHAR_VALUES, k = right + 1; k > left; ) {
  1180                 while (count[--i] == 0);
  1468                 while (count[--i] == 0);
  1181                 char value = (char) i;
  1469                 char value = (char) i;
  1182                 int s = count[i];
  1470                 int s = count[i];
  1183 
  1471 
  1184                 do {
  1472                 do {
  1185                     a[--k] = value;
  1473                     a[--k] = value;
  1186                 } while (--s > 0);
  1474                 } while (--s > 0);
  1187             }
  1475             }
  1188         } else { // Use Dual-Pivot Quicksort on small arrays
  1476         } else { // Use Dual-Pivot Quicksort on small arrays
  1189             sort(a, left, right, true);
  1477             doSort(a, left, right);
  1190         }
  1478         }
  1191     }
  1479     }
  1192 
  1480 
  1193     /** The number of distinct char values. */
  1481     /** The number of distinct char values. */
  1194     private static final int NUM_CHAR_VALUES = 1 << 16;
  1482     private static final int NUM_CHAR_VALUES = 1 << 16;
       
  1483 
       
  1484     /**
       
  1485      * Sorts the specified range of the array.
       
  1486      *
       
  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
       
  1490      */
       
  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);
       
  1495             return;
       
  1496         }
       
  1497 
       
  1498         /*
       
  1499          * Index run[i] is the start of i-th run
       
  1500          * (ascending or descending sequence).
       
  1501          */
       
  1502         int[] run = new int[MAX_RUN_COUNT];
       
  1503         int count = 0; run[0] = left;
       
  1504 
       
  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;
       
  1513                 }
       
  1514             } else { // equal
       
  1515                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
  1516                     if (--m == 0) {
       
  1517                         sort(a, left, right, true);
       
  1518                         return;
       
  1519                     }
       
  1520                 }
       
  1521             }
       
  1522 
       
  1523             /*
       
  1524              * The array is not highly structured,
       
  1525              * use Quicksort instead of merge sort.
       
  1526              */
       
  1527             if (++count == MAX_RUN_COUNT) {
       
  1528                 sort(a, left, right, true);
       
  1529                 return;
       
  1530             }
       
  1531         }
       
  1532 
       
  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
       
  1537             return;
       
  1538         }
       
  1539 
       
  1540         /*
       
  1541          * Create temporary array, which is used for merging.
       
  1542          * Implementation note: variable "right" is increased by 1.
       
  1543          */
       
  1544         char[] b; byte odd = 0;
       
  1545         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
  1546 
       
  1547         if (odd == 0) {
       
  1548             b = a; a = new char[b.length];
       
  1549             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
  1550         } else {
       
  1551             b = new char[a.length];
       
  1552         }
       
  1553 
       
  1554         // Merging
       
  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]) {
       
  1560                         b[i] = a[p++];
       
  1561                     } else {
       
  1562                         b[i] = a[q++];
       
  1563                     }
       
  1564                 }
       
  1565                 run[++last] = hi;
       
  1566             }
       
  1567             if ((count & 1) != 0) {
       
  1568                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
  1569                     b[i] = a[i]
       
  1570                 );
       
  1571                 run[++last] = right;
       
  1572             }
       
  1573             char[] t = a; a = b; b = t;
       
  1574         }
       
  1575     }
  1195 
  1576 
  1196     /**
  1577     /**
  1197      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  1578      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  1198      *
  1579      *
  1199      * @param a the array to be sorted
  1580      * @param a the array to be sorted
  1202      * @param leftmost indicates if this part is the leftmost in the range
  1583      * @param leftmost indicates if this part is the leftmost in the range
  1203      */
  1584      */
  1204     private static void sort(char[] a, int left, int right, boolean leftmost) {
  1585     private static void sort(char[] a, int left, int right, boolean leftmost) {
  1205         int length = right - left + 1;
  1586         int length = right - left + 1;
  1206 
  1587 
  1207         // Use insertion sort on small arrays
  1588         // Use insertion sort on tiny arrays
  1208         if (length < INSERTION_SORT_THRESHOLD) {
  1589         if (length < INSERTION_SORT_THRESHOLD) {
  1209             if (leftmost) {
  1590             if (leftmost) {
  1210                 /*
  1591                 /*
  1211                  * Traditional (without sentinel) insertion sort,
  1592                  * Traditional (without sentinel) insertion sort,
  1212                  * optimized for server VM, is used in case of
  1593                  * optimized for server VM, is used in case of
  1225             } else {
  1606             } else {
  1226                 /*
  1607                 /*
  1227                  * Skip the longest ascending sequence.
  1608                  * Skip the longest ascending sequence.
  1228                  */
  1609                  */
  1229                 do {
  1610                 do {
  1230                     if (left++ >= right) {
  1611                     if (left >= right) {
  1231                         return;
  1612                         return;
  1232                     }
  1613                     }
  1233                 } while (a[left - 1] <= a[left]);
  1614                 } while (a[++left] >= a[left - 1]);
  1234 
  1615 
  1235                 /*
  1616                 /*
  1236                  * Every element from adjoining part plays the role
  1617                  * Every element from adjoining part plays the role
  1237                  * of sentinel, therefore this allows us to avoid the
  1618                  * of sentinel, therefore this allows us to avoid the
  1238                  * left range check on each iteration. Moreover, we use
  1619                  * left range check on each iteration. Moreover, we use
  1239                  * the best improved algorithm, so called pair insertion
  1620                  * the more optimized algorithm, so called pair insertion
  1240                  * sort, which is faster than traditional implementation
  1621                  * sort, which is faster (in the context of Quicksort)
  1241                  * in the context of Dual-Pivot Quicksort.
  1622                  * than traditional implementation of insertion sort.
  1242                  */
  1623                  */
  1243                 for (int k = left--; (left += 2) <= right; ) {
  1624                 for (int k = left; ++left <= right; k = ++left) {
  1244                     char a1, a2; k = left - 1;
  1625                     char a1 = a[k], a2 = a[left];
  1245 
  1626 
  1246                     if (a[k] < a[left]) {
  1627                     if (a1 < a2) {
  1247                         a2 = a[k]; a1 = a[left];
  1628                         a2 = a1; a1 = a[left];
  1248                     } else {
       
  1249                         a1 = a[k]; a2 = a[left];
       
  1250                     }
  1629                     }
  1251                     while (a1 < a[--k]) {
  1630                     while (a1 < a[--k]) {
  1252                         a[k + 2] = a[k];
  1631                         a[k + 2] = a[k];
  1253                     }
  1632                     }
  1254                     a[++k + 1] = a1;
  1633                     a[++k + 1] = a1;
  1301                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1680                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1302                 }
  1681                 }
  1303             }
  1682             }
  1304         }
  1683         }
  1305 
  1684 
  1306         /*
       
  1307          * Use the second and fourth of the five sorted elements as pivots.
       
  1308          * These values are inexpensive approximations of the first and
       
  1309          * second terciles of the array. Note that pivot1 <= pivot2.
       
  1310          */
       
  1311         char pivot1 = a[e2];
       
  1312         char pivot2 = a[e4];
       
  1313 
       
  1314         // Pointers
  1685         // Pointers
  1315         int less  = left;  // The index of the first element of center part
  1686         int less  = left;  // The index of the first element of center part
  1316         int great = right; // The index before the first element of right part
  1687         int great = right; // The index before the first element of right part
  1317 
  1688 
  1318         if (pivot1 != pivot2) {
  1689         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
  1690             /*
       
  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.
       
  1694              */
       
  1695             char pivot1 = a[e2];
       
  1696             char pivot2 = a[e4];
       
  1697 
  1319             /*
  1698             /*
  1320              * The first and the last elements to be sorted are moved to the
  1699              * The first and the last elements to be sorted are moved to the
  1321              * locations formerly occupied by the pivots. When partitioning
  1700              * locations formerly occupied by the pivots. When partitioning
  1322              * is complete, the pivots are swapped back into their final
  1701              * is complete, the pivots are swapped back into their final
  1323              * positions, and excluded from subsequent sorting.
  1702              * positions, and excluded from subsequent sorting.
  1358                     /*
  1737                     /*
  1359                      * Here and below we use "a[i] = b; i++;" instead
  1738                      * Here and below we use "a[i] = b; i++;" instead
  1360                      * of "a[i++] = b;" due to performance issue.
  1739                      * of "a[i++] = b;" due to performance issue.
  1361                      */
  1740                      */
  1362                     a[less] = ak;
  1741                     a[less] = ak;
  1363                     less++;
  1742                     ++less;
  1364                 } else if (ak > pivot2) { // Move a[k] to right part
  1743                 } else if (ak > pivot2) { // Move a[k] to right part
  1365                     while (a[great] > pivot2) {
  1744                     while (a[great] > pivot2) {
  1366                         if (great-- == k) {
  1745                         if (great-- == k) {
  1367                             break outer;
  1746                             break outer;
  1368                         }
  1747                         }
  1369                     }
  1748                     }
  1370                     if (a[great] < pivot1) { // a[great] <= pivot2
  1749                     if (a[great] < pivot1) { // a[great] <= pivot2
  1371                         a[k] = a[less];
  1750                         a[k] = a[less];
  1372                         a[less] = a[great];
  1751                         a[less] = a[great];
  1373                         less++;
  1752                         ++less;
  1374                     } else { // pivot1 <= a[great] <= pivot2
  1753                     } else { // pivot1 <= a[great] <= pivot2
  1375                         a[k] = a[great];
  1754                         a[k] = a[great];
  1376                     }
  1755                     }
  1377                     /*
  1756                     /*
  1378                      * Here and below we use "a[i] = b; i--;" instead
  1757                      * Here and below we use "a[i] = b; i--;" instead
  1379                      * of "a[i--] = b;" due to performance issue.
  1758                      * of "a[i--] = b;" due to performance issue.
  1380                      */
  1759                      */
  1381                     a[great] = ak;
  1760                     a[great] = ak;
  1382                     great--;
  1761                     --great;
  1383                 }
  1762                 }
  1384             }
  1763             }
  1385 
  1764 
  1386             // Swap pivots into their final positions
  1765             // Swap pivots into their final positions
  1387             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  1766             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  1398             if (less < e1 && e5 < great) {
  1777             if (less < e1 && e5 < great) {
  1399                 /*
  1778                 /*
  1400                  * Skip elements, which are equal to pivot values.
  1779                  * Skip elements, which are equal to pivot values.
  1401                  */
  1780                  */
  1402                 while (a[less] == pivot1) {
  1781                 while (a[less] == pivot1) {
  1403                     less++;
  1782                     ++less;
  1404                 }
  1783                 }
       
  1784 
  1405                 while (a[great] == pivot2) {
  1785                 while (a[great] == pivot2) {
  1406                     great--;
  1786                     --great;
  1407                 }
  1787                 }
  1408 
  1788 
  1409                 /*
  1789                 /*
  1410                  * Partitioning:
  1790                  * Partitioning:
  1411                  *
  1791                  *
  1429                 for (int k = less - 1; ++k <= great; ) {
  1809                 for (int k = less - 1; ++k <= great; ) {
  1430                     char ak = a[k];
  1810                     char ak = a[k];
  1431                     if (ak == pivot1) { // Move a[k] to left part
  1811                     if (ak == pivot1) { // Move a[k] to left part
  1432                         a[k] = a[less];
  1812                         a[k] = a[less];
  1433                         a[less] = ak;
  1813                         a[less] = ak;
  1434                         less++;
  1814                         ++less;
  1435                     } else if (ak == pivot2) { // Move a[k] to right part
  1815                     } else if (ak == pivot2) { // Move a[k] to right part
  1436                         while (a[great] == pivot2) {
  1816                         while (a[great] == pivot2) {
  1437                             if (great-- == k) {
  1817                             if (great-- == k) {
  1438                                 break outer;
  1818                                 break outer;
  1439                             }
  1819                             }
  1447                              * of different signs. Therefore in float and
  1827                              * of different signs. Therefore in float and
  1448                              * double sorting methods we have to use more
  1828                              * double sorting methods we have to use more
  1449                              * accurate assignment a[less] = a[great].
  1829                              * accurate assignment a[less] = a[great].
  1450                              */
  1830                              */
  1451                             a[less] = pivot1;
  1831                             a[less] = pivot1;
  1452                             less++;
  1832                             ++less;
  1453                         } else { // pivot1 < a[great] < pivot2
  1833                         } else { // pivot1 < a[great] < pivot2
  1454                             a[k] = a[great];
  1834                             a[k] = a[great];
  1455                         }
  1835                         }
  1456                         a[great] = ak;
  1836                         a[great] = ak;
  1457                         great--;
  1837                         --great;
  1458                     }
  1838                     }
  1459                 }
  1839                 }
  1460             }
  1840             }
  1461 
  1841 
  1462             // Sort center part recursively
  1842             // Sort center part recursively
  1463             sort(a, less, great, false);
  1843             sort(a, less, great, false);
  1464 
  1844 
  1465         } else { // Pivots are equal
  1845         } else { // Partitioning with one pivot
       
  1846             /*
       
  1847              * Use the third of the five sorted elements as pivot.
       
  1848              * This value is inexpensive approximation of the median.
       
  1849              */
       
  1850             char pivot = a[e3];
       
  1851 
  1466             /*
  1852             /*
  1467              * Partitioning degenerates to the traditional 3-way
  1853              * Partitioning degenerates to the traditional 3-way
  1468              * (or "Dutch National Flag") schema:
  1854              * (or "Dutch National Flag") schema:
  1469              *
  1855              *
  1470              *   left part    center part              right part
  1856              *   left part    center part              right part
  1482              *   all in (great, right) > pivot
  1868              *   all in (great, right) > pivot
  1483              *
  1869              *
  1484              * Pointer k is the first index of ?-part.
  1870              * Pointer k is the first index of ?-part.
  1485              */
  1871              */
  1486             for (int k = less; k <= great; ++k) {
  1872             for (int k = less; k <= great; ++k) {
  1487                 if (a[k] == pivot1) {
  1873                 if (a[k] == pivot) {
  1488                     continue;
  1874                     continue;
  1489                 }
  1875                 }
  1490                 char ak = a[k];
  1876                 char ak = a[k];
  1491                 if (ak < pivot1) { // Move a[k] to left part
  1877                 if (ak < pivot) { // Move a[k] to left part
  1492                     a[k] = a[less];
  1878                     a[k] = a[less];
  1493                     a[less] = ak;
  1879                     a[less] = ak;
  1494                     less++;
  1880                     ++less;
  1495                 } else { // a[k] > pivot1 - Move a[k] to right part
  1881                 } else { // a[k] > pivot - Move a[k] to right part
  1496                     while (a[great] > pivot1) {
  1882                     while (a[great] > pivot) {
  1497                         great--;
  1883                         --great;
  1498                     }
  1884                     }
  1499                     if (a[great] < pivot1) { // a[great] <= pivot1
  1885                     if (a[great] < pivot) { // a[great] <= pivot
  1500                         a[k] = a[less];
  1886                         a[k] = a[less];
  1501                         a[less] = a[great];
  1887                         a[less] = a[great];
  1502                         less++;
  1888                         ++less;
  1503                     } else { // a[great] == pivot1
  1889                     } else { // a[great] == pivot
  1504                         /*
  1890                         /*
  1505                          * Even though a[great] equals to pivot1, the
  1891                          * Even though a[great] equals to pivot, the
  1506                          * assignment a[k] = pivot1 may be incorrect,
  1892                          * assignment a[k] = pivot may be incorrect,
  1507                          * if a[great] and pivot1 are floating-point
  1893                          * if a[great] and pivot are floating-point
  1508                          * zeros of different signs. Therefore in float
  1894                          * zeros of different signs. Therefore in float
  1509                          * and double sorting methods we have to use
  1895                          * and double sorting methods we have to use
  1510                          * more accurate assignment a[k] = a[great].
  1896                          * more accurate assignment a[k] = a[great].
  1511                          */
  1897                          */
  1512                         a[k] = pivot1;
  1898                         a[k] = pivot;
  1513                     }
  1899                     }
  1514                     a[great] = ak;
  1900                     a[great] = ak;
  1515                     great--;
  1901                     --great;
  1516                 }
  1902                 }
  1517             }
  1903             }
  1518 
  1904 
  1519             /*
  1905             /*
  1520              * Sort left and right parts recursively.
  1906              * Sort left and right parts recursively.
  1548     public static void sort(byte[] a, int left, int right) {
  1934     public static void sort(byte[] a, int left, int right) {
  1549         // Use counting sort on large arrays
  1935         // Use counting sort on large arrays
  1550         if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
  1936         if (right - left > COUNTING_SORT_THRESHOLD_FOR_BYTE) {
  1551             int[] count = new int[NUM_BYTE_VALUES];
  1937             int[] count = new int[NUM_BYTE_VALUES];
  1552 
  1938 
  1553             for (int i = left - 1; ++i <= right; ) {
  1939             for (int i = left - 1; ++i <= right;
  1554                 count[a[i] - Byte.MIN_VALUE]++;
  1940                 count[a[i] - Byte.MIN_VALUE]++
  1555             }
  1941             );
  1556             for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
  1942             for (int i = NUM_BYTE_VALUES, k = right + 1; k > left; ) {
  1557                 while (count[--i] == 0);
  1943                 while (count[--i] == 0);
  1558                 byte value = (byte) (i + Byte.MIN_VALUE);
  1944                 byte value = (byte) (i + Byte.MIN_VALUE);
  1559                 int s = count[i];
  1945                 int s = count[i];
  1560 
  1946 
  1595     public static void sort(float[] a, int left, int right) {
  1981     public static void sort(float[] a, int left, int right) {
  1596         /*
  1982         /*
  1597          * Phase 1: Move NaNs to the end of the array.
  1983          * Phase 1: Move NaNs to the end of the array.
  1598          */
  1984          */
  1599         while (left <= right && Float.isNaN(a[right])) {
  1985         while (left <= right && Float.isNaN(a[right])) {
  1600             right--;
  1986             --right;
  1601         }
  1987         }
  1602         for (int k = right; --k >= left; ) {
  1988         for (int k = right; --k >= left; ) {
  1603             float ak = a[k];
  1989             float ak = a[k];
  1604             if (ak != ak) { // a[k] is NaN
  1990             if (ak != ak) { // a[k] is NaN
  1605                 a[k] = a[right];
  1991                 a[k] = a[right];
  1606                 a[right] = ak;
  1992                 a[right] = ak;
  1607                 right--;
  1993                 --right;
  1608             }
  1994             }
  1609         }
  1995         }
  1610 
  1996 
  1611         /*
  1997         /*
  1612          * Phase 2: Sort everything except NaNs (which are already in place).
  1998          * Phase 2: Sort everything except NaNs (which are already in place).
  1613          */
  1999          */
  1614         sort(a, left, right, true);
  2000         doSort(a, left, right);
  1615 
  2001 
  1616         /*
  2002         /*
  1617          * Phase 3: Place negative zeros before positive zeros.
  2003          * Phase 3: Place negative zeros before positive zeros.
  1618          */
  2004          */
  1619         int hi = right;
  2005         int hi = right;
  1634 
  2020 
  1635         /*
  2021         /*
  1636          * Skip the last negative value (if any) or all leading negative zeros.
  2022          * Skip the last negative value (if any) or all leading negative zeros.
  1637          */
  2023          */
  1638         while (left <= right && Float.floatToRawIntBits(a[left]) < 0) {
  2024         while (left <= right && Float.floatToRawIntBits(a[left]) < 0) {
  1639             left++;
  2025             ++left;
  1640         }
  2026         }
  1641 
  2027 
  1642         /*
  2028         /*
  1643          * Move negative zeros to the beginning of the sub-range.
  2029          * Move negative zeros to the beginning of the sub-range.
  1644          *
  2030          *
  1671             }
  2057             }
  1672         }
  2058         }
  1673     }
  2059     }
  1674 
  2060 
  1675     /**
  2061     /**
       
  2062      * Sorts the specified range of the array.
       
  2063      *
       
  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
       
  2067      */
       
  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);
       
  2072             return;
       
  2073         }
       
  2074 
       
  2075         /*
       
  2076          * Index run[i] is the start of i-th run
       
  2077          * (ascending or descending sequence).
       
  2078          */
       
  2079         int[] run = new int[MAX_RUN_COUNT];
       
  2080         int count = 0; run[0] = left;
       
  2081 
       
  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;
       
  2090                 }
       
  2091             } else { // equal
       
  2092                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
  2093                     if (--m == 0) {
       
  2094                         sort(a, left, right, true);
       
  2095                         return;
       
  2096                     }
       
  2097                 }
       
  2098             }
       
  2099 
       
  2100             /*
       
  2101              * The array is not highly structured,
       
  2102              * use Quicksort instead of merge sort.
       
  2103              */
       
  2104             if (++count == MAX_RUN_COUNT) {
       
  2105                 sort(a, left, right, true);
       
  2106                 return;
       
  2107             }
       
  2108         }
       
  2109 
       
  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
       
  2114             return;
       
  2115         }
       
  2116 
       
  2117         /*
       
  2118          * Create temporary array, which is used for merging.
       
  2119          * Implementation note: variable "right" is increased by 1.
       
  2120          */
       
  2121         float[] b; byte odd = 0;
       
  2122         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
  2123 
       
  2124         if (odd == 0) {
       
  2125             b = a; a = new float[b.length];
       
  2126             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
  2127         } else {
       
  2128             b = new float[a.length];
       
  2129         }
       
  2130 
       
  2131         // Merging
       
  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]) {
       
  2137                         b[i] = a[p++];
       
  2138                     } else {
       
  2139                         b[i] = a[q++];
       
  2140                     }
       
  2141                 }
       
  2142                 run[++last] = hi;
       
  2143             }
       
  2144             if ((count & 1) != 0) {
       
  2145                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
  2146                     b[i] = a[i]
       
  2147                 );
       
  2148                 run[++last] = right;
       
  2149             }
       
  2150             float[] t = a; a = b; b = t;
       
  2151         }
       
  2152     }
       
  2153 
       
  2154     /**
  1676      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  2155      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  1677      *
  2156      *
  1678      * @param a the array to be sorted
  2157      * @param a the array to be sorted
  1679      * @param left the index of the first element, inclusive, to be sorted
  2158      * @param left the index of the first element, inclusive, to be sorted
  1680      * @param right the index of the last element, inclusive, to be sorted
  2159      * @param right the index of the last element, inclusive, to be sorted
  1681      * @param leftmost indicates if this part is the leftmost in the range
  2160      * @param leftmost indicates if this part is the leftmost in the range
  1682      */
  2161      */
  1683     private static void sort(float[] a, int left, int right,boolean leftmost) {
  2162     private static void sort(float[] a, int left, int right, boolean leftmost) {
  1684         int length = right - left + 1;
  2163         int length = right - left + 1;
  1685 
  2164 
  1686         // Use insertion sort on small arrays
  2165         // Use insertion sort on tiny arrays
  1687         if (length < INSERTION_SORT_THRESHOLD) {
  2166         if (length < INSERTION_SORT_THRESHOLD) {
  1688             if (leftmost) {
  2167             if (leftmost) {
  1689                 /*
  2168                 /*
  1690                  * Traditional (without sentinel) insertion sort,
  2169                  * Traditional (without sentinel) insertion sort,
  1691                  * optimized for server VM, is used in case of
  2170                  * optimized for server VM, is used in case of
  1704             } else {
  2183             } else {
  1705                 /*
  2184                 /*
  1706                  * Skip the longest ascending sequence.
  2185                  * Skip the longest ascending sequence.
  1707                  */
  2186                  */
  1708                 do {
  2187                 do {
  1709                     if (left++ >= right) {
  2188                     if (left >= right) {
  1710                         return;
  2189                         return;
  1711                     }
  2190                     }
  1712                 } while (a[left - 1] <= a[left]);
  2191                 } while (a[++left] >= a[left - 1]);
  1713 
  2192 
  1714                 /*
  2193                 /*
  1715                  * Every element from adjoining part plays the role
  2194                  * Every element from adjoining part plays the role
  1716                  * of sentinel, therefore this allows us to avoid the
  2195                  * of sentinel, therefore this allows us to avoid the
  1717                  * left range check on each iteration. Moreover, we use
  2196                  * left range check on each iteration. Moreover, we use
  1718                  * the best improved algorithm, so called pair insertion
  2197                  * the more optimized algorithm, so called pair insertion
  1719                  * sort, which is faster than traditional implementation
  2198                  * sort, which is faster (in the context of Quicksort)
  1720                  * in the context of Dual-Pivot Quicksort.
  2199                  * than traditional implementation of insertion sort.
  1721                  */
  2200                  */
  1722                 for (int k = left--; (left += 2) <= right; ) {
  2201                 for (int k = left; ++left <= right; k = ++left) {
  1723                     float a1, a2; k = left - 1;
  2202                     float a1 = a[k], a2 = a[left];
  1724 
  2203 
  1725                     if (a[k] < a[left]) {
  2204                     if (a1 < a2) {
  1726                         a2 = a[k]; a1 = a[left];
  2205                         a2 = a1; a1 = a[left];
  1727                     } else {
       
  1728                         a1 = a[k]; a2 = a[left];
       
  1729                     }
  2206                     }
  1730                     while (a1 < a[--k]) {
  2207                     while (a1 < a[--k]) {
  1731                         a[k + 2] = a[k];
  2208                         a[k + 2] = a[k];
  1732                     }
  2209                     }
  1733                     a[++k + 1] = a1;
  2210                     a[++k + 1] = a1;
  1780                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  2257                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  1781                 }
  2258                 }
  1782             }
  2259             }
  1783         }
  2260         }
  1784 
  2261 
  1785         /*
       
  1786          * Use the second and fourth of the five sorted elements as pivots.
       
  1787          * These values are inexpensive approximations of the first and
       
  1788          * second terciles of the array. Note that pivot1 <= pivot2.
       
  1789          */
       
  1790         float pivot1 = a[e2];
       
  1791         float pivot2 = a[e4];
       
  1792 
       
  1793         // Pointers
  2262         // Pointers
  1794         int less  = left;  // The index of the first element of center part
  2263         int less  = left;  // The index of the first element of center part
  1795         int great = right; // The index before the first element of right part
  2264         int great = right; // The index before the first element of right part
  1796 
  2265 
  1797         if (pivot1 != pivot2) {
  2266         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
  2267             /*
       
  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.
       
  2271              */
       
  2272             float pivot1 = a[e2];
       
  2273             float pivot2 = a[e4];
       
  2274 
  1798             /*
  2275             /*
  1799              * The first and the last elements to be sorted are moved to the
  2276              * The first and the last elements to be sorted are moved to the
  1800              * locations formerly occupied by the pivots. When partitioning
  2277              * locations formerly occupied by the pivots. When partitioning
  1801              * is complete, the pivots are swapped back into their final
  2278              * is complete, the pivots are swapped back into their final
  1802              * positions, and excluded from subsequent sorting.
  2279              * positions, and excluded from subsequent sorting.
  1837                     /*
  2314                     /*
  1838                      * Here and below we use "a[i] = b; i++;" instead
  2315                      * Here and below we use "a[i] = b; i++;" instead
  1839                      * of "a[i++] = b;" due to performance issue.
  2316                      * of "a[i++] = b;" due to performance issue.
  1840                      */
  2317                      */
  1841                     a[less] = ak;
  2318                     a[less] = ak;
  1842                     less++;
  2319                     ++less;
  1843                 } else if (ak > pivot2) { // Move a[k] to right part
  2320                 } else if (ak > pivot2) { // Move a[k] to right part
  1844                     while (a[great] > pivot2) {
  2321                     while (a[great] > pivot2) {
  1845                         if (great-- == k) {
  2322                         if (great-- == k) {
  1846                             break outer;
  2323                             break outer;
  1847                         }
  2324                         }
  1848                     }
  2325                     }
  1849                     if (a[great] < pivot1) { // a[great] <= pivot2
  2326                     if (a[great] < pivot1) { // a[great] <= pivot2
  1850                         a[k] = a[less];
  2327                         a[k] = a[less];
  1851                         a[less] = a[great];
  2328                         a[less] = a[great];
  1852                         less++;
  2329                         ++less;
  1853                     } else { // pivot1 <= a[great] <= pivot2
  2330                     } else { // pivot1 <= a[great] <= pivot2
  1854                         a[k] = a[great];
  2331                         a[k] = a[great];
  1855                     }
  2332                     }
  1856                     /*
  2333                     /*
  1857                      * Here and below we use "a[i] = b; i--;" instead
  2334                      * Here and below we use "a[i] = b; i--;" instead
  1858                      * of "a[i--] = b;" due to performance issue.
  2335                      * of "a[i--] = b;" due to performance issue.
  1859                      */
  2336                      */
  1860                     a[great] = ak;
  2337                     a[great] = ak;
  1861                     great--;
  2338                     --great;
  1862                 }
  2339                 }
  1863             }
  2340             }
  1864 
  2341 
  1865             // Swap pivots into their final positions
  2342             // Swap pivots into their final positions
  1866             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  2343             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  1877             if (less < e1 && e5 < great) {
  2354             if (less < e1 && e5 < great) {
  1878                 /*
  2355                 /*
  1879                  * Skip elements, which are equal to pivot values.
  2356                  * Skip elements, which are equal to pivot values.
  1880                  */
  2357                  */
  1881                 while (a[less] == pivot1) {
  2358                 while (a[less] == pivot1) {
  1882                     less++;
  2359                     ++less;
  1883                 }
  2360                 }
       
  2361 
  1884                 while (a[great] == pivot2) {
  2362                 while (a[great] == pivot2) {
  1885                     great--;
  2363                     --great;
  1886                 }
  2364                 }
  1887 
  2365 
  1888                 /*
  2366                 /*
  1889                  * Partitioning:
  2367                  * Partitioning:
  1890                  *
  2368                  *
  1908                 for (int k = less - 1; ++k <= great; ) {
  2386                 for (int k = less - 1; ++k <= great; ) {
  1909                     float ak = a[k];
  2387                     float ak = a[k];
  1910                     if (ak == pivot1) { // Move a[k] to left part
  2388                     if (ak == pivot1) { // Move a[k] to left part
  1911                         a[k] = a[less];
  2389                         a[k] = a[less];
  1912                         a[less] = ak;
  2390                         a[less] = ak;
  1913                         less++;
  2391                         ++less;
  1914                     } else if (ak == pivot2) { // Move a[k] to right part
  2392                     } else if (ak == pivot2) { // Move a[k] to right part
  1915                         while (a[great] == pivot2) {
  2393                         while (a[great] == pivot2) {
  1916                             if (great-- == k) {
  2394                             if (great-- == k) {
  1917                                 break outer;
  2395                                 break outer;
  1918                             }
  2396                             }
  1926                              * of different signs. Therefore in float and
  2404                              * of different signs. Therefore in float and
  1927                              * double sorting methods we have to use more
  2405                              * double sorting methods we have to use more
  1928                              * accurate assignment a[less] = a[great].
  2406                              * accurate assignment a[less] = a[great].
  1929                              */
  2407                              */
  1930                             a[less] = a[great];
  2408                             a[less] = a[great];
  1931                             less++;
  2409                             ++less;
  1932                         } else { // pivot1 < a[great] < pivot2
  2410                         } else { // pivot1 < a[great] < pivot2
  1933                             a[k] = a[great];
  2411                             a[k] = a[great];
  1934                         }
  2412                         }
  1935                         a[great] = ak;
  2413                         a[great] = ak;
  1936                         great--;
  2414                         --great;
  1937                     }
  2415                     }
  1938                 }
  2416                 }
  1939             }
  2417             }
  1940 
  2418 
  1941             // Sort center part recursively
  2419             // Sort center part recursively
  1942             sort(a, less, great, false);
  2420             sort(a, less, great, false);
  1943 
  2421 
  1944         } else { // Pivots are equal
  2422         } else { // Partitioning with one pivot
       
  2423             /*
       
  2424              * Use the third of the five sorted elements as pivot.
       
  2425              * This value is inexpensive approximation of the median.
       
  2426              */
       
  2427             float pivot = a[e3];
       
  2428 
  1945             /*
  2429             /*
  1946              * Partitioning degenerates to the traditional 3-way
  2430              * Partitioning degenerates to the traditional 3-way
  1947              * (or "Dutch National Flag") schema:
  2431              * (or "Dutch National Flag") schema:
  1948              *
  2432              *
  1949              *   left part    center part              right part
  2433              *   left part    center part              right part
  1961              *   all in (great, right) > pivot
  2445              *   all in (great, right) > pivot
  1962              *
  2446              *
  1963              * Pointer k is the first index of ?-part.
  2447              * Pointer k is the first index of ?-part.
  1964              */
  2448              */
  1965             for (int k = less; k <= great; ++k) {
  2449             for (int k = less; k <= great; ++k) {
  1966                 if (a[k] == pivot1) {
  2450                 if (a[k] == pivot) {
  1967                     continue;
  2451                     continue;
  1968                 }
  2452                 }
  1969                 float ak = a[k];
  2453                 float ak = a[k];
  1970                 if (ak < pivot1) { // Move a[k] to left part
  2454                 if (ak < pivot) { // Move a[k] to left part
  1971                     a[k] = a[less];
  2455                     a[k] = a[less];
  1972                     a[less] = ak;
  2456                     a[less] = ak;
  1973                     less++;
  2457                     ++less;
  1974                 } else { // a[k] > pivot1 - Move a[k] to right part
  2458                 } else { // a[k] > pivot - Move a[k] to right part
  1975                     while (a[great] > pivot1) {
  2459                     while (a[great] > pivot) {
  1976                         great--;
  2460                         --great;
  1977                     }
  2461                     }
  1978                     if (a[great] < pivot1) { // a[great] <= pivot1
  2462                     if (a[great] < pivot) { // a[great] <= pivot
  1979                         a[k] = a[less];
  2463                         a[k] = a[less];
  1980                         a[less] = a[great];
  2464                         a[less] = a[great];
  1981                         less++;
  2465                         ++less;
  1982                     } else { // a[great] == pivot1
  2466                     } else { // a[great] == pivot
  1983                         /*
  2467                         /*
  1984                          * Even though a[great] equals to pivot1, the
  2468                          * Even though a[great] equals to pivot, the
  1985                          * assignment a[k] = pivot1 may be incorrect,
  2469                          * assignment a[k] = pivot may be incorrect,
  1986                          * if a[great] and pivot1 are floating-point
  2470                          * if a[great] and pivot are floating-point
  1987                          * zeros of different signs. Therefore in float
  2471                          * zeros of different signs. Therefore in float
  1988                          * and double sorting methods we have to use
  2472                          * and double sorting methods we have to use
  1989                          * more accurate assignment a[k] = a[great].
  2473                          * more accurate assignment a[k] = a[great].
  1990                          */
  2474                          */
  1991                         a[k] = a[great];
  2475                         a[k] = a[great];
  1992                     }
  2476                     }
  1993                     a[great] = ak;
  2477                     a[great] = ak;
  1994                     great--;
  2478                     --great;
  1995                 }
  2479                 }
  1996             }
  2480             }
  1997 
  2481 
  1998             /*
  2482             /*
  1999              * Sort left and right parts recursively.
  2483              * Sort left and right parts recursively.
  2024     public static void sort(double[] a, int left, int right) {
  2508     public static void sort(double[] a, int left, int right) {
  2025         /*
  2509         /*
  2026          * Phase 1: Move NaNs to the end of the array.
  2510          * Phase 1: Move NaNs to the end of the array.
  2027          */
  2511          */
  2028         while (left <= right && Double.isNaN(a[right])) {
  2512         while (left <= right && Double.isNaN(a[right])) {
  2029             right--;
  2513             --right;
  2030         }
  2514         }
  2031         for (int k = right; --k >= left; ) {
  2515         for (int k = right; --k >= left; ) {
  2032             double ak = a[k];
  2516             double ak = a[k];
  2033             if (ak != ak) { // a[k] is NaN
  2517             if (ak != ak) { // a[k] is NaN
  2034                 a[k] = a[right];
  2518                 a[k] = a[right];
  2035                 a[right] = ak;
  2519                 a[right] = ak;
  2036                 right--;
  2520                 --right;
  2037             }
  2521             }
  2038         }
  2522         }
  2039 
  2523 
  2040         /*
  2524         /*
  2041          * Phase 2: Sort everything except NaNs (which are already in place).
  2525          * Phase 2: Sort everything except NaNs (which are already in place).
  2042          */
  2526          */
  2043         sort(a, left, right, true);
  2527         doSort(a, left, right);
  2044 
  2528 
  2045         /*
  2529         /*
  2046          * Phase 3: Place negative zeros before positive zeros.
  2530          * Phase 3: Place negative zeros before positive zeros.
  2047          */
  2531          */
  2048         int hi = right;
  2532         int hi = right;
  2063 
  2547 
  2064         /*
  2548         /*
  2065          * Skip the last negative value (if any) or all leading negative zeros.
  2549          * Skip the last negative value (if any) or all leading negative zeros.
  2066          */
  2550          */
  2067         while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) {
  2551         while (left <= right && Double.doubleToRawLongBits(a[left]) < 0) {
  2068             left++;
  2552             ++left;
  2069         }
  2553         }
  2070 
  2554 
  2071         /*
  2555         /*
  2072          * Move negative zeros to the beginning of the sub-range.
  2556          * Move negative zeros to the beginning of the sub-range.
  2073          *
  2557          *
  2100             }
  2584             }
  2101         }
  2585         }
  2102     }
  2586     }
  2103 
  2587 
  2104     /**
  2588     /**
       
  2589      * Sorts the specified range of the array.
       
  2590      *
       
  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
       
  2594      */
       
  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);
       
  2599             return;
       
  2600         }
       
  2601 
       
  2602         /*
       
  2603          * Index run[i] is the start of i-th run
       
  2604          * (ascending or descending sequence).
       
  2605          */
       
  2606         int[] run = new int[MAX_RUN_COUNT];
       
  2607         int count = 0; run[0] = left;
       
  2608 
       
  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;
       
  2617                 }
       
  2618             } else { // equal
       
  2619                 for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
       
  2620                     if (--m == 0) {
       
  2621                         sort(a, left, right, true);
       
  2622                         return;
       
  2623                     }
       
  2624                 }
       
  2625             }
       
  2626 
       
  2627             /*
       
  2628              * The array is not highly structured,
       
  2629              * use Quicksort instead of merge sort.
       
  2630              */
       
  2631             if (++count == MAX_RUN_COUNT) {
       
  2632                 sort(a, left, right, true);
       
  2633                 return;
       
  2634             }
       
  2635         }
       
  2636 
       
  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
       
  2641             return;
       
  2642         }
       
  2643 
       
  2644         /*
       
  2645          * Create temporary array, which is used for merging.
       
  2646          * Implementation note: variable "right" is increased by 1.
       
  2647          */
       
  2648         double[] b; byte odd = 0;
       
  2649         for (int n = 1; (n <<= 1) < count; odd ^= 1);
       
  2650 
       
  2651         if (odd == 0) {
       
  2652             b = a; a = new double[b.length];
       
  2653             for (int i = left - 1; ++i < right; a[i] = b[i]);
       
  2654         } else {
       
  2655             b = new double[a.length];
       
  2656         }
       
  2657 
       
  2658         // Merging
       
  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]) {
       
  2664                         b[i] = a[p++];
       
  2665                     } else {
       
  2666                         b[i] = a[q++];
       
  2667                     }
       
  2668                 }
       
  2669                 run[++last] = hi;
       
  2670             }
       
  2671             if ((count & 1) != 0) {
       
  2672                 for (int i = right, lo = run[count - 1]; --i >= lo;
       
  2673                     b[i] = a[i]
       
  2674                 );
       
  2675                 run[++last] = right;
       
  2676             }
       
  2677             double[] t = a; a = b; b = t;
       
  2678         }
       
  2679     }
       
  2680 
       
  2681     /**
  2105      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  2682      * Sorts the specified range of the array by Dual-Pivot Quicksort.
  2106      *
  2683      *
  2107      * @param a the array to be sorted
  2684      * @param a the array to be sorted
  2108      * @param left the index of the first element, inclusive, to be sorted
  2685      * @param left the index of the first element, inclusive, to be sorted
  2109      * @param right the index of the last element, inclusive, to be sorted
  2686      * @param right the index of the last element, inclusive, to be sorted
  2110      * @param leftmost indicates if this part is the leftmost in the range
  2687      * @param leftmost indicates if this part is the leftmost in the range
  2111      */
  2688      */
  2112     private static void sort(double[] a, int left,int right,boolean leftmost) {
  2689     private static void sort(double[] a, int left, int right, boolean leftmost) {
  2113         int length = right - left + 1;
  2690         int length = right - left + 1;
  2114 
  2691 
  2115         // Use insertion sort on small arrays
  2692         // Use insertion sort on tiny arrays
  2116         if (length < INSERTION_SORT_THRESHOLD) {
  2693         if (length < INSERTION_SORT_THRESHOLD) {
  2117             if (leftmost) {
  2694             if (leftmost) {
  2118                 /*
  2695                 /*
  2119                  * Traditional (without sentinel) insertion sort,
  2696                  * Traditional (without sentinel) insertion sort,
  2120                  * optimized for server VM, is used in case of
  2697                  * optimized for server VM, is used in case of
  2133             } else {
  2710             } else {
  2134                 /*
  2711                 /*
  2135                  * Skip the longest ascending sequence.
  2712                  * Skip the longest ascending sequence.
  2136                  */
  2713                  */
  2137                 do {
  2714                 do {
  2138                     if (left++ >= right) {
  2715                     if (left >= right) {
  2139                         return;
  2716                         return;
  2140                     }
  2717                     }
  2141                 } while (a[left - 1] <= a[left]);
  2718                 } while (a[++left] >= a[left - 1]);
  2142 
  2719 
  2143                 /*
  2720                 /*
  2144                  * Every element from adjoining part plays the role
  2721                  * Every element from adjoining part plays the role
  2145                  * of sentinel, therefore this allows us to avoid the
  2722                  * of sentinel, therefore this allows us to avoid the
  2146                  * left range check on each iteration. Moreover, we use
  2723                  * left range check on each iteration. Moreover, we use
  2147                  * the best improved algorithm, so called pair insertion
  2724                  * the more optimized algorithm, so called pair insertion
  2148                  * sort, which is faster than traditional implementation
  2725                  * sort, which is faster (in the context of Quicksort)
  2149                  * in the context of Dual-Pivot Quicksort.
  2726                  * than traditional implementation of insertion sort.
  2150                  */
  2727                  */
  2151                 for (int k = left--; (left += 2) <= right; ) {
  2728                 for (int k = left; ++left <= right; k = ++left) {
  2152                     double a1, a2; k = left - 1;
  2729                     double a1 = a[k], a2 = a[left];
  2153 
  2730 
  2154                     if (a[k] < a[left]) {
  2731                     if (a1 < a2) {
  2155                         a2 = a[k]; a1 = a[left];
  2732                         a2 = a1; a1 = a[left];
  2156                     } else {
       
  2157                         a1 = a[k]; a2 = a[left];
       
  2158                     }
  2733                     }
  2159                     while (a1 < a[--k]) {
  2734                     while (a1 < a[--k]) {
  2160                         a[k + 2] = a[k];
  2735                         a[k + 2] = a[k];
  2161                     }
  2736                     }
  2162                     a[++k + 1] = a1;
  2737                     a[++k + 1] = a1;
  2209                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  2784                     if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
  2210                 }
  2785                 }
  2211             }
  2786             }
  2212         }
  2787         }
  2213 
  2788 
  2214         /*
       
  2215          * Use the second and fourth of the five sorted elements as pivots.
       
  2216          * These values are inexpensive approximations of the first and
       
  2217          * second terciles of the array. Note that pivot1 <= pivot2.
       
  2218          */
       
  2219         double pivot1 = a[e2];
       
  2220         double pivot2 = a[e4];
       
  2221 
       
  2222         // Pointers
  2789         // Pointers
  2223         int less  = left;  // The index of the first element of center part
  2790         int less  = left;  // The index of the first element of center part
  2224         int great = right; // The index before the first element of right part
  2791         int great = right; // The index before the first element of right part
  2225 
  2792 
  2226         if (pivot1 != pivot2) {
  2793         if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
       
  2794             /*
       
  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.
       
  2798              */
       
  2799             double pivot1 = a[e2];
       
  2800             double pivot2 = a[e4];
       
  2801 
  2227             /*
  2802             /*
  2228              * The first and the last elements to be sorted are moved to the
  2803              * The first and the last elements to be sorted are moved to the
  2229              * locations formerly occupied by the pivots. When partitioning
  2804              * locations formerly occupied by the pivots. When partitioning
  2230              * is complete, the pivots are swapped back into their final
  2805              * is complete, the pivots are swapped back into their final
  2231              * positions, and excluded from subsequent sorting.
  2806              * positions, and excluded from subsequent sorting.
  2266                     /*
  2841                     /*
  2267                      * Here and below we use "a[i] = b; i++;" instead
  2842                      * Here and below we use "a[i] = b; i++;" instead
  2268                      * of "a[i++] = b;" due to performance issue.
  2843                      * of "a[i++] = b;" due to performance issue.
  2269                      */
  2844                      */
  2270                     a[less] = ak;
  2845                     a[less] = ak;
  2271                     less++;
  2846                     ++less;
  2272                 } else if (ak > pivot2) { // Move a[k] to right part
  2847                 } else if (ak > pivot2) { // Move a[k] to right part
  2273                     while (a[great] > pivot2) {
  2848                     while (a[great] > pivot2) {
  2274                         if (great-- == k) {
  2849                         if (great-- == k) {
  2275                             break outer;
  2850                             break outer;
  2276                         }
  2851                         }
  2277                     }
  2852                     }
  2278                     if (a[great] < pivot1) { // a[great] <= pivot2
  2853                     if (a[great] < pivot1) { // a[great] <= pivot2
  2279                         a[k] = a[less];
  2854                         a[k] = a[less];
  2280                         a[less] = a[great];
  2855                         a[less] = a[great];
  2281                         less++;
  2856                         ++less;
  2282                     } else { // pivot1 <= a[great] <= pivot2
  2857                     } else { // pivot1 <= a[great] <= pivot2
  2283                         a[k] = a[great];
  2858                         a[k] = a[great];
  2284                     }
  2859                     }
  2285                     /*
  2860                     /*
  2286                      * Here and below we use "a[i] = b; i--;" instead
  2861                      * Here and below we use "a[i] = b; i--;" instead
  2287                      * of "a[i--] = b;" due to performance issue.
  2862                      * of "a[i--] = b;" due to performance issue.
  2288                      */
  2863                      */
  2289                     a[great] = ak;
  2864                     a[great] = ak;
  2290                     great--;
  2865                     --great;
  2291                 }
  2866                 }
  2292             }
  2867             }
  2293 
  2868 
  2294             // Swap pivots into their final positions
  2869             // Swap pivots into their final positions
  2295             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  2870             a[left]  = a[less  - 1]; a[less  - 1] = pivot1;
  2306             if (less < e1 && e5 < great) {
  2881             if (less < e1 && e5 < great) {
  2307                 /*
  2882                 /*
  2308                  * Skip elements, which are equal to pivot values.
  2883                  * Skip elements, which are equal to pivot values.
  2309                  */
  2884                  */
  2310                 while (a[less] == pivot1) {
  2885                 while (a[less] == pivot1) {
  2311                     less++;
  2886                     ++less;
  2312                 }
  2887                 }
       
  2888 
  2313                 while (a[great] == pivot2) {
  2889                 while (a[great] == pivot2) {
  2314                     great--;
  2890                     --great;
  2315                 }
  2891                 }
  2316 
  2892 
  2317                 /*
  2893                 /*
  2318                  * Partitioning:
  2894                  * Partitioning:
  2319                  *
  2895                  *
  2337                 for (int k = less - 1; ++k <= great; ) {
  2913                 for (int k = less - 1; ++k <= great; ) {
  2338                     double ak = a[k];
  2914                     double ak = a[k];
  2339                     if (ak == pivot1) { // Move a[k] to left part
  2915                     if (ak == pivot1) { // Move a[k] to left part
  2340                         a[k] = a[less];
  2916                         a[k] = a[less];
  2341                         a[less] = ak;
  2917                         a[less] = ak;
  2342                         less++;
  2918                         ++less;
  2343                     } else if (ak == pivot2) { // Move a[k] to right part
  2919                     } else if (ak == pivot2) { // Move a[k] to right part
  2344                         while (a[great] == pivot2) {
  2920                         while (a[great] == pivot2) {
  2345                             if (great-- == k) {
  2921                             if (great-- == k) {
  2346                                 break outer;
  2922                                 break outer;
  2347                             }
  2923                             }
  2355                              * of different signs. Therefore in float and
  2931                              * of different signs. Therefore in float and
  2356                              * double sorting methods we have to use more
  2932                              * double sorting methods we have to use more
  2357                              * accurate assignment a[less] = a[great].
  2933                              * accurate assignment a[less] = a[great].
  2358                              */
  2934                              */
  2359                             a[less] = a[great];
  2935                             a[less] = a[great];
  2360                             less++;
  2936                             ++less;
  2361                         } else { // pivot1 < a[great] < pivot2
  2937                         } else { // pivot1 < a[great] < pivot2
  2362                             a[k] = a[great];
  2938                             a[k] = a[great];
  2363                         }
  2939                         }
  2364                         a[great] = ak;
  2940                         a[great] = ak;
  2365                         great--;
  2941                         --great;
  2366                     }
  2942                     }
  2367                 }
  2943                 }
  2368             }
  2944             }
  2369 
  2945 
  2370             // Sort center part recursively
  2946             // Sort center part recursively
  2371             sort(a, less, great, false);
  2947             sort(a, less, great, false);
  2372 
  2948 
  2373         } else { // Pivots are equal
  2949         } else { // Partitioning with one pivot
       
  2950             /*
       
  2951              * Use the third of the five sorted elements as pivot.
       
  2952              * This value is inexpensive approximation of the median.
       
  2953              */
       
  2954             double pivot = a[e3];
       
  2955 
  2374             /*
  2956             /*
  2375              * Partitioning degenerates to the traditional 3-way
  2957              * Partitioning degenerates to the traditional 3-way
  2376              * (or "Dutch National Flag") schema:
  2958              * (or "Dutch National Flag") schema:
  2377              *
  2959              *
  2378              *   left part    center part              right part
  2960              *   left part    center part              right part
  2390              *   all in (great, right) > pivot
  2972              *   all in (great, right) > pivot
  2391              *
  2973              *
  2392              * Pointer k is the first index of ?-part.
  2974              * Pointer k is the first index of ?-part.
  2393              */
  2975              */
  2394             for (int k = less; k <= great; ++k) {
  2976             for (int k = less; k <= great; ++k) {
  2395                 if (a[k] == pivot1) {
  2977                 if (a[k] == pivot) {
  2396                     continue;
  2978                     continue;
  2397                 }
  2979                 }
  2398                 double ak = a[k];
  2980                 double ak = a[k];
  2399                 if (ak < pivot1) { // Move a[k] to left part
  2981                 if (ak < pivot) { // Move a[k] to left part
  2400                     a[k] = a[less];
  2982                     a[k] = a[less];
  2401                     a[less] = ak;
  2983                     a[less] = ak;
  2402                     less++;
  2984                     ++less;
  2403                 } else { // a[k] > pivot1 - Move a[k] to right part
  2985                 } else { // a[k] > pivot - Move a[k] to right part
  2404                     while (a[great] > pivot1) {
  2986                     while (a[great] > pivot) {
  2405                         great--;
  2987                         --great;
  2406                     }
  2988                     }
  2407                     if (a[great] < pivot1) { // a[great] <= pivot1
  2989                     if (a[great] < pivot) { // a[great] <= pivot
  2408                         a[k] = a[less];
  2990                         a[k] = a[less];
  2409                         a[less] = a[great];
  2991                         a[less] = a[great];
  2410                         less++;
  2992                         ++less;
  2411                     } else { // a[great] == pivot1
  2993                     } else { // a[great] == pivot
  2412                         /*
  2994                         /*
  2413                          * Even though a[great] equals to pivot1, the
  2995                          * Even though a[great] equals to pivot, the
  2414                          * assignment a[k] = pivot1 may be incorrect,
  2996                          * assignment a[k] = pivot may be incorrect,
  2415                          * if a[great] and pivot1 are floating-point
  2997                          * if a[great] and pivot are floating-point
  2416                          * zeros of different signs. Therefore in float
  2998                          * zeros of different signs. Therefore in float
  2417                          * and double sorting methods we have to use
  2999                          * and double sorting methods we have to use
  2418                          * more accurate assignment a[k] = a[great].
  3000                          * more accurate assignment a[k] = a[great].
  2419                          */
  3001                          */
  2420                         a[k] = a[great];
  3002                         a[k] = a[great];
  2421                     }
  3003                     }
  2422                     a[great] = ak;
  3004                     a[great] = ak;
  2423                     great--;
  3005                     --great;
  2424                 }
  3006                 }
  2425             }
  3007             }
  2426 
  3008 
  2427             /*
  3009             /*
  2428              * Sort left and right parts recursively.
  3010              * Sort left and right parts recursively.