排序算法總結(jié)

類別 排序方法 平均情況 最好情況 最壞情況 空間復(fù)雜度 穩(wěn)定性
插入排序 插入排序 O(n^2) O(n) O(n^2) O(1) 穩(wěn)定
插入排序 Shell排序 O(n^{1.3}) O(n) O(n^2) O(1) 不穩(wěn)定
選擇排序 選擇排序 O(n^2) O(n^2) O(n^2) O(1) 不穩(wěn)定
選擇排序 堆排序 O(nlog_{2}n) O(nlog_{2}n) O(nlog_{2}n) O(1) 不穩(wěn)定
交換排序 冒泡排序 O(n^2) O(n) O(n^2) O(1) 穩(wěn)定
交換排序 快速排序 O(nlog_{2}n) O(nlog_{2}n) O(n^2) O(nlog_{2}n) 不穩(wěn)定
歸并排序 歸并排序 O(nlog_{2}n) O(nlog_{2}n) O(nlog_{2}n) O(n) 穩(wěn)定
基數(shù)排序 基數(shù)排序 O(d(r+n)) O(d(n+rd)) O(d(r+n)) O(rd+n) 穩(wěn)定

基數(shù)排序中r代表關(guān)鍵字的基數(shù),d代表長度瓤鼻,n代表關(guān)鍵字的個數(shù)

package com.sort_algorithm;

import java.util.Arrays;

public class SortSummarize {

    public static void main(String[] args) {
        int[] a = { 9, 8, 7, 6, 5, 1, 3, 0, 10, -1, 99, -10 };
        int[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16 };
        // 將a的地址也就是引用給了b弛随,使b的引用發(fā)生了改變疫稿。此時打印铁坎,其實打印的是同一個地址下的數(shù)組碳抄,
        // 此時a、b都指向a的值舌稀,原來的b數(shù)組會因為沒有被引用到啊犬,被JVM的GC回收
        b = a;
        // 將b的地址也就是引用給了a,使a的引用發(fā)生了改變壁查。此時打印觉至,其實打印的是同一個地址下的數(shù)組,
        // 此時a睡腿、b都指向b的值语御,原來的a數(shù)組會因為沒有被引用到,被JVM的GC回收
        // a = b;
        print("選擇排序 ", selectSort(a));
        print("冒泡排序 ", bubbleSort(b));
        print("插入排序 ", insertSort(b));
        print("歸并排序 ", mergeSort(b));
        print("希爾排序 ", shellSort(b));
        print("堆排序 ", heapSort(b));
        print("快速排序 ", quickSort(b));
    }

    /**
     * 冒泡排序算法:每次將最小的一個數(shù)”浮“上去 最好情況O(n),即數(shù)組本身有序 最壞情況O(n^2)
     */
    public static int[] bubbleSort(int[] a) {
        a = Arrays.copyOf(a, a.length);

        int count = 0;
        boolean terminated = false;

        for (int i = 0; i < a.length - 1 && !terminated; i++) {
            terminated = true;
            for (int j = a.length - 2; j >= i; j--) {
                count++;
                if (a[j] > a[j + 1]) {
                    swap(a, j + 1, j);
                    terminated = false;
                }
            }
        }
        System.out.println("冒泡排序比較次數(shù): " + count);
        return a;
    }

    /**
     * 選擇排序:每次選出待排序中最小的一個
     */
    public static int[] selectSort(int[] a) {
        a = Arrays.copyOf(a, a.length);

        int count = 0;
        for (int i = 0; i < a.length - 1; i++) {
            int min = a[i];
            int minIndex = i;
            for (int j = i + 1; j < a.length; j++) {
                count++;
                if (a[j] < min) {
                    minIndex = j;
                    min = a[j];
                }
            }
            swap(a, i, minIndex);
        }
        System.out.println("選擇排序比較次數(shù): " + count);
        return a;
    }

    public static int[] insertSort(int[] a) {
        a = Arrays.copyOf(a, a.length);

        int count = 0;
        for (int i = 1; i < a.length; i++) {// i之前有序,i指向待排序的元素
            if (a[i] < a[i - 1]) {
                int temp = a[i];
                int j = i - 1;// j指向當(dāng)前元素的前一個
                for (; j >= 0 && a[j] > temp; j--) {
                    count++;
                    a[j + 1] = a[j];
                }
                a[j + 1] = temp;
            }
        }

        System.out.println("插入排序比較次數(shù): " + count);
        return a;
    }

    private static void swap(int[] a, int i, int j) {
        if (i == j)
            return;
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    /**
     * 希爾排序
     * 
     * @param a
     * @return
     */
    public static int[] shellSort(int[] a) {
        a = Arrays.copyOf(a, a.length);
        int count = 0;

        int increment = a.length;
        do {
            increment = increment / 3 + 1;
            for (int i = increment; i < a.length; i = i + increment) {
                if (a[i] < a[i - increment]) {
                    int temp = a[i];
                    int j = i - increment;
                    for (; j >= 0 && a[j] > temp; j -= increment) {
                        count++;
                        a[j + increment] = a[j];
                    }
                    a[j + increment] = temp;
                }
            }
        } while (increment > 1);

        System.out.println("希爾排序比較次數(shù): " + count);
        return a;

    }

    /**
     * 堆排序
     * 
     * @param a
     * @return
     */
    public static int[] heapSort(int[] a) {
        a = Arrays.copyOf(a, a.length);
        int length = a.length;
        for (int i = length / 2 - 1; i >= 0; i--)// 2*i+1<=length-1,最后一個有左孩子的節(jié)點
            heapAdjust(a, i, length);

        for (int i = length - 1; i >= 0; i--) {
            swap(a, 0, i);
            heapAdjust(a, 0, i);//
        }

        return a;
    }

    // 每次將以i為root的子樹滿足最大堆特性;i指向待調(diào)整的節(jié)點
    private static void heapAdjust(int[] a, int i, int length) {
        int temp = a[i];

        for (int j = 2 * i + 1; j < length; j = 2 * i + 1) {// 剛開始時指向左孩子
            if (j + 1 < length && a[j + 1] > a[j])// 如果有做右孩子席怪,且右孩子比左孩子大
                j++;// j指向左右孩子中值較大的一個

            if (temp >= a[j])
                break;
            a[i] = a[j];

            i = j;
        }
        a[i] = temp;
    }

    public static int[] mergeSort(int[] a) {
        a = Arrays.copyOf(a, a.length);
        int[] aux = new int[a.length];
        mergeSort(a, aux, 0, a.length - 1);
        return a;
    }

    private static void mergeSort(int[] a, int[] aux, int lo, int high) {
        if (lo >= high)
            return;

        int mid = (lo + high) / 2;
        mergeSort(a, aux, lo, mid);
        mergeSort(a, aux, mid + 1, high);
        merge(a, aux, lo, mid, high);
    }

    private static void merge(int[] a, int aux[], int lo, int mid, int high) {
        for (int i = lo; i <= high; i++) {
            aux[i] = a[i];
        }

        int lo_h = mid + 1;
        for (int i = lo; i <= high; i++) {
            if (lo > mid)
                a[i] = aux[lo_h++];
            else if (lo_h > high)
                a[i] = aux[lo++];
            else {
                if (aux[lo] <= aux[lo_h])
                    a[i] = aux[lo++];
                else
                    a[i] = aux[lo_h++];
            }
        }
    }

    /**
     * 快速排序
     * 
     * @param a
     * @return
     */

    public static int[] quickSort(int[] a) {
        a = Arrays.copyOf(a, a.length);
        quickSort(a, 0, a.length - 1);
        return a;
    }

    private static void quickSort(int[] a, int low, int high) {
        if (low >= high)
            return;

        int partition = partition(a, low, high);
        quickSort(a, low, partition - 1);
        quickSort(a, partition + 1, high);
    }

    private static int partition(int[] a, int low, int high) {
        int tempt = a[low];

        while (low < high) {
            while (low < high && a[high] >= tempt)
                high--;
            // swap(a,low,high);
            a[low] = a[high];

            while (low < high && a[low] <= tempt)
                low++;
            // swap(a,low,high);
            a[high] = a[low];
        }
        a[low] = tempt;
        return low;
    }

    private static void print(String str, int[] a) {
        System.out.println(str);
        for (int i = 0; i < a.length; i++)
            System.out.print(a[i] + " ");
        System.out.println("\n");
    }

    /**
     * 下面是鏈表的快速排序
     * 
     * @param str
     * @param a
     */
    public static ListNode quickSort(ListNode begin, ListNode end) {
        // 判斷為空应闯,判斷是不是只有一個節(jié)點
        if (begin == null || end == null || begin == end)
            return begin;
        // 從第一個節(jié)點和第一個節(jié)點的后面一個幾點
        // begin指向的是當(dāng)前遍歷到的最后一個<= nMidValue的節(jié)點
        ListNode first = begin;
        ListNode second = begin.next;

        int nMidValue = begin.val;
        // 結(jié)束條件,second到最后了
        while (second != end.next && second != null) {// 結(jié)束條件
            // 一直往后尋找<=nMidValue的節(jié)點挂捻,然后與fir的后繼節(jié)點交換
            if (second.val < nMidValue) {
                first = first.next;
                // 判斷一下孽锥,避免后面的數(shù)比第一個數(shù)小,不用換的局面
                if (first != second) {
                    int temp = first.val;
                    first.val = second.val;
                    second.val = temp;
                }
            }
            second = second.next;
        }
        // 判斷细层,有些情況是不用換的惜辑,提升性能
        if (begin != first) {
            int temp = begin.val;
            begin.val = first.val;
            first.val = temp;
        }
        // 前部分遞歸
        quickSort(begin, first);
        // 后部分遞歸
        quickSort(first.next, end);
        return begin;
    }

    /**
     * 鏈表的歸并排序
     */
    public ListNode sortList1(ListNode head) {
        if (head == null || head.next == null)
            return head;
        quickSort(null, null);
        return mergeSort(head);
    }

    private ListNode mergeSort(ListNode head) {
        if (head == null || head.next == null)
            return head;

        ListNode mid = getMid(head);
        ListNode second = mid.next;
        mid.next = null;

        ListNode left = mergeSort(head);
        ListNode right = mergeSort(second);
        return merge(right, left);
    }

    private ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (l1 != null)
            cur.next = l1;
        else
            cur.next = l2;

        return dummy.next;

    }

    private ListNode getMid(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode slow = head;
        ListNode faster = head.next;
        while (faster != null && faster.next != null) {
            slow = slow.next;
            faster = faster.next.next;
        }
        return slow;
    }
}
package com.sort_algorithm;

public class ListNode {

    int val;
    ListNode next;

    public ListNode(int x) {
        val = x;
        next = null;
    }
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市疫赎,隨后出現(xiàn)的幾起案子盛撑,更是在濱河造成了極大的恐慌,老刑警劉巖捧搞,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抵卫,死亡現(xiàn)場離奇詭異,居然都是意外死亡胎撇,警方通過查閱死者的電腦和手機介粘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晚树,“玉大人姻采,你說我怎么就攤上這事【粼鳎” “怎么了慨亲?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長宝鼓。 經(jīng)常有香客問我刑棵,道長,這世上最難降的妖魔是什么愚铡? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任蛉签,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘碍舍。我一直安慰自己柠座,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布乒验。 她就那樣靜靜地躺著愚隧,像睡著了一般蒂阱。 火紅的嫁衣襯著肌膚如雪锻全。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天录煤,我揣著相機與錄音鳄厌,去河邊找鬼。 笑死妈踊,一個胖子當(dāng)著我的面吹牛了嚎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播廊营,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼歪泳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了露筒?” 一聲冷哼從身側(cè)響起呐伞,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎慎式,沒想到半個月后伶氢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡瘪吏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年癣防,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片掌眠。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蕾盯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蓝丙,到底是詐尸還是另有隱情刑枝,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布迅腔,位于F島的核電站装畅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏沧烈。R本人自食惡果不足惜掠兄,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蚂夕,春花似錦迅诬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至等脂,卻和暖如春俏蛮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背上遥。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工搏屑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人粉楚。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓辣恋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親模软。 傳聞我的和親對象是個殘疾皇子伟骨,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 一、概述 排序算法概念 在計算機科學(xué)與數(shù)學(xué)中燃异,一個排序算法是將一組雜亂無章的數(shù)據(jù)按一定的規(guī)律順次排列起來的算法携狭。排...
    簡書冷雨閱讀 1,033評論 0 0
  • 作者:大海里的太陽原文地址:http://www.cnblogs.com/wxisme/ 前言 查找和排序算法是算...
    IT程序獅閱讀 2,500評論 0 63
  • 題記: 直接插入排序(穩(wěn)定)-->希爾排序 : 屬于插入排序 簡單選擇排序(穩(wěn)定)-->堆排序 :屬于選擇排序...
    Pitfalls閱讀 2,807評論 2 3
  • Dubbo 是阿里巴巴開源的基于 Java 的高性能 RPC 框架,本文介紹 Spring Boot 集成 Dub...
    郭藝賓閱讀 265評論 0 0
  • 足球比賽是一項與身體接觸的比賽,在足球比賽中難免磕磕碰碰鲫剿,摩摩擦擦鳄逾,有時候,我的頭遇你的伸出的腳灵莲,有時候你的腳被我...
    讀寫人家閱讀 148評論 0 2