Java 快速排序

package com.yk.sort;

public class Fast {
    
    public static void fastSort(int left, int right, int[] array){
        if(left >= right)
            return;
        int povit = array[left];//基准数
        int i = left;
        int j = right;
        //比较
        while(i != j){
            while(j > i && array[j] >= povit)
                j--;
            if(i != j)
                array[i] = array[j];
            
            while(j > i && array[i] <= povit)
                i++;
            if(i != j)
                array[j] = array[i];
        }
        //i,j相遇,一轮比较结束,变换基准数位置
        array[i] = povit;
        //进行下一轮比较
        fastSort(left, i - 1, array);
        fastSort(i + 1, right, array);
    }
    
    public static void main(String[] args) {
        int[] array = {5,6,4,8,9,1,10,2,3,9,3};
        fastSort(0, array.length - 1, array);
        for (int ii : array)
            System.out.print(ii + "\t");
    }
}

 

posted @ 2016-05-13 11:33  活下去  阅读(175)  评论(0编辑  收藏  举报