快速排序 练手

package Algorithm;

public class Day1218 {
    public static void quickSort(int[] arr,int low,int high)
    {
        if(low>=high)
        {
            return;
        }
        int key=arr[low],temp;
        int start=low,end=high;
        while(start<end)
        {
            while(start<end && key<=arr[end])
            {
                end--;
            }
            if(start<end)
            {
                temp=arr[end];
                arr[end]=arr[start];
                arr[start]=temp;
            }
            while(start<end && key>=arr[start])
            {
                start++;
            }
            if(start<end)
            {
                temp=arr[end];
                arr[end]=arr[start];
                arr[start]=temp;
            }
        }
        //这时候start和end是一样的,指向同一个数
        quickSort(arr,low,start-1);
        quickSort(arr,end+1,high);
    }

    public static void main(String[] args) {
        int[] a = {10,7,2,1,12,8,9,15};
        quickSort(a,0,a.length-1);
        for(int s:a)
        System.out.println(s);
    }
}



你可能感兴趣的:(快速排序 练手)