wiki搬運,排序算法


穩定的排序[編輯]

  • 冒泡排序(bubble sort)— {\displaystyle O(n^{2})}O(n^{2})
  • 插入排序(insertion sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 雞尾酒排序(cocktail sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 桶排序(bucket sort)—{\displaystyle O(n)}O(n);需要{\displaystyle O(k)}O(k)額外空間
  • 計數排序(counting sort)—{\displaystyle O(n+k)}O(n+k);需要{\displaystyle O(n+k)}O(n+k)額外空間
  • 歸並排序(merge sort)—{\displaystyle O(n\log n)}O(n\log n);需要{\displaystyle O(n)}O(n)額外空間
  • 原地歸並排序— {\displaystyle O(n\log ^{2}n)}O(n\log^2 n)如果使用最佳的現在版本
  • 二叉排序樹排序(binary tree sort)— {\displaystyle O(n\log n)}O(n\log n)期望時間;{\displaystyle O(n^{2})}O(n^{2})最壞時間;需要{\displaystyle O(n)}O(n)額外空間
  • 鴿巢排序(pigeonhole sort)—{\displaystyle O(n+k)}O(n+k);需要{\displaystyle O(k)}O(k)額外空間
  • 基數排序(radix sort)—{\displaystyle O(nk)}{\displaystyle O(nk)};需要{\displaystyle O(n)}O(n)額外空間
  • 侏儒排序(gnome sort)— {\displaystyle O(n^{2})}O(n^{2})
  • 圖書館排序(library sort)— {\displaystyle O(n\log n)}O(n\log n)期望時間;{\displaystyle O(n^{2})}O(n^{2})最壞時間;需要{\displaystyle (1+\varepsilon )n}{\displaystyle (1+\varepsilon )n}額外空間
  • 塊排序(block sort)— {\displaystyle O(n\log n)}O(n\log n)

不穩定的排序[編輯]

  • 選擇排序(selection sort)—{\displaystyle O(n^{2})}O(n^{2})
  • 希爾排序(shell sort)—{\displaystyle O(n\log ^{2}n)}O(n\log^2 n)如果使用最佳的現在版本
  • 克洛弗排序(Clover sort)—{\displaystyle O(n)}O(n)期望時間,{\displaystyle O(n^{2})}O(n^{2})最壞情況
  • 梳排序— {\displaystyle O(n\log n)}O(n\log n)
  • 堆排序(heap sort)—{\displaystyle O(n\log n)}O(n\log n)
  • 平滑排序(smooth sort)— {\displaystyle O(n\log n)}O(n\log n)
  • 快速排序(quick sort)—{\displaystyle O(n\log n)}O(n\log n)期望時間,{\displaystyle O(n^{2})}O(n^{2})最壞情況;對於大的、隨機數列表一般相信是最快的已知排序
  • 內省排序(introsort)—{\displaystyle O(n\log n)}O(n\log n)
  • 耐心排序(patience sort)—{\displaystyle O(n\log n+k)}{\displaystyle O(n\log n+k)}最壞情況時間,需要額外的{\displaystyle O(n+k)}O(n+k)空間,也需要找到最長的遞增子序列(longest increasing subsequence)

不實用的排序[編輯]

  • Bogo排序— {\displaystyle O(n\times n!)}{\displaystyle O(n\times n!)},最壞的情況下期望時間為無窮。
  • Stupid排序{\displaystyle O(n^{3})}O(n^{3});遞歸版本需要{\displaystyle O(n^{2})}O(n^{2})額外存儲器
  • 珠排序(bead sort)— {\displaystyle O(n)}O(n) 或 {\displaystyle O({\sqrt {n}})}O({\sqrt  {n}}),但需要特別的硬件
  • 煎餅排序{\displaystyle O(n)}O(n),但需要特別的硬件
  • 臭皮匠排序(stooge sort)算法簡單,但需要約{\displaystyle n^{2.7}}{\displaystyle n^{2.7}}的時間

 

名稱 數據對象 穩定性 時間復雜度 額外空間復雜度 描述
平均 最壞
冒泡排序 數組 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (無序區,有序區)。
從無序區透過交換找出最大元素放到有序區前端。
選擇排序 數組 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (有序區,無序區)。
在無序區里找一個最小的元素跟在有序區的后面。對數組:比較得多,換得少。
鏈表
插入排序 數組、鏈表 {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) (有序區,無序區)。
把無序區的第一個元素插入到有序區的合適的位置。對數組:比較得少,換得多。
堆排序 數組 {\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(1)}O(1) (最大堆,有序區)。
從堆頂把根卸出來放在有序區之前,再恢復堆。
歸並排序 數組 {\displaystyle O(n\log ^{2}n)}{\displaystyle O(n\log ^{2}n)} {\displaystyle O(1)}{\displaystyle O(1)} 把數據分為兩段,從兩段中逐個選最小的元素移入新數據段的末尾。
可從上到下或從下到上進行。
{\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(n)+O(\log n)}O(n)+O(\log n)
如果不是從下到上
鏈表 {\displaystyle O(1)}O(1)
快速排序 數組 {\displaystyle O(n\log n)}O(n\log n) {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(\log n)}O(\log n) (小數,基准元素,大數)。 
在區間中隨機挑選一個元素作基准,將小於基准的元素放在基准之前,大於基准的元素放在基准之后,再分別對小數區與大數區進行排序。
希爾排序 數組 {\displaystyle O(n\log ^{2}n)}O(n\log ^{2}n) {\displaystyle O(n^{2})}O(n^{2}) {\displaystyle O(1)}O(1) 每一輪按照事先決定的間隔進行插入排序,間隔會依次縮小,最后一次一定要是1。
 
計數排序 數組、鏈表 {\displaystyle O(n+m)}O(n+m) {\displaystyle O(n+m)}O(n+m) 統計小於等於該元素值的元素的個數i,於是該元素就放在目標數組的索引i位(i≥0)。
桶排序 數組、鏈表 {\displaystyle O(n)}O(n) {\displaystyle O(m)}O(m) 將值為i的元素放入i號桶,最后依次把桶里的元素倒出來。
基數排序 數組、鏈表 {\displaystyle O(k\times n)}O(k\times n) {\displaystyle O(n^{2})}O(n^{2})   一種多關鍵字的排序算法,可用桶排序實現。
    • 均按從小到大排列
    • k代表數值中的"數字"個數
    • n代表數據規模
    • m代表數據的最大值減最小值


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM