快速排序

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] a={4,3,2,5,8,6};
        sort(a, 0, 5);
        System.out.println(Arrays.toString(a));
    }
    public static int partition(int []array,int low,int high){
        //固定的切分方式
        int key=array[low];
        while(low<high){
            while(array[high]>=key&&high>low){//从后半部分向前扫描
                high--;
            }
            array[low]=array[high];
            while(array[low]<=key&&high>low){//{从前半部分向后扫描
                low++;
            }
            array[high]=array[low];
        }
        array[high]=key;
        return high;
    }
    
    public static void sort(int[] array,int low ,int high){
        if(low>=high){
            return ;
        }
        int index=partition(array,low,high);
        sort(array,low,index-1);
        sort(array,index+1,high); 
    }
}

 [2, 3, 4, 5, 6, 8]

posted @ 2017-08-19 21:00  tonggc1668  阅读(100)  评论(0编辑  收藏  举报