演算法3 七大排序之:直接插入排序和希爾排序

來源:互聯網
上載者:User

演算法3 七大排序之:直接插入排序和希爾排序

上一篇總結了直接選擇排序和堆排序,這一篇要總結的是插入排序中的直接插入排序和希爾排序,我們主要從以下幾點進行總結。

1、直接插入排序及演算法實現

2、希爾排序及演算法實現

3、直接插入排序PK希爾排序

1、直接插入排序及演算法實現

什麼是直接插入排序呢?直接插入排序的基本思想是:每次從無序序列中取出第一個元素插入到已經排好序的有序序列中,從而得到一個新的,數量加1的有序序列。

1-1、

下面是直接插入排序的圖解說明。

 

1-2、代碼

下面是直接插入排序的演算法實現代碼。

InsertSort.java

public class InsertSort {    public static void main(String[] args) {        int[] list = {90, 10, 20, 50, 70, 40, 80, 60, 30, 52};        System.out.println("************直接插入排序************");        System.out.println("排序前:");        display(list);        System.out.println("");        System.out.println("排序後:");        insertSort(list);        display(list);    }    /**     * 直接插入排序演算法     */    public static void insertSort(int[] list) {        // 從無序序列中取出第一個元素 (注意無序序列是從第二個元素開始的)        for (int i = 1; i < list.length; i++) {            int temp = list[i];            int j;            // 遍曆有序序列            // 如果有序序列中的元素比臨時元素大,則將有序序列中比臨時元素大的元素依次後移            for (j = i - 1; j >= 0 && list[j] > temp; j--) {                list[j + 1] = list[j];            }            // 將臨時元素插入到騰出的位置中            list[j + 1] = temp;        }    }        /**     * 遍曆列印     */    public static void display(int[] list) {        System.out.println("********展示開始********");        if (list != null && list.length > 0) {            for (int num :                    list) {                System.out.print(num + " ");            }            System.out.println("");        }        System.out.println("********展示結束********");    }} 

測試結果:

 

2、希爾排序及演算法實現

 希爾排序是直接插入排序的一種更高效的改進版本,又稱為“縮小增量排序”。

2-1、

 

 

2-2、代碼

ShellSort.java 

public class ShellSort {    public static void main(String[] args) {        int[] list = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};        System.out.println("************希爾排序************");        System.out.println("排序前:");        display(list);        System.out.println("");        System.out.println("排序後:");        shellSort(list);        display(list);    }    /**     * 希爾排序演算法     */    public static void shellSort(int[] list) {        // 取增量        int gap = list.length / 2;        while (gap >= 1) {            // 無序序列            for (int i = gap; i < list.length; i++) {                int temp = list[i];                int j;                // 有序序列                for (j = i - gap; j >= 0 && list[j] > temp; j = j - gap) {                    list[j + gap] = list[j];                }                list[j + gap] = temp;            }            // 縮小增量            gap = gap / 2;        }    }    /**     * 遍曆列印     */    public static void display(int[] list) {        System.out.println("********展示開始********");        if (list != null && list.length > 0) {            for (int num :                    list) {                System.out.print(num + " ");            }            System.out.println("");        }        System.out.println("********展示結束********");    }} 

測試結果:

 

3、直接插入排序PK希爾排序

既然希爾排序是直接插入排序的改進版,我們就來測試一下希爾排序是否真的比直接插入排序更快? 

代碼:

InsertSortPkShellSort.java

public class InsertSortPkShellSort {    public static void main(String[] args) {        testInsertSort();        testShellSort();    }    /**     * 測試直接插入排序耗費的時間     */    public static void testInsertSort() {        int[] list = new int[10000];        for (int i = 0; i < 10000; i++) {            list[i] = (int) (Math.random() * 100000);        }        // 直接插入排序        long start = System.currentTimeMillis();        InsertSort.insertSort(list);        long end = System.currentTimeMillis();        System.out.println("直接插入排序耗費的時間:" + (end - start));        display(list);    }    /**     * 測試���爾排序耗費的時間     */    public static void testShellSort() {        int[] list = new int[10000];        for (int i = 0; i < 10000; i++) {            list[i] = (int) (Math.random() * 100000);        }        // 希爾排序        long start = System.currentTimeMillis();        ShellSort.shellSort(list);        long end = System.currentTimeMillis();        System.out.println("希爾排序耗費的時間:" + (end - start));        display(list);    }    /**     * 遍曆列印前10個數     */    public static void display(int[] list) {        System.out.println("********排序之後的前10個數start********");        if (list != null && list.length > 0) {            for (int i = 0; i < 10; i++) {                System.out.print(list[i] + " ");            }            System.out.println("");        }        System.out.println("********排序之後的前10個數end**********");        System.out.println("");    }} 

測試結果:

從測試結果可以看出,希爾排序比直接插入排序更快。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.