排序
引入
7:45
数据结构与算法
这门课讲的是如何根据开销选择容器。第一个单元介绍衡量标准:开销如何随输入规模增长。接下来的四个单元介绍容器:列表、栈和队列、哈希表、树和堆,以及图。最后两个单元介绍算法:排序和搜索。这里所有关于开销的结论,都是在讲义中列出的数据集上统计得出的。
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| Insertion sort/ɪnˈsɜːʃn sɔːt/ | 插入排序 | chā rù pái xù |
| inversion/ɪnˈvɜːʃn/ | 逆序 | nì xù |
| Merge sort/mɜːdʒ sɔːt/ | 归并排序 | guī bìng pái xù |
| Quicksort/ˈkwɪksɔːt/ | 快速排序 | kuài sù pái xù |
| lower bound/ˈləʊə baʊnd/ | 下界 | xià jiè |
| Counting sort/ˈkaʊntɪŋ sɔːt/ | 计数排序 | jì shù pái xù |
| stable/ˈsteɪbl/ | 稳定的 | wěn dìng de |
课程数据集
- 排序$[5, 2, 9, 1, 7, 3]$。插入排序扩展有序前缀,每次将新卡片向后滑动;归并排序对半分、分别排序再合并;快速排序围绕基准值分区;冒泡排序交换相邻逆序——仅作教学用途,实际极少使用。


这张卡片已更正,翻译更新前会显示英文。
Inversions measure the disorder
- An inversion 逆序 is a pair of items in the wrong order. Our list has 8: $(5,2), (5,1), (5,3), (2,1), (9,1), (9,7), (9,3), (7,3)$.
- Swapping two neighbours that are out of order fixes exactly one inversion. So insertion sort implemented with adjacent swaps, and any sort that swaps only out-of-order neighbours (such as bubble sort), needs exactly 8 swaps here.
- A reversed list of $n$ items has $n(n-1)/2$ inversions, so these sorts are $O(n^2)$: fine for a short or nearly sorted list, slow for a long one.
A sort that swaps only adjacent out-of-order pairs makes, on a dataset, exactly — swaps
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Each swap of neighbours fixes exactly one inversion, so the number of swaps equals the number of inversions.
The dataset [5, 2, 9, 1, 7, 3] has — inversions
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Count the out-of-order pairs: (5,2), (5,1), (5,3), (2,1), (9,1), (9,7), (9,3), (7,3).
手工演示两者
- 插入排序:$2$移动一次;$9$零次;$1$三次;$7$一次;$3$三次——总计$8$次移动,$11$次比较,结果为$[1, 2, 3, 5, 7, 9]$。
- 归并排序:分为$[5, 2, 9]$和$[1, 7, 3]$;递归处理$[2, 5, 9]$和$[1, 3, 7]$;合并一次:$1, 2, 3, 5, 7, 9$需$10$次比较。在$n = 1000$处家族按$100$分割。
以首元素为基准值的已排序输入是快速排序的最坏情况,$O(n^2)$——这并非罕见而是陷阱。稳定性在归并排序和插入排序中得以保留,但在快速排序和堆排序中不保证。

Merge sort on the same dataset uses — comparisons
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Measured: 10 comparisons against insertion 11 — and the gap widens with size.
这是一张新卡片,翻译完成前会显示英文。
Quicksort: partition around a pivot
- Quicksort 快速排序 picks a pivot, moves smaller items to its left and larger ones to its right, then sorts each side the same way. With the last item 3 as pivot, $[5, 2, 9, 1, 7, 3]$ becomes $[2, 1, 3, 5, 7, 9]$: 3 is now at index 2, its final place.
- If each pivot splits the list roughly in half, quicksort takes $O(n \log n)$ on average and needs no extra array.
- But if the pivot is always the smallest or largest item (for example, the first item of an already sorted list), each step removes only one item: $O(n^2)$. Real libraries choose the pivot at random or as a median of three.
Partition $[4, 8, 1, 6, 5]$ around the last element $5$. Where does $5$ land?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Two elements (4 and 1) are smaller than 5, so the pivot takes index 2.
这张卡片已更正,翻译更新前会显示英文。
The lower bound for comparison sorts
- A sort that only compares items must tell apart all $n!$ possible orders. Each yes/no comparison can at best halve the possibilities, so the worst case needs at least $\lceil \log_2 n! \rceil$ comparisons.
- For $n = 6$: $6! = 720$ and $\log_2 720 \approx 9.5$, so at least 10 comparisons are needed in the worst case. The example above uses 10 comparisons on that particular input. In general $\log_2 n! \approx n \log_2 n$, so this lower bound 下界 is $\Theta(n \log n)$.
What is the minimum worst-case number of comparisons to sort 5 items?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
5! = 120 and log2 120 is about 6.9, so at least 7 comparisons.
A cleverer comparison sort could sort every list of $6$ distinct items with at most $9$ comparisons.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
$6! = 720$ orders, and $9$ yes/no comparisons can tell apart at most $2^9 = 512$ of them. The worst case needs at least $\lceil \log_2 720 \rceil = 10$ comparisons, whatever the algorithm.
这是一张新卡片,翻译完成前会显示英文。
Beating the bound without comparing
- The bound is only for sorts that compare. Counting sort 计数排序 counts how often each key value appears, then writes the values out in order: $O(n + k)$ for keys from 0 to $k$.
- 1,000 exam marks from 0 to 100 take about $1{,}000 + 101 = 1{,}101$ steps, against $1{,}000 \times \log_2 1{,}000 \approx 9{,}966$ comparisons. Radix sort repeats counting sort digit by digit for larger keys.
Counting sort of 1,000 keys with values 0 to 100: about how many steps ($n + k + 1$)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
$1{,}000 + 101 = 1{,}101$.
No sorting algorithm can beat $O(n \log n)$.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Only comparison sorts are bound; counting sort is $O(n + k)$.
这是一张新卡片,翻译完成前会显示英文。
Worked example: why stability matters
- Records already sorted by name: (Al, A), (Bo, B), (Cy, B), (Di, A). Now sort them by grade with a stable 稳定的 sort, which keeps items with equal keys in their old order.
- Result: (Al, A), (Di, A), (Bo, B), (Cy, B). Within each grade the names are still in alphabetical order, so the list is now sorted by grade, then name.
- An unstable sort may put (Di, A) before (Al, A). Merge sort and insertion sort are stable; the usual quicksort and heapsort are not.
Sort (Al, A), (Bo, B), (Cy, B), (Di, A) by grade with a stable sort. The second record is:
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Equal grades keep their name order: Al, Di, Bo, Cy.
这是一张新卡片,翻译完成前会显示英文。
Marks that slip away
- Calling quicksort always $O(n \log n)$. A bad pivot, such as the first item of a sorted list, gives $\Theta(n^2)$.
- Assuming every sort is stable. Heapsort and the usual quicksort can reorder equal keys.
- Hoping to beat $n \log n$ by comparing. Six items need at least 10 comparisons in the worst case; only sorts that do not compare escape the bound.
这是一张新卡片,翻译完成前会显示英文。
Recap
- Insertion sort: one move per inversion, $O(n^2)$, good for small or nearly sorted lists.
- Merge sort: $O(n \log n)$ and stable; quicksort: $O(n \log n)$ on average, $O(n^2)$ with bad pivots.
- Comparison sorts need at least $\lceil \log_2 n! \rceil$ comparisons; counting sort avoids comparing.
出了什么问题?只有网站团队能看到这些信息。