基本排序算法回顧(JAVA)

Java 基礎之排序算法

最近回顧了一下幾個基本的排序算法,包括排序,插入,選擇。接來下就上代碼回顧一下


冒泡排序

簡要說來,冒泡排序就是對需要排序的數組進行遍歷,然後從頭開始依次兩兩比較大小,根據對應的比較要求進行左右互換,從頭到尾輪一遍,然後第一次遍歷結束後最大(小)的到了數組的末尾。舉例如下:比如1 2 8 6 4 0 5,則進行第一次遍歷比較過程如下:
1,2,8,6,4,0,5。(1,2比較)–>
1,2,8,6,4,0,5。(2,8比較)–>
1,2,6,8,4,0。(8,6比較,互換)–>
1,2,6,4,8,0。(8,4比較,互換)–>
1,2,6,4,0,8。(8,0比較)。一次遍歷比較完成。

代碼塊

/**
 * Created by Ying  on 2017/7/2.
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] a = {1, 2, 5, 3, 9, 6, 7};
        sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }

    public static void sort(int[] a) {
        int length = a.length;
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i - 1; j++) {
            //若降序,則下面的 > 改爲 <
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }
}

插入排序

插入排序,簡單說來就是從最初的無序序列中,先選一個放到有序序列裏,一般爲第一個,然後從剩下的無序序列中依次選取需要排序的數值放入到有序序列中,此時要在有序序列中尋找適當的位置然後插入其中,這也就是爲什麼叫插入排序的原因。具體過程看如下代碼:

代碼塊


/**
 * Created by ying on 2017/7/3.
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] a = {1,2,4,3,9,0,6};
        sort2(a);
        for(int a1:a){
            System.out.println(a1);
        }

    }
    static void sort(int[] a) {
        int pass, i, temp;
        for (pass = 1; pass < a.length; pass++) {
            temp = a[pass];// the value would to be instered;
            for (i = pass - 1; i >= 0; i--)//內部循環,遍歷已經排序完畢的隊伍
                if (a[i] <= temp)
                    break;
                else
                    a[i + 1] = a[i];
            a[i + 1] = temp;
        }
    }

//降序排列
    static void sort2(int[] a){
        int pass,i,temp;
        for(pass=1;pass<a.length;pass++){
            temp = a[pass];
            for(i = pass-1;i>=0;i--){
                if(a[i]>=temp){
                    break;
                }else{
                    a[i+1] = a[i];
                }
            }
            a[i+1] = temp;
        }
     }
}

選擇排序

選擇排序,原理就是對原始的序列進行遍歷,然後每次遍歷中選取無序序列中的最大(小)的值,放到有序序列的末尾。在代碼實現中,我們一般用一個變量標記當前的最大(小)值的索引,然後再將其與無序序列末尾的那個值進行互換。過程見如下代碼:

代碼塊


/**
 * Created by admin on 2017/7/3.
 */
public class select {
    public static void main(String[] args) {
        int[] a = {1, 4, 5, 8, 2, 3, 9};
        sort2(a);
        for (int a2 : a) {
            System.out.println(a2);
        }
    }

    static void sort(int[] a) {
        int pass, i, temp, k;
        for (pass = 0; pass < a.length - 1; pass++) {
            for (i = pass, k = i; i < a.length; i++) {
                if (a[i] < a[k]) {
                    k = i;
                }
            }
            temp = a[pass];
            a[pass] = a[k];
            a[k] = temp;
        }
    }

    static void sort2(int[] a) {
        int pass, temp, i, k;
        for (pass = 0; pass < a.length; pass++) {
            for (i = pass, k = i; i < a.length; i++) {
                if (a[i] > a[k]) {
                    k = i;
                }
            }
             temp = a[pass];
            a[pass] = a[k];
            a[k] = temp;
        }

    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章