參考維基百科
0. 一些基本概念
--(體會)空間換時間是進一步提高演算法速度的最終方法,比如希爾排序和基數排序就是通過佔用額外空間來獲得比快速排序更快的速度。當然,空間換不換的到時間,也是要看你寫的演算法的水平的。
--best, worst and average cases. In computer science,
best,
worst and
average cases of a given algorithm express what the resource usage is
at least,
at most and
on average, respectively. Usually the resource being considered is running time, but it could also be memory or other resources.
1. example for best case: when doing a search, the first element is the one you look for.
2. example for worst case: when doing a search, visiting every element to either find it in last position or not find it at all
3. some algorithms like hash tables have very poor worst case behaviours, but a well written hash table of sufficient size will statistically never give the worst case
--Big O notation: In computer science, it is useful in the analysis of algorithms. If a function
f(
n) can be written as a finite sum of other functions, then the fastest growing one determines the order of
f(
n). For example
| Notation |
Name |
Example |
|
constant |
Determining if a number is even or odd; using a constant-size lookup table or hash table |
|
logarithmic |
Finding an item in a sorted array with a binary search or a balanced search tree as well as all operations in a Binomial heap. |
|
fractional power |
Searching in a kd-tree |
|
linear |
Finding an item in an unsorted list or a malformed tree (worst case) or in an unsorted array; Adding two n-bit integers by ripple carry. |
|
linearithmic, loglinear, or quasilinear |
Performing a Fast Fourier transform; heapsort, quicksort (best and average case), or merge sort |
|
quadratic |
Multiplying two n-digit numbers by a simple algorithm; bubble sort (worst case or naive implementation), shell sort, quicksort (worst case), selection sort or insertion sort |
|
polynomial or algebraic |
Tree-adjoining grammar parsing; maximum matching for bipartite graphs |
|
L-notation or sub-exponential |
Factoring a number using the quadratic sieve or number field sieve |
|
exponential |
Finding the (exact) solution to the traveling salesman problem using dynamic programming; determining if two logical statements are equivalent using brute-force search |
|
factorial |
Solving the traveling salesman problem via brute-force search; generating all unrestricted permutations of a poset; finding the determinant with expansion by minors. |
1. 多種分類協助理解排序基本概念
--內排序和外排序
--就地排序和非就地排序
--General method: insertion, exchange, selection, merging, etc.. Exchange sorts include bubble sort and quicksort. Selection sorts include shaker sort and heapsort.
2. 排序穩定性。註:不穩定的演算法可以稍作修改考慮index來變成穩定的演算法
3. 排序演算法比較(關鍵是明白各個演算法適用的場合)
n比較小時:選擇排序
序列比較有序時:直接插入排序,冒泡排序
n比較大時:快速排序,堆排序,歸併排序
(列表如下)
穩定的
- 冒泡排序(bubble sort) — O(n2)
- 雞尾酒排序 (Cocktail sort, 雙向的冒泡排序) — O(n2)
- 插入排序 (insertion sort)— O(n2)
- 桶排序 (bucket sort)— O(n); 需要 O(k) 額外空間
- 計數排序 (counting sort) — O(n+k); 需要 O(n+k) 額外空間
- 合併排序 (merge sort)— O(n log n); 需要 O(n) 額外空間
- 原地合併排序 — O(n2)
- 二叉排序樹排序 (Binary tree sort) — O(n log n)期望時間; O(n2)最壞時間; 需要 O(n) 額外空間
- 鴿巢排序 (Pigeonhole sort) — O(n+k); 需要 O(k) 額外空間
- 基數排序 (radix sort)— O(n·k); 需要 O(n) 額外空間
- Gnome 排序 — O(n2)
- 圖書館排序 — O(n log n) with high probability, 需要 (1+ε)n 額外空間
不穩定
- 選擇排序 (selection sort)— O(n2)
- 希爾排序 (shell sort)— O(n log n) 如果使用最佳的現在版本
- 組合排序 — O(n log n)
- 堆排序 (heapsort)— O(n log n)
- 平滑排序 — O(n log n)
- 快速排序 (quicksort)— O(n log n) 期望時間, O(n2) 最壞情況; 對於大的、亂數串列一般相信是最快的已知排序
- Introsort — O(n log n)
- Patience sorting — O(n log n + k) 最壞情況時間,需要 額外的 O(n + k) 空間,也需要找到最長的遞增子序列(longest increasing subsequence)
平均時間複雜度
平均時間複雜度由高到低為:
- 冒泡排序 O(n2)
- 插入排序 O(n2)
- 選擇排序 O(n2)
- 歸併排序 O(n log n)
- 堆排序 O(n log n)
- 快速排序 O(n log n)
- 希爾排序 O(n1.25)
- 基數排序 O(n)
說明:雖然完全逆續的情況下,快速排序會降到選擇排序的速度,不過從機率角度來說(參考資訊學理論,和機率學),不對演算法做編程上最佳化時,快速排序的平均速度比堆排序要快一些。