代码改变世界

快排算法

2020-11-16 00:55  woshihuangrulin  阅读(96)  评论(0编辑  收藏  举报
public static int partition(int[] arr,int left,int right){
        int pivot = arr[left];
        while(left < right){
            while(left<right && arr[right] >= pivot)
                right--;
            arr[left] = arr[right];
            while(left < right && arr[left]<= pivot)
                left++;
            arr[right] = arr[left];
        }
        arr[left] = pivot;
        return left;
    }

 

找到一个数,通常是左边或者右边的数,使用此数将数组分为两部分,左边的小,右边的大