# 排序

排序是把一组称为记录（records）的项排列成其键按某种明确定义的顺序（如数值顺序或字母顺序）出现的问题。由于键往往只占每条记录的一小部分，因此常常更高效的做法是只对键的列表排序，而不实际重排记录本身。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/sorting/)</sup> 排序是数据结构和算法设计的核心组成部分：数组一旦排好序，就可以通过二分查找在 \( O(\log N) \) 时间内定位某个条目，或证明它不存在，而线性查找需要 \( O(N) \) 时间。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup>

| 关键事实 | 细节 |
|---|---|
| 比较下界 | 任何基于比较的排序算法在最坏情况下至少需要 \( \lg(N!) \sim N \lg N \) 次比较；归并排序和堆排序达到该下界。<sup>[3](https://www.cs.princeton.edu/courses/archive/spring11/cos226/lectures/05-22Mergesort-2x2.pdf)</sup><sup> • </sup><sup>[4](https://www.cs.fsu.edu/~burmeste/slideshow/chapter9/9-1.html)</sup> |
| 插入排序 | 稳定且高度自适应；最坏情况时间 \( O(N^2) \)，空间 \( O(1) \)。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> |
| 归并排序 | 稳定但非自适应；最坏情况时间 \( O(N \log N) \)，空间 \( O(N) \)。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> |
| 快速排序 | 不稳定且非自适应；最坏情况为 \( O(N^2) \)，但期望运行时间为 \( O(N \log N) \)，实践中往往表现良好。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> |
| 计数排序与基数排序 | 计数排序对范围大小为 \( k \) 的键在 \( O(n + k) \) 时间运行；基数排序逐位应用它，位数少时保持线性。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/7392afcfcced991eeb067c822e9ace04_Nz1KZXbghj8.pdf)</sup> |
| 标准库排序 | Python 的 `list.sort()` 自 Python 2.3 起使用 Timsort，自 Python 3.11（2022年10月）起在内部使用 Powersort 的归并策略。<sup>[6](https://powersort.github.io/)</sup> |

## 排序算法的界定

大多数排序算法是比较排序：它们通过比较值来工作，反复询问一个对象小于、等于还是大于另一个对象。算法的作者并不知道被排序的确切对象或排序关系；算法只是调用一个比较方法，例如 Java 的 `compareTo`，该方法由类自身的作者定义，用来决定一个对象应出现在另一个对象之前还是之后。<sup>[7](https://courses.cs.washington.edu/courses/cse373/24sp/lessons/sorting-algorithms/)</sup>

三个性质区分目的相同的算法。若一个排序保留在排序关系下被视为相等的元素的原始顺序，它就是稳定的。稳定性对数字这类基本值无关紧要，因为相等的值可以互换；但它对记录很重要：用稳定排序按发件人姓名给电子邮件排序，会为每个发件人保留邮件的到达顺序。<sup>[7](https://courses.cs.washington.edu/courses/cse373/24sp/lessons/sorting-algorithms/)</sup> 若输入已经几乎有序时其性能有所改善，则排序是自适应的。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> 当一个排序除输入本身之外实际上不需要额外内存时，它就是原地的；这是最节省内存的一类；链表表示需要约 \( N \) 个额外字的指针空间，而复制输入的算法需要完整的第二个数组。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/sorting/)</sup>

## 初等排序与分治排序

**初等排序**在二次时间内运行。[插入排序](https://www.edgepedia.org/zh/insertion-sort)一次一个条目地构建有序数组，把每个未排序元素插入到前面的已排序部分：通过向左交换，直到它到达正确位置。<sup>[7](https://courses.cs.washington.edu/courses/cse373/24sp/lessons/sorting-algorithms/)</sup> 它稳定且高度自适应，最坏情况时间 \( O(N^2) \)，空间 \( O(1) \)。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> [选择排序](https://www.edgepedia.org/zh/selection-sort)则反复扫描未排序部分以寻找最小值，并将其移到正确位置。<sup>[8](https://pages.cs.wisc.edu/~skrentny/cs367/readings/liblitVersion/Sorting/)</sup> [冒泡排序](https://www.edgepedia.org/zh/bubble-sort)为便于参照而列入比较，通常最好避免使用。<sup>[1](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/sorting/)</sup>

**归并排序**递归地把数组分成更小的片段，对片段排序，再归并排序结果。它稳定但非自适应，最坏情况时间 \( O(N \log N) \)，空间 \( O(N) \)。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> **快速排序**围绕一个枢轴划分数组，并递归排序两段。它不稳定且非自适应，最坏情况为 \( O(N^2) \)，但期望运行时间为 \( O(N \log N) \)，实践中往往表现良好，良好的枢轴选择策略能改善其性能。<sup>[2](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> [快速排序](https://www.edgepedia.org/zh/quicksort)由[托尼·霍尔](https://www.edgepedia.org/zh/tony-hoare)（Tony Hoare）于1960年开发，并由 C. A. R. 霍尔（C. A. R. Hoare）以《快速排序》（"Quicksort"）为题发表于《计算机杂志》（*The Computer Journal*）1962年第5卷第1期第10至16页。<sup>[9](https://www.cs.pomona.edu/classes/cs62/archive/sp2026/history/sortingalgs/)</sup><sup> • </sup><sup>[10](https://academic.oup.com/comjnl/issue/5/1)</sup> 据其同事吉姆·迈尔斯（Jim Miles）的记述，霍尔在[埃利奥特兄弟公司](https://www.edgepedia.org/zh/elliott-brothers-computers)（Elliott Brothers）发明了该算法：他告诉老板自己知道一种比刚实现的方法更快的排序方法，老板回答“我跟你赌六便士，你并不知道”；快速排序确实更快。<sup>[11](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>

## 比较下界

比较排序的最坏情况运行时间绝不可能低于 \( O(N \log N) \)。<sup>[8](https://pages.cs.wisc.edu/~skrentny/cs367/readings/liblitVersion/Sorting/)</sup> 标准证明把任何基于比较的算法建模为一棵决策树：有 \( N \) 个不同值时，共有 \( N! \) 种可能顺序，因此树至少需要 \( N! \) 个叶节点，而高度为 \( h \) 的二叉树最多有 \( 2^h \) 个叶节点。最坏情况比较次数等于树的高度，所以 \( 2^h \geq N! \)，并由斯特林公式（Stirling's formula）得 \( h \geq \lg(N!) \sim N \lg N \)。<sup>[3](https://www.cs.princeton.edu/courses/archive/spring11/cos226/lectures/05-22Mergesort-2x2.pdf)</sup> 由于归并排序和堆排序都在 \( O(n \lg n) \) 时间内运行，它们是渐近最优的比较排序。<sup>[4](https://www.cs.fsu.edu/~burmeste/slideshow/chapter9/9-1.html)</sup>

该下界取决于模型。如果算法拥有关于输入初始顺序、键值分布或键的表示的信息，该下界可能不成立。<sup>[3](https://www.cs.princeton.edu/courses/archive/spring11/cos226/lectures/05-22Mergesort-2x2.pdf)</sup>

## 无需比较的排序

当键是有限范围内的整数时，从不比较两个键的算法可以突破该下界。[计数排序](https://www.edgepedia.org/zh/counting-sort)统计每个可能键的出现次数，对 \( n \) 个键范围大小为 \( k \) 的元素在 \( O(n + k) \) 时间运行；当 \( k \) 与 \( n \) 同阶时它是线性的，但随着范围增大它变得不实用。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/7392afcfcced991eeb067c822e9ace04_Nz1KZXbghj8.pdf)</sup> [基数排序](https://www.edgepedia.org/zh/radix-sort)通过把每个整数分解为某个基数 \( b \) 下的数位，并从最低有效位到最高有效位对每一位应用稳定的计数排序来扩展这一思想，对 \( d \) 位每趟耗时 \( O(n + b) \)；键的范围可以是 \( n \) 的多项式，而排序仍在线性时间内运行。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/7392afcfcced991eeb067c822e9ace04_Nz1KZXbghj8.pdf)</sup> 逐位计数排序的稳定性正是多趟方案得以成立的原因，因为每一趟都保留先前各趟建立的顺序。<sup>[12](https://ics.uci.edu/~thornton/ics46/Notes/LinearTimeSorting/)</sup> 与快速排序或归并排序等基于比较的算法不同，基数排序在某些条件下以线性时间运行，这使它适用于处理财务记录、数字信号处理和大规模数据组织等任务。<sup>[9](https://www.cs.pomona.edu/classes/cs62/archive/sp2026/history/sortingalgs/)</sup>

## 实践中的排序

**库排序是混合算法。** [Python](https://www.edgepedia.org/zh/python-programming-language) 内置的 `list.sort()` 和 `sorted()` 自 Python 2.3 起使用 Timsort；该算法利用数据中已有的顺序，并且是稳定的，能保留键相等项的原有顺序。<sup>[6](https://powersort.github.io/)</sup><sup> • </sup><sup>[13](https://docs.python.org/3.13/howto/sorting.html)</sup> 随着2022年10月发布的 Python 3.11，CPython 团队采用了 [Powersort](https://www.edgepedia.org/zh/powersort) 的归并策略，用无需浮点运算的节点幂计算取代 Timsort 的归并决策逻辑；Powersort 的归并策略也嵌入 Rust 的 `slice::sort`，作为更大混合设计的一部分。<sup>[6](https://powersort.github.io/)</sup> Powersort 及其同类 peeksort 是稳定的归并排序变体，它们利用已有的连续有序段（runs），以实际上可忽略的开销找到近乎最优的归并顺序。<sup>[14](https://arxiv.org/abs/1805.04154)</sup>

**数据库不断排序。** 在 PostgreSQL 中，除非 `ORDER BY` 子句明确要求排序，查询的输出行以未指定顺序返回；当给出多个排序表达式时，后面的值对在前面的表达式下相等的行进行排序。<sup>[15](https://www.postgresql.org/docs/16/queries-order.html)</sup> 当索引已经提供所需顺序时，查询规划器可以完全避免排序步骤；它还可以使用增量排序，在完整结果集排好序之前就返回元组，这对 `LIMIT` 查询尤其有帮助，代价是把结果拆成多个批次。<sup>[16](https://www.postgresql.org/docs/17/using-explain.html)</sup>

**硬件改变权衡。** 一项2026年的跨语言研究在 C、C++、Go、Java、PHP 和 Python 中使用相同的、含10万个元素的数组，对冒泡、堆、插入、归并、快速和选择排序进行基准测试，发现语言环境主导执行效率，C 和 C++ 最快，PHP 和 Python 显著较慢；堆排序和归并排序在各类输入模式下表现最一致，快速排序在随机数据上表现最好但对有序输入敏感，插入排序在近乎有序的数组上非常高效。<sup>[17](https://www.sciencedirect.com/science/article/pii/S2590118426000134)</sup> 在 GPU 上，一项统一的 CUDA 基准在 NVIDIA GTX 1660 SUPER 上对一千万个整数、跨随机、有序、逆序和近乎有序分布测试归并排序、快速排序、冒泡排序和基数排序，测得相对于 CPU 串行实现有7倍到38倍的加速。<sup>[18](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0342167)</sup>

**算法现在可以被自动发现。** AlphaDev 是一个用深度强化学习训练的系统，它发现了在固定长度和可变长度排序任务上优于人类基准的排序网络程序，在可变排序设置中最多探索1200万个程序后收敛到最优解。<sup>[19](https://pmc.ncbi.nlm.nih.gov/articles/PMC10247365/)</sup>

## 历史

机器排序早于电子计算机。穿孔卡片排序的概念可追溯到19世纪30年代，实用机器出现于19世纪90年代，当时[赫尔曼·霍列瑞斯](https://www.edgepedia.org/zh/herman-hollerith)（Herman Hollerith）获得了其霍列瑞斯制表机（Hollerith Tabulating Machine）的专利；这种基于卡片的装置在一场寻求更快统计1890年美国人口普查方法的竞赛中胜出。<sup>[20](https://computerhistory.org/blog/the-bourne-collection-online-search-is-older-than-you-think/)</sup><sup> • </sup><sup>[21](https://www.computerhistory.org/tdih/january/8/)</sup> 霍列瑞斯的制表机和分拣机首先应用一种基于某一列穿孔的鸽巢排序；依次对多列应用它便得到基数10排序。<sup>[22](https://www.cs.cornell.edu/courses/JavaAndDS/files/sort6RadixHistory.pdf)</sup> 在穿孔卡片时代，一台卡片分拣机一次只能按一位数字排序，因此要对六位员工编号排序，需要把数千张卡片送过分拣机六次。<sup>[23](https://computerhistory.org/blog/about-the-computer-history-museums-ibm-1401-machines/)</sup>

## References

1. [Sorting, MIT OpenCourseWare, Foundations of Software Engineering (Fall 2000)](https://ocw.mit.edu/courses/1-124j-foundations-of-software-engineering-fall-2000/pages/lecture-notes/sorting/)
2. [Sorting Algorithms, CS 2110, Cornell University (Spring 2026)](https://www.cs.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)
3. [Two Classic Sorting Algorithms, COS 226, Princeton University](https://www.cs.princeton.edu/courses/archive/spring11/cos226/lectures/05-22Mergesort-2x2.pdf)
4. [Lower Bounds for Sorting, Florida State University](https://www.cs.fsu.edu/~burmeste/slideshow/chapter9/9-1.html)
5. [Counting Sort, Radix Sort, Lower Bounds for Sorting, MIT 6.006 (Fall 2011)](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-fall-2011/7392afcfcced991eeb067c822e9ace04_Nz1KZXbghj8.pdf)
6. [Powersort: Nearly-Optimal Adaptive Stable Sorting](https://powersort.github.io/)
7. [Sorting Algorithms, CSE 373, University of Washington (Spring 2024)](https://courses.cs.washington.edu/courses/cse373/24sp/lessons/sorting-algorithms/)
8. [Sorting, CS 367 Reading, University of Wisconsin–Madison](https://pages.cs.wisc.edu/~skrentny/cs367/readings/liblitVersion/Sorting/)
9. [History of Sorting Algorithms, CS 62, Pomona College](https://www.cs.pomona.edu/classes/cs62/archive/sp2026/history/sortingalgs/)
10. [The Computer Journal, Volume 5, Issue 1 (1962), Oxford Academic](https://academic.oup.com/comjnl/issue/5/1)
11. [Turing Award winner Tony Hoare, computing pioneer who invented the Quicksort algorithm, 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/)
12. [Linear-Time Sorting, ICS 46, University of California, Irvine](https://ics.uci.edu/~thornton/ics46/Notes/LinearTimeSorting/)
13. [Sorting Techniques, Python 3.13 documentation](https://docs.python.org/3.13/howto/sorting.html)
14. [Nearly-Optimal Mergesorts: Fast, Practical Sorting Methods That Optimally Adapt to Existing Runs, arXiv](https://arxiv.org/abs/1805.04154)
15. [Sorting Rows (ORDER BY), PostgreSQL 16 Documentation](https://www.postgresql.org/docs/16/queries-order.html)
16. [Using EXPLAIN, PostgreSQL 17 Documentation](https://www.postgresql.org/docs/17/using-explain.html)
17. [Cross-language evaluation of sorting algorithms on various input patterns: A comparative study, Journal of Computer Languages (2026)](https://www.sciencedirect.com/science/article/pii/S2590118426000134)
18. [Performance evaluation of GPU-based parallel sorting algorithms, PLOS One](https://journals.plos.org/plosone/article?id=10.1371%2Fjournal.pone.0342167)
19. [Faster sorting algorithms discovered using deep reinforcement learning, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC10247365/)
20. [The Bourne Collection: Online Search Is Older than You Think!, Computer History Museum](https://computerhistory.org/blog/the-bourne-collection-online-search-is-older-than-you-think/)
21. [January 8: Herman Hollerith Receives a Patent for the Hollerith Tabulating Machine, Computer History Museum](https://www.computerhistory.org/tdih/january/8/)
22. [Radix Sort History, Cornell University](https://www.cs.cornell.edu/courses/JavaAndDS/files/sort6RadixHistory.pdf)
23. [About the Computer History Museum's IBM 1401 Machines, Computer History Museum](https://computerhistory.org/blog/about-the-computer-history-museums-ibm-1401-machines/)

---
*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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/sorting

English original: https://www.edgepedia.org/sorting
