chaozhiwen / LeetCode-in-Java

1 stars 1 forks source link

快速排序 #3

Open chaozhiwen opened 5 years ago

chaozhiwen commented 5 years ago
    private void sort(int[] nums,int lo,int hi){
        if(lo>=hi) return;
        int i=lo,j=hi;
        while(i<j){
            while(i<j&&nums[j]>=nums[lo])
                j--;
            while(i<j&&nums[i]<=nums[lo])
                i++;
            if(i<j)
                swap(nums,i,j);
        }
        swap(nums,lo,i);        
        sort(nums,lo,i-1);
        sort(nums,i+1,hi);
    }

    private void swap(int[] nums,int i,int j){
        int tmp=nums[i];
        nums[i]=nums[j];
        nums[j]=tmp;
    }