# 比较排序

比较排序是一种仅通过两两比较来将一组元素排成顺序的排序算法：每一步它询问一个元素是否排在另一个之前，而从不检查元素的内部结构，例如它们的各位数字或比特位。[快速排序](https://www.edgepedia.org/zh/quicksort)、[归并排序](https://www.edgepedia.org/zh/merge-sort)、堆排序、插入排序和 Timsort 都是比较排序；[计数排序](https://www.edgepedia.org/zh/counting-sort)和基数排序直接读取键的各位数字或数值，因此不属于比较排序，而这一区别对于排序速度有一个可证明的推论。

| 关键事实 | 详情 |
|---|---|
| 最坏情况下界 | 任何基于比较的排序对 n 个不同元素进行排序，在最坏情况下都需要 Ω(n log n) 次比较，这由一棵有 n! 个叶子的决策树论证证明<sup>[1](https://mathcenter.oxford.emory.edu/site/cs171/decisionTree/)</sup> |
| 平均情况下界 | 信息论下界为平均需要 n lg n − 1.4427n + O(log n) 次比较<sup>[2](https://arxiv.org/abs/1705.00849)</sup> |
| 已知最佳平均常数 | MergeInsertion 达到 n lg n − 1.3999n；2017 年的一项改进达到 n lg n − 1.4106n，将与该下界的差距缩小了约 25%<sup>[2](https://arxiv.org/abs/1705.00849)</sup> |
| 经典算法 | 归并排序归功于冯·诺依曼（von Neumann，1945）；快速排序由 C. A. R. 霍尔（C. A. R. Hoare）于 1962 年发表在《计算机期刊》（*The Computer Journal*）5(1):10–15<sup>[3](https://opendatastructures.org/newhtml/ods/latex/sorting.html)</sup><sup> • </sup><sup>[4](https://dblp.dagstuhl.de/rec/journals/cj/Hoare62.html)</sup> |
| 标准库实践 | Python 使用稳定的 Timsort；Java 的基本类型使用双枢轴快速排序；C++ 的 std::sort 使用内省排序，并要求最坏情况为 O(N log N)<sup>[5](https://docs.python.org/3.13/builtins/functions.html)</sup><sup> • </sup><sup>[6](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Arrays.html?is-external=true)</sup><sup> • </sup><sup>[7](https://en.cppreference.com/cpp/algorithm/sort)</sup> |
| 跳出下界 | 计数排序和基数排序通过直接利用键值在线性时间内运行，因此 Ω(n log n) 下界不适用于它们<sup>[8](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-7-counting-sort-radix-sort-lower-bounds-for-sorting/)</sup> |

## 决策树下界

每一个基于比较的算法都可以建模为一棵决策树。每个内部节点代表算法所做的一次比较，每个叶子代表关于正确排序顺序的一个最终结论。为了使算法对每个输入都正确，它需要为每一种可能的排列顺序准备一个叶子：n 个元素有 n! 种排列，其中任何一种都可能就是排序后的答案。<sup>[9](https://ics.uci.edu/~thornton/ics46/Notes/LinearTimeSorting/)</sup>

于是树的高度就限定了最坏情况下的比较次数，因为最长的根到叶路径正是树高的定义。高度为 h 的二叉树最多有 2<sup>h</sup> 个叶子，因此正确性要求 n! ≤ 2<sup>h</sup>，两边取以 2 为底的对数得到 h ≥ log<sub>2</sub>(n!)。根据[斯特林近似](https://www.edgepedia.org/zh/stirlings-approximation)，log<sub>2</sub>(n!) 的增长量为 n log<sub>2</sub> n，因此没有任何基于比较的排序算法能保证用少于 log<sub>2</sub>(n!) 次比较对 n 个元素进行排序，最坏情况是 Ω(n log<sub>2</sub> n)。<sup>[1](https://mathcenter.oxford.emory.edu/site/cs171/decisionTree/)</sup>

这一论证可以推广到确定性算法之外。随机化算法是确定性算法上的一个概率分布，而由于每个确定性比较排序的平均情况代价至少为 ⌊log<sub>2</sub>(n!)⌋，它们的任何加权平均，包括每一个随机化算法，都具有相同的平均情况下界。<sup>[10](https://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15451-s07/www/lecture_notes/lect0130.pdf)</sup>

## 模型内的算法

**分治法。** 归并排序——克努特（Knuth）将其归功于冯·诺依曼于 1945 年提出——将输入拆分，对两半分别排序，然后合并；它进行 Θ(n log n) 次比较，在对链表排序时是明显的赢家，因为合并不需要辅助数组。<sup>[3](https://opendatastructures.org/newhtml/ods/latex/sorting.html)</sup> 快速排序由 C. A. R. 霍尔于 1962 年发表在《计算机期刊》上，它围绕一个枢轴对输入进行划分并递归；它是原地的，不需要为元素分配额外内存，且不稳定。<sup>[4](https://dblp.dagstuhl.de/rec/journals/cj/Hoare62.html)</sup><sup> • </sup><sup>[11](https://cs.stanford.edu/people/eroberts/courses/soco/projects/tony-hoare/quicksort.html)</sup> 霍尔于 1959 年在埃利奥特计算机公司（Elliott Computers）设计了它，此前他与老板打赌六便士，说他能击败公司现有的排序算法。<sup>[12](https://www.pcgamer.com/gaming-industry/turing-award-winner-tony-hoare-computing-pioneer-who-invented-the-quicksort-algorithm-for-a-sixpence-bet-dies-at-the-age-of-92/)</sup>

**自适应稳定归并。** Timsort 由蒂姆·彼得斯（Tim Peters）于 2002 年为 Python 设计，它将输入分解为单调的游程，并在特定规则下成对合并，从而利用数据中已有的顺序。<sup>[13](https://arxiv.org/abs/1805.08612)</sup> 其 O(n log n) 最坏情况界是在没有证明的情况下宣布的，十多年间一直未被证明；2018 年发表的一个证明还建立了更精确的界 O(n + n log ρ)，其中 ρ 是游程的数量，并表明合并代价至多为 1.5nH + O(n)，其中 H 为游程长度熵。同一分析还发现了原始版本中一个被破坏的不变式，Python 对此进行了修补，Java 则绕开了它。<sup>[13](https://arxiv.org/abs/1805.08612)</sup>

**现代工程。** 近期的比较排序优化的是内存行为而非比较次数。原地并行超标量样本排序（In-place Parallel Super Scalar Samplesort，IPS4o）将快速排序推广到多枢轴，用无分支二叉决策树对元素进行分类，原地工作，并以高概率执行 O(n log n) 的工作量；它的速度在并行场景下最高为最接近的原地并行竞争对手的 3 倍，在顺序场景下最高为 BlockQuicksort 的 1.5 倍。<sup>[14](https://arxiv.org/abs/1705.02257)</sup> Rust 的 sort_unstable 基于 ipnsort，它结合了快速排序的平均情况和堆排序的最坏情况，在具有 k 个不同元素的输入上以期望 O(n log k) 时间运行。<sup>[15](https://doc.rust-lang.org/std/primitive.slice.html?search=sort)</sup>

## 平均情况常数

Ω(n log n) 这一陈述隐藏了一个线性项，其常数是一个活跃的研究问题。平均比较次数的信息论下界为 n lg n − 1.4427n + O(log n)。达到首项 n lg n 很容易；有趣的是线性项上的负常数。MergeInsertion 保持了最佳值 −1.3999，直到 2017 年一个基于双元素插入的算法达到 −1.4106，缩小了剩余差距的约四分之一。<sup>[2](https://arxiv.org/abs/1705.00849)</sup>

## 标准库中的比较排序

生产环境中的排序是混合算法，根据数据切换策略，而各语言在稳定性——即相等元素保持其原始相对顺序的保证——上有所不同：<sup>[16](https://masteringalgorithms.com/chapters/sorting-algorithms.html)</sup>

- Python 的 sorted() 和 list.sort() 使用 Timsort（自 Python 3.11 起采用 powersort 合并策略），并保证稳定；排序仅使用元素间的 < 比较。<sup>[5](https://docs.python.org/3.13/builtins/functions.html)</sup><sup> • </sup><sup>[16](https://masteringalgorithms.com/chapters/sorting-algorithms.html)</sup>
- Java 的 Arrays.sort(Object[]) 使用 Timsort，而基本类型数组使用由弗拉基米尔·雅罗斯拉夫斯基（Vladimir Yaroslavskiy）、乔恩·本特利（Jon Bentley）和约书亚·布洛赫（Joshua Bloch）提出的双枢轴快速排序，它在所有数据集上提供 O(n log n) 的性能。<sup>[6](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Arrays.html?is-external=true)</sup>
- C++ 的 std::sort 必须在最坏情况下进行 O(N log N) 次比较，这一要求由缺陷报告 LWG 713 从仅平均情况收紧而来；内省排序——从快速排序回退到堆排序——是通常的实现。<sup>[7](https://en.cppreference.com/cpp/algorithm/sort)</sup>
- Rust 两者都提供：sort（稳定）和 sort_unstable，如果比较函数未实现全序，后者会触发恐慌，因为非传递的比较器可能使算法无法推进。<sup>[15](https://doc.rust-lang.org/std/primitive.slice.html?search=sort)</sup>

稳定性对于多趟排序很重要，例如先按部门再按薪资等级排序，而各语言在名称中标示了这一保证：std::sort 和 sort_unstable 不保留相等元素的顺序，而 std::stable_sort 和 slice::sort 保留，代价是少许性能损失。<sup>[16](https://masteringalgorithms.com/chapters/sorting-algorithms.html)</sup>

## 通过离开模型来击败下界

Ω(n log n) 下界仅在比较模型中成立，即算法访问元素的唯一方式是两两比较。计数排序和基数排序则利用关于键本身的信息，不进行比较而按各位数字或数值放置元素，在适当条件下在线性时间内运行；计数排序也是稳定的。<sup>[8](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-7-counting-sort-radix-sort-lower-bounds-for-sorting/)</sup><sup> • </sup><sup>[17](https://algoparc.ics.hawaii.edu/~nodari/teaching/f26/Notes/Topic-10.html)</sup> 这些非比较方法是专用的：它们要求键具有受限的形式，例如有界范围内的整数，这就是为什么通用库仍然依赖比较排序。<sup>[18](https://web.stanford.edu/class/archive/cs/cs161/cs161.1178/notes/linear_time_sorting.pdf)</sup>

## References

1. [Decision Trees: Finding a Lower Bound on Comparison Cost, Emory University CS171](https://mathcenter.oxford.emory.edu/site/cs171/decisionTree/)
2. [Kazuo Iwama, Junichi Teruyama (2017). Improved Average Complexity for Comparison-Based Sorting. arXiv:1705.00849](https://arxiv.org/abs/1705.00849)
3. [Pat Morin. Open Data Structures, Chapter 11: Sorting Algorithms](https://opendatastructures.org/newhtml/ods/latex/sorting.html)
4. [C. A. R. Hoare (1962). Quicksort. The Computer Journal 5(1):10–15, dblp record](https://dblp.dagstuhl.de/rec/journals/cj/Hoare62.html)
5. [Built-in Functions, Python 3.13 documentation](https://docs.python.org/3.13/builtins/functions.html)
6. [java.util.Arrays, Java SE 21 API documentation](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Arrays.html?is-external=true)
7. [std::sort, cppreference.com](https://en.cppreference.com/cpp/algorithm/sort)
8. [Lecture 7: Counting Sort, Radix Sort, Lower Bounds for Sorting, MIT 6.006 Fall 2011](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/lecture-7-counting-sort-radix-sort-lower-bounds-for-sorting/)
9. [ICS 46 Notes: Linear-Time Sorting, University of California, Irvine](https://ics.uci.edu/~thornton/ics46/Notes/LinearTimeSorting/)
10. [Lecture notes on lower bounds for randomized algorithms, Carnegie Mellon University 15-451](https://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15451-s07/www/lecture_notes/lect0130.pdf)
11. [Tony Hoare: Quicksort, Stanford CS project page](https://cs.stanford.edu/people/eroberts/courses/soco/projects/tony-hoare/quicksort.html)
12. [Turing Award winner Tony Hoare dies at 92, PC Gamer](https://www.pcgamer.com/gaming-industry/turing-award-winner-tony-hoare-computing-pioneer-who-invented-the-quicksort-algorithm-for-a-sixpence-bet-dies-at-the-age-of-92/)
13. [Nicolas Auger, Vincent Jugé, Cyril Nicaud, Carine Pivoteau (2018). On the Worst-Case Complexity of TimSort. arXiv:1805.08612](https://arxiv.org/abs/1805.08612)
14. [Michael Axtmann, Sascha Witt, Daniel Ferizovic, Peter Sanders (2017). In-place Parallel Super Scalar Samplesort (IPS4o). arXiv:1705.02257](https://arxiv.org/abs/1705.02257)
15. [slice, Rust standard library documentation](https://doc.rust-lang.org/std/primitive.slice.html?search=sort)
16. [Sorting Algorithms, Mastering Algorithms](https://masteringalgorithms.com/chapters/sorting-algorithms.html)
17. [ICS 311 #10: Theoretical Limits of Sorting, O(n) Sorts, University of Hawaii](https://algoparc.ics.hawaii.edu/~nodari/teaching/f26/Notes/Topic-10.html)
18. [CS 161 Lecture 6: Sorting Lower Bounds, Bucket Sort, and Radix Sort, Stanford University](https://web.stanford.edu/class/archive/cs/cs161/cs161.1178/notes/linear_time_sorting.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Sorting, searching, and selection › Sorting and comparison lower bounds

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/comparison-sort

English original: https://www.edgepedia.org/comparison-sort
