更新快排中的partition

这一次是将partition 过程中, 维护三个区域。 <x   =x  >x  三区域。 还有个待定的区域。

 

    /*
     * 将数组划分为三个分区, 小于arr[R], 等于arr[R], 大于arr[R];
     */
    public static int[] partion01(int arr[], int L, int R){
        int less = L-1;
        int more = R;
        int cur = L;
        while( cur < more ){
            if( arr[cur] < arr[R] ){
                swap(arr, ++less, cur++);
            }else if( arr[cur] > arr[R] ){
                swap(arr, --more, cur);
            }else {
                cur++;
            }
        }
        swap(arr, more, R);
        return new int[] {less+1,more};
        
    }

 

posted @ 2018-12-21 09:33  清湾大威少  阅读(226)  评论(0编辑  收藏  举报