快速排序

package my0410;

public class QuickSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int data[]={2,2,4,1,4};
        Qsort(data);
        for(int i=0;i<data.length;i++){
            System.out.println(data[i]);
        }
    }
    public static void Qsort(int [] data){
        quickSort(data,0,data.length-1);
        
    }
    public static void quickSort(int data[],int i,int j){
        int privot;
        if(i<j){
            privot=partition(data,i,j);            
            quickSort(data,i,privot-1);
            quickSort(data,privot+1,j);
        }
        
    }
    
    public static void swap(int []data,int a,int b){
        int temp=data[a];
        data[a]=data[b];
        data[b]=temp;        
    }
    public static int partition(int []data,int i,int j){
        int index=(i+j)/2;
        swap(data,index,i);
        int privot=data[i];
        while(i<j){
            while(i<j&&data[j]>=privot)--j;
            data[i]=data[j];
            while(i<j&&data[i]<=privot)++i;
            data[j]=data[i];            
        }
        data[i]=privot;
        return i;
    }

}

 

posted @ 2015-04-11 21:40  ~每天进步一点点~  阅读(145)  评论(0编辑  收藏  举报