
When the sequence of elements to be sorted in a large number of repeat ordering code, the efficiency of fast simple sorting algorithm will be reduced to very low. The idea is a direct sort column will be divided into three sequences: one is smaller than the reference element ordering code ordering code; part of the reference element ordering code equivalent; part is larger than the reference element ordering code
Quicksort threeway division
But if we direct accordingly thought to write algorithm, then we will face great difficulties. Element reference elements equivalent in the end how much? As well as how best to quickly and efficiently determine the division of the border? So, to complete this threeway division is very difficult, even more complex than the twoway division process.
We can achieve threeway division based on the following idea: the division process, you will encounter when scanning left subsequence with the reference element ordering code equivalent elements into the leftmost sequence will encounter the right sequence Sort the elements of the reference element and the equivalent code sequence into the far right.
Threeway division of implementation
When scanning two pointers meet, the exact location of the sort code equal elements will know. Then we just sort of all the elements and the reference element equivalent code scanning pointer elements sequentially exchange, we can get the results of a threeway division.
This method not only effectively deal with the elements of the sequence to be sorted duplicate values issues, and in the absence of duplicate values it can maintain the performance of the original algorithm. Its advantages are:
First, if there is no sequence of elements duplicate values, this method can maintain the efficiency of the algorithm, because there is no extra work to do;
Second, in each division of the words in the process, it can be sorted with a reference element code element equal split out all elements have the same sort code division multiple values will not participate.
Code
private void quickSort (int [] a, int left, int right) {
if (right < = left)
return;
/ *
* Work pointer
* P is equal to the position of pivot point to the left of the sequence of elements
* Q points to the right of the equal position of Pivot sequence elements
* Element i pointing from left to right when the sweep plane
Point element j from right to left when scanning *
* /
int p, q, i, j;
int pivot; // anchor
i = p = left;
j = q = right  1;
/ *
* Always take each element of a sequence rightmost anchor
* /
pivot = a [right];
while (true) {
/ *
* Pointer i work from right to left constantly scanning, looking less than or equal to the anchor element elements
* /
while (i < right && a [i] < = pivot) {
/ *
* Locate the anchor element element equal to its exchange position indicated by p
* /
if (a [i] == pivot) {
swap (a, i, p);
p ++;
}
i ++;
}
/ *
* Work pointer j constantly scan from left to right, looking for an anchor element greater than or equal elements
* /
while (left < = j && a [j]> = pivot) {
/ *
* Locate the anchor element element equal to its exchange position indicated by q
* /
if (a [j] == pivot) {
swap (a, j, q);
q;
}
j;
}
/ *
* If you work two pointers i j meet the end of a trip traversal
* /
if (i> = j)
break;
/ *
* The left and the right side is greater than the pivot element is less than the pivot element exchange
* /
swap (a, i, j);
i ++;
j;
}
/ *
* Because i work pointer points to the next element to deal with the current element
* Therefore need to return to the actual position of the current element, then the exchange will be equal to the sequence of intermediate pivot element
* /
i;
p;
while (p> = left) {
swap (a, i, p);
i;
p;
}
/ *
* Because the work pointer j points to a need to deal with the elements of the current element
* Therefore need to return to the actual position of the current element, then the exchange will be equal to the sequence of intermediate pivot element
* /
j ++;
q ++;
while (q < = right) {
swap (a, j, q);
j ++;
q ++;
}
/ *
* Recursive traversal sequences around
* /
quickSort (a, left, i);
quickSort (a, j, right);
}
private void quick (int [] a) {
if (a.length> 0) {
quickSort (a, 0, a.length  1);
}
}
private void swap (int [] arr, int a, int b) {
int temp = arr [a];
arr [a] = arr [b];
arr [b] = temp;
} 


