插入排序 和 希爾排序 java

http://mp.weixin.qq.com/s/deUy_VPJ2m6BFbrEZp9DKg
選擇排序

/**
 * Created by fupeng on 2017/1/19.
 */
public class insert {

    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 sort(int [] a){
        for(int i=1; i< a.length; i++){


            int the = a[i];
            int j = i-1;
            for(; j>=0 && the < a[j]  ;  j--){
                a[j+1] = a[j];
            }
            a[j+1] = the;


        }
    }

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

    }
}

希爾排序 shell sort, 希爾排序是 改良版的 插入排序

/**
 * Created by fupeng on 2017/1/20.
 */
public class shell {
    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 sort(int [] a){

        for(int gap = a.length/2;gap>0;gap /= 2 ) {

            for (int i = gap; i < a.length; i=i+gap) {

                int the = a[i];
                int j = i - gap;
                for (; j >= 0 && the < a[j]; j= j-gap) {
                    a[j + gap] = a[j];
                }
                a[j + gap] = the;

            }
        }
    }

    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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章