* Skip the longest ascending sequence.
} while (a[++left] >= a[left - 1]);
* Every element from adjoining part plays the role
* of sentinel, therefore this allows us to avoid the
* left range check on each iteration. Moreover, we use
* the more optimized algorithm, so called pair insertion
* sort, which is faster (in the context of Quicksort)
* than traditional implementation of insertion sort.
for (int k = left; ++left <= right; k = ++left) {
int a1 = a[k], a2 = a[left];
while (last < a[--right]) {
// Inexpensive approximation of length / 7
int seventh = (length >> 3) + (length >> 6) + 1;
* Sort five evenly spaced elements around (and including) the
* center element in the range. These elements will be used for
* pivot selection as described below. The choice for spacing
* these elements was empirically determined to work well on
* a wide variety of inputs.
int e3 = (left + right) >>> 1; // The midpoint
// Sort these elements using insertion sort
if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
int less = left; // The index of the first element of center part
int great = right; // The index before the first element of right part
if (a[e1] != a[e2] && a[e2] != a[e3] && a[e3] != a[e4] && a[e4] != a[e5]) {
* Use the second and fourth of the five sorted elements as pivots.
* These values are inexpensive approximations of the first and
* second terciles of the array. Note that pivot1 <= pivot2.
* The first and the last elements to be sorted are moved to the
* locations formerly occupied by the pivots. When partitioning
* is complete, the pivots are swapped back into their final
* positions, and excluded from subsequent sorting.
* Skip elements, which are less or greater than pivot values.
while (a[++less] < pivot1);
while (a[--great] > pivot2);
* left part center part right part
* +--------------------------------------------------------------+
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
* +--------------------------------------------------------------+
* all in (left, less) < pivot1
* pivot1 <= all in [less, k) <= pivot2
* all in (great, right) > pivot2
* Pointer k is the first index of ?-part.
for (int k = less - 1; ++k <= great; ) {
if (ak < pivot1) { // Move a[k] to left part
* Here and below we use "a[i] = b; i++;" instead
* of "a[i++] = b;" due to performance issue.
} else if (ak > pivot2) { // Move a[k] to right part
while (a[great] > pivot2) {
if (a[great] < pivot1) { // a[great] <= pivot2
} else { // pivot1 <= a[great] <= pivot2
* Here and below we use "a[i] = b; i--;" instead
* of "a[i--] = b;" due to performance issue.
// Swap pivots into their final positions
a[left] = a[less - 1]; a[less - 1] = pivot1;
a[right] = a[great + 1]; a[great + 1] = pivot2;
// Sort left and right parts recursively, excluding known pivots
sort(a, left, less - 2, leftmost);
sort(a, great + 2, right, false);
* If center part is too large (comprises > 4/7 of the array),
* swap internal pivot values to ends.
if (less < e1 && e5 < great) {
* Skip elements, which are equal to pivot values.
while (a[less] == pivot1) {
while (a[great] == pivot2) {
* left part center part right part
* +----------------------------------------------------------+
* | == pivot1 | pivot1 < && < pivot2 | ? | == pivot2 |
* +----------------------------------------------------------+
* all in (*, less) == pivot1
* pivot1 < all in [less, k) < pivot2
* all in (great, *) == pivot2
* Pointer k is the first index of ?-part.
for (int k = less - 1; ++k <= great; ) {
if (ak == pivot1) { // Move a[k] to left part
} else if (ak == pivot2) { // Move a[k] to right part
while (a[great] == pivot2) {
if (a[great] == pivot1) { // a[great] < pivot2
* Even though a[great] equals to pivot1, the
* assignment a[less] = pivot1 may be incorrect,
* if a[great] and pivot1 are floating-point zeros
* of different signs. Therefore in float and
* double sorting methods we have to use more
* accurate assignment a[less] = a[great].
} else { // pivot1 < a[great] < pivot2
// Sort center part recursively
sort(a, less, great, false);
} else { // Partitioning with one pivot
* Use the third of the five sorted elements as pivot.
* This value is inexpensive approximation of the median.
* Partitioning degenerates to the traditional 3-way
* (or "Dutch National Flag") schema:
* left part center part right part
* +-------------------------------------------------+
* | < pivot | == pivot | ? | > pivot |
* +-------------------------------------------------+
* all in (left, less) < pivot
* all in [less, k) == pivot
* all in (great, right) > pivot
* Pointer k is the first index of ?-part.
for (int k = less; k <= great; ++k) {
if (ak < pivot) { // Move a[k] to left part
} else { // a[k] > pivot - Move a[k] to right part
while (a[great] > pivot) {
if (a[great] < pivot) { // a[great] <= pivot
} else { // a[great] == pivot
* Even though a[great] equals to pivot, the
* assignment a[k] = pivot may be incorrect,
* if a[great] and pivot are floating-point
* zeros of different signs. Therefore in float
* and double sorting methods we have to use
* more accurate assignment a[k] = a[great].
* Sort left and right parts recursively.
* All elements from center part are equal
* and, therefore, already sorted.
sort(a, left, less - 1, leftmost);
sort(a, great + 1, right, false);