算法复习之快排

import java.util.Arrays;
public class QuickSort {
    public static void sort(int[]src,int start,int end){
        if(start>=end||start<0){
            return ;
        }
        int aim = src[start];
        int before=start+1,after=end;
        while(before<after){
            if(src[before]>aim){
                while(after>before&&src[after]>=aim){
                    after--;
                }
                if(src[after]<aim){
                    exchange(src,after,before);
                }else{
                    break;
                }
            }
            before++;
        }
        System.out.println(start+" "+end +" "+Arrays.toString(src));
        if(src[before]>aim){
            before--;
        }
        exchange(src,before,start);

        sort(src,start,before-1);
        sort(src,before+1,end);
    }
    public static void exchange(int src[],int i,int j){
        int temp  = src[i];
        src[i] = src[j];
        src[j] = temp;
    }
    public static void main(String[] args) {
        int[] src = new int[]{5,2,8,3,5,0};
        sort(src,0,src.length-1);
        for (int i : src) {
            System.out.println(i);
        }
    }
}

你可能感兴趣的:(快排)