三分法 three-way partitioning

参考: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 ij and n, maintaining the invariant that i ≤ jn holds the boundary of numbers greater than midj 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 将数组分为 <, =, >的三部分。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值