# 快速排序

**快速排序**是一种分治排序算法，它选取一个称为基准的元素，重排数组使较小元素排在基准之前、较大元素排在基准之后（即划分步骤），然后递归地对两侧进行排序。该算法由[托尼·霍尔](https://www.edgepedia.org/zh/tony-hoare)（Tony Hoare）于1959年发明，1961年发表，至今仍是最广泛使用的通用排序方法之一。<sup>[1](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><sup> • </sup><sup>[2](https://cir.nii.ac.jp/crid/1363951794331789568)</sup>

| 关键事实 | 详情 |
|---|---|
| 发明者 | C. A. R.（托尼）霍尔，当时是埃利奥特兄弟公司（Elliott Brothers）的程序员，1959年发明，据称源于与老板的六便士赌约<sup>[1](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> |
| 首次发表 | “算法64：快速排序”，《ACM通讯》4(7)：321，1961年7月<sup>[2](https://cir.nii.ac.jp/crid/1363951794331789568)</sup> |
| 最佳情况 | 约N lg N次比较，当每个基准将其子数组均匀分割时<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> |
| 最差情况 | 约1/2 N²次比较，当每次划分都极度不平衡时（基准为最小值或最大值）<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup><sup> • </sup><sup>[4](https://www.cs.bilkent.edu.tr/~ugur/teaching/cs473/material/06a_AnalysisOfQuicksort.pdf)</sup> |
| 平均情况 | 使用随机基准或预先打乱时，2N ln N次比较<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> |
| 空间与稳定性 | 原地排序，除递归外仅需常数级额外空间；不稳定且非自适应<sup>[5](https://atharaq.github.io/data-structures-f23/lesson20.html)</sup><sup> • </sup><sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> |
| 库中的作用 | C语言`qsort`传统的基础，并通过内省排序成为C++ `std::sort`和Rust `sort_unstable`的基础<sup>[7](https://gallium.inria.fr/~maranget/X/421/09/bentley93engineering.pdf)</sup><sup> • </sup><sup>[8](https://en.cppreference.com/cpp/algorithm/sort)</sup><sup> • </sup><sup>[9](https://doc.rust-lang.org/std/primitive.slice.html)</sup> |

## 工作原理

该算法遵循三个步骤：按某个基准进行划分，对基准左侧的元素进行快速排序，对基准右侧的元素进行快速排序。<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> 划分是一次线性遍历，将每个元素与基准比较并相应地重排子数组；基准一旦落到最终位置，就不再参与后续考虑。<sup>[5](https://atharaq.github.io/data-structures-f23/lesson20.html)</sup>

基准的选择决定了两个子问题的大小，因而决定了运行时间。<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> 将列表恰好对半分割的基准给出最佳情况。作为子数组最小值或最大值的基准在每次递归调用时产生大小为1和n−1的划分，得到递推式T(n) = T(n−1) + Θ(n)，求和为Θ(n²)。<sup>[4](https://www.cs.bilkent.edu.tr/~ugur/teaching/cs473/material/06a_AnalysisOfQuicksort.pdf)</sup> 采用首元素或末元素作为基准的规则时，已排序或逆序排列的数组恰好触发这种退化行为，即使不需要任何重排。<sup>[4](https://www.cs.bilkent.edu.tr/~ugur/teaching/cs473/material/06a_AnalysisOfQuicksort.pdf)</sup><sup> • </sup><sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup>

**随机化。** 随机选择基准，或在排序前打乱数组，使得无论输入顺序如何都难以出现糟糕的划分，并产生期望的2N ln N次比较。<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> 使用合理基准的期望运行时间为O(N log N)，典型性能远更接近最佳情况而非最差情况。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> 一种改进方法是检查每次划分是否合理平衡，例如最差为1/3–2/3分割，否则用新的随机基准重新划分。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> 理想基准——子数组的中位数——可以通过O(N)算法（如中位数的中位数）来近似，但额外的计算成本往往超过更简单的采样方案所节省的开销。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup>

## 实际性能

尽管在某些情况下比归并排序进行更多比较，快速排序在实践中通常更快，这一特性归因于其内存访问模式和缓存行为。<sup>[3](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)</sup> 1994年一项对[希尔排序](https://www.edgepedia.org/zh/shellsort)、快速排序和归并排序基本实现在1,000到2 × 10⁶个元素数据上的实证研究发现，快速排序在1,000以上的所有测试规模中都优于其他两种算法。<sup>[10](https://pure.ku.edu.kw/en/publications/an-empirical-study-of-the-run-time-behavior-of-quicksort-shellsor/)</sup> 后来一项在MATLAB中进行的比较研究发现，快速排序在小数组上更快，归并排序在大数组上更快，两种算法在最佳和平均情况下均为O(n log n)，快速排序在最差情况下为O(n²)；同一研究指出，归并排序需要辅助内存，使得在缓存局部性重要的场合快速排序更为可取。<sup>[11](https://www.sciencedirect.com/science/article/pii/S1877050920312667)</sup>

快速排序在划分过程中的长距离交换使其不稳定，即相等元素可能被重排，且没有简单方法能在不损失性能的情况下恢复稳定性。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup>

## 历史

霍尔于1959年在[埃利奥特兄弟公司](https://www.edgepedia.org/zh/elliott-brothers-computers)——一家英国小型计算机制造商——工作时设计了该算法，此前他告诉老板自己知道一种比公司刚实现的排序方法更快的排序方法；老板以六便士打赌他并不知道这样的方法，结果快速排序被证明更快。<sup>[1](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> 他于1961年7月在《ACM通讯》上以“算法64：快速排序”为题发表，随后于1962年在《计算机杂志》上发表了更完整的论文。<sup>[2](https://cir.nii.ac.jp/crid/1363951794331789568)</sup><sup> • </sup><sup>[7](https://gallium.inria.fr/~maranget/X/421/09/bentley93engineering.pdf)</sup> 霍尔后来设计了用于证明程序正确性的[霍尔逻辑](https://www.edgepedia.org/zh/hoare-logic)，并参与共同设计了ALGOL W；他于1980年获得[图灵奖](https://www.edgepedia.org/zh/turing-award)，2000年被封为爵士。<sup>[1](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> 计算机历史博物馆将快速排序列为世界上使用最广泛的排序方法之一。<sup>[12](https://www.computerhistory.org/timeline/ai-robotics/)</sup>

## 工程实现与变体

**本特利–麦克罗伊qsort。** 1993年，AT&T贝尔实验室的乔恩·L·本特利（Jon L. Bentley）和M·道格拉斯·麦克罗伊（M. Douglas McIlroy）描述了一个重建的C库`qsort`，其动机来自一位同事的发现：现有的快速排序在结构化输入（如“管风琴”数组：1, 2, ..., n, n, ..., 2, 1）上以Θ(n²)时间运行。他们的版本通过采样选择划分元素（3取中，或9个样本的图基九数取中），对小规模子数组切换到插入排序，并采用解决迪杰斯特拉荷兰国旗问题的三路划分方案，将等于基准的元素归为一组，高效处理大量重复键。<sup>[7](https://gallium.inria.fr/~maranget/X/421/09/bentley93engineering.pdf)</sup><sup> • </sup><sup>[13](https://algs4.cs.princeton.edu/lectures/keynote/23Quicksort-2x2.pdf)</sup>

**保证最差情况。** 普通快速排序的O(n²)最差情况在库中是不可接受的，因此生产实现会切换算法。C++ `std::sort`通常用内省排序实现，它在O(N log N)次比较内处理所有输入，且无平均情况的额外开销；1998年的一份缺陷报告（LWG 713）将标准的复杂度要求从平均情况收紧到最差情况。<sup>[8](https://en.cppreference.com/cpp/algorithm/sort)</sup> Rust的`sort_unstable`使用基于卢卡斯·伯格多尔（Lukas Bergdoll）和奥森·彼得斯（Orson Peters）的ipnsort的内省选择实现，结合了快速排序的快速平均情况和堆排序的快速最差情况，在完全有序和逆序输入上以线性时间运行。<sup>[9](https://doc.rust-lang.org/std/primitive.slice.html)</sup> 相比之下，glibc的`qsort`尝试分配辅助内存并使用归并排序，如果分配失败则回退到较慢的算法。<sup>[14](https://sourceware.org/glibc/manual/2.43/html_node/Array-Sort-Function.html)</sup>

**混合与多基准方案。** 快速归并排序——将快速排序的划分与用作基本情况排序器的归并排序相结合——在使用√n取中基准选择时，平均最多需要n log n − 1.3999n + o(n)次元素比较，接近约n log n − 1.44n的信息论下界，实验表明它优于C++内省排序和Java的双基准快速排序。<sup>[15](https://arxiv.org/abs/1804.10062)</sup> 双基准快速排序使用两个基准将数组分为三部分，其平均情况分析表明相比经典单基准快速排序在比较次数上有所节省。<sup>[16](https://arxiv.org/abs/1304.0988)</sup>

## 快速排序与归并排序的比较

两种算法共享分治结构，但区别在于工作发生的位置：快速排序先划分再递归，而归并排序先递归再合并。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup> 归并排序是稳定的，保证O(N log N)最差情况时间，但需要O(N)辅助空间；快速排序是原地排序，实践中通常更快，但不稳定且最差情况为O(N²)。<sup>[6](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup><sup> • </sup><sup>[5](https://atharaq.github.io/data-structures-f23/lesson20.html)</sup> 这些权衡解释了标准库之间的分歧：C++和Rust接受经内省排序或内省选择加固的快速排序家族方法，而glibc的`qsort`在内存允许时更倾向于归并排序。<sup>[8](https://en.cppreference.com/cpp/algorithm/sort)</sup><sup> • </sup><sup>[9](https://doc.rust-lang.org/std/primitive.slice.html)</sup><sup> • </sup><sup>[14](https://sourceware.org/glibc/manual/2.43/html_node/Array-Sort-Function.html)</sup>

## References

1. [Turing Award winner Tony Hoare, computing pioneer who invented the Quicksort algorithm for a sixpence bet, dies at the age of 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/)
2. [Algorithm 64: Quicksort, C. A. R. Hoare, Communications of the ACM 4(7): 321, July 1961 (CiNii Research record)](https://cir.nii.ac.jp/crid/1363951794331789568)
3. [Quicksort Study Guide, COS 226, Princeton University](https://www.cs.princeton.edu/courses/archive/fall23/cos226/lectures/study/23Quicksort.html)
4. [Analysis of Quicksort, CS 473 lecture notes, Bilkent University](https://www.cs.bilkent.edu.tr/~ugur/teaching/cs473/material/06a_AnalysisOfQuicksort.pdf)
5. [Data Structures Lesson 20: Quicksort analysis, Professor Abdul-Quader's Course Notes](https://atharaq.github.io/data-structures-f23/lesson20.html)
6. [7. Sorting Algorithms, CS 2110, Cornell University](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)
7. [Jon L. Bentley and M. Douglas McIlroy (1993). Engineering a Sort Function. Software: Practice and Experience 23(11): 1249–1265](https://gallium.inria.fr/~maranget/X/421/09/bentley93engineering.pdf)
8. [std::sort, cppreference.com](https://en.cppreference.com/cpp/algorithm/sort)
9. [slice, Rust standard library documentation](https://doc.rust-lang.org/std/primitive.slice.html)
10. [Mansoor Sarwar, M. H. A. Jaragh, and Mike Wind (1994). An empirical study of the run-time behavior of quicksort, Shellsort and mergesort for medium to large size data. Computer Languages 20(2): 127–134](https://pure.ku.edu.kw/en/publications/an-empirical-study-of-the-run-time-behavior-of-quicksort-shellsor/)
11. [Comparative Study of Two Divide and Conquer Sorting Algorithms: Quicksort and Mergesort, ScienceDirect (Procedia Computer Science)](https://www.sciencedirect.com/science/article/pii/S1877050920312667)
12. [AI & Robotics, Timeline of Computer History, Computer History Museum](https://www.computerhistory.org/timeline/ai-robotics/)
13. [Engineering a system sort (in 1993), Bentley-McIlroy quicksort, Princeton algs4 lecture slides](https://algs4.cs.princeton.edu/lectures/keynote/23Quicksort-2x2.pdf)
14. [Array Sort Function, The GNU C Library manual](https://sourceware.org/glibc/manual/2.43/html_node/Array-Sort-Function.html)
15. [QuickMergesort: Practically Efficient Constant-Factor Optimal Sorting, arXiv:1804.10062](https://arxiv.org/abs/1804.10062)
16. [Average Case and Distributional Analysis of Dual-Pivot Quicksort, arXiv:1304.0988](https://arxiv.org/abs/1304.0988)

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

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

Copyright 2026 Edgepedia

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

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

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