参考:http://blog.jobbole.com/105219/
先考虑这样一个问题,给定红、白、蓝三种颜色的小球若干个,将其排成一列,使相同颜色的小球相邻,三种颜色先后顺序为红,白,蓝。这就是经典的 Dutch national flag problem。
我们可以针对红,蓝,白三种颜色的球分别计数,然后根据计数结果来重新放球。不过如果我们将问题进一步抽象,也就是说将一个数组按照某个target值分为三部分,使得左边部分的值小于 target,中间部分等于 target,右边部分大于 target,这样就不能再用简单的计数来确定排序后的结果。这时候,就可以用到另一种 partition 算法:three-way-partition。它的思路稍微复杂一点,用三个指针将数组分为四个部分,通过一次扫描最终将数组分为 <,=,> 的三部分,如下图所示:
Pseudocode[edit]
The following pseudocode for three-way partitioning assumes zero-based array indexing. It uses three indices i, j and n, maintaining the invariant that i ≤ j. n holds the boundary of numbers greater than mid. j is the position of number under consideration. And i is the boundary for the numbers lesser than the mid.
procedure three-way-partition(A : array of values, mid : value): i ← 0 j ← 0 n ← size of A - 1 while j ≤ n: if A[j] < mid: swap A[i] and A[j] i ← i + 1 j ← j + 1 else if A[j] > mid: swap A[j] and A[n] n ← n - 1 else: j ← j + 1
Note that j will be greater than i only if the mid is hit.
可以结合下面代码来理解具体的逻辑:
// Assume target is in the arr.
voidthree_way_partition(vector<int>&arr,inttarget){
intnext_less_pos=0,next_bigger_pos=arr.size()-1;
intnext_scan_pos=0;
while(next_scan_pos<=next_bigger_pos){
if(arr[next_scan_pos]<target){
swap(arr[next_scan_pos++],arr[next_less_pos++]);
}
elseif(arr[next_scan_pos]>target){
swap(arr[next_scan_pos],arr[next_bigger_pos--]);
}
else{
next_scan_pos++;
}
}
}
这里的主要思想就是在一遍扫描中,通过交换不同位置的数字,使得数组最终可以维持一定的顺序,和前面快排中用到的 partition 思想一致。区别在于快排按照 pivot 将数组分为两部分,左右部分中的值都可能等于 pivot,而 three-way-partition 将数组分为 <, =, >的三部分。