快速排序 Java

快速排序,真的很快

快速排序和 歸併排序的速度都很快, 因爲它們都是在不斷地 縮小問題的規模


/**
 * Created by fupeng on 2017/1/20.
 */
public class quick {
    static void show(int [] a){
        for(int i : a){
            System.out.println(i);
        }
    }

    static void exch(int [] a, int i, int j){
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    static void quick(int []a, int low, int high){
        if(low >= high){
           return;
        }
        int key = a[low];
        int i=low;
        int j=high;

        while(i!=j){
            while(a[j]>=key &&i<j)
                j--;
            while(a[i] <= key && i<j)
                i++;
            if(i<j)
                exch(a,i,j);

        }

        exch(a,i,low);

        quick(a,low, i-1);
        quick(a,i+1, high);


    }

    static void sort(int [] a){
        quick(a, 0, a.length-1);
    }

    public static void main(String[] args) {
        int [] a = {2,3,4,1,5,9,8,6,7,0};
        sort(a);
        show(a);

    }
}
發佈了485 篇原創文章 · 獲贊 45 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章