# 快速选择

**快速选择**（Quickselect，亦称霍尔（Hoare）选择算法）是一种选择算法，用于寻找无序数组中的第 k 小元素，或第 k 顺序统计量。它完全像快速排序那样围绕枢轴划分数组，但不是递归进入两侧，而是只递归进入包含目标元素的一侧，从而将期望运行时间从 O(n log n) 降至 O(n)。<sup>[1](https://doi.org/10.1145/3649839)</sup><sup> • </sup><sup>[2](https://link.springer.com/article/10.1007/s00034-023-02370-x)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由托尼·霍尔（Tony Hoare）作为快速排序的副产品设计；以算法 65（FIND）发表于《ACM通讯》（Communications of the ACM），1961 年 7 月<sup>[3](https://academic.oup.com/comjnl/pages/65th-anniversary-perspective-ym2)</sup><sup> • </sup><sup>[4](https://www.csauthors.net/tony-hoare/)</sup> |
| 期望时间 | 使用随机枢轴时为 O(n)；通常的霍尔或洛穆托（Lomuto）划分实现在最坏情况下为 O(n²)<sup>[1](https://doi.org/10.1145/3649839)</sup><sup> • </sup><sup>[5](https://www.baeldung.com/cs/quickselect)</sup> |
| 最坏情况线性变体 | 中位数的中位数枢轴选择（布卢姆（Blum）、弗洛伊德（Floyd）、普拉特（Pratt）、里维斯特（Rivest）与塔扬（Tarjan），1973）保证线性时间，至多 5.4305n 次比较<sup>[6](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup> |
| 最佳平均界 | 弗洛伊德与里维斯特的 SELECT（1975）平均需要 n + min(i, n − i) + o(n) 次比较<sup>[7](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup> |
| 实际最坏情况 | 内省选择（马瑟（Musser），1997）限制划分深度，并切换到布卢姆–弗洛伊德–普拉特–里维斯特–塔扬算法，从而以接近霍尔的速度获得线性最坏情况<sup>[8](https://onlinelibrary.wiley.com/doi/abs/10.1002/%28SICI%291097-024X%28199708%2927%3A8%3C983%3A%3AAID-SPE117%3E3.0.CO;2-%23)</sup> |
| 库使用 | NumPy 的 `partition`、C++ 的 `std::nth_element` 和 Rust 的 `select_nth_unstable` 都提供基于内省选择的选择功能<sup>[9](https://numpy.org/doc/2.2/reference/generated/numpy.partition.html)</sup><sup> • </sup><sup>[10](https://eel.is/c++draft/alg.nth.element)</sup><sup> • </sup><sup>[11](https://doc.rust-lang.org/std/primitive.slice.html)</sup> |

## 算法如何工作

快速选择接收一个数组和一个索引 k，并返回若数组已排序时会位于位置 k 的元素。每一轮选择一个枢轴并划分数组，使所有更小的元素排在枢轴之前，所有相等或更大的元素排在枢轴之后；此时枢轴的秩 r 已知。如果 r 等于 k，枢轴就是答案。如果 k 更小，搜索在左分区继续；如果更大，则在右分区继续。与递归进入两侧的快速排序不同，快速选择丢弃不可能包含答案的一侧。<sup>[2](https://link.springer.com/article/10.1007/s00034-023-02370-x)</sup><sup> • </sup><sup>[1](https://doi.org/10.1145/3649839)</sup>

由于每一轮只有一侧保留下来，期望工作量形成一个递减级数，而不是快速排序的 n log n 总量。该算法通常就地实现，并且作为副作用，它会围绕被选元素对数据做部分排列。<sup>[2](https://link.springer.com/article/10.1007/s00034-023-02370-x)</sup> 递归调用也可以写成循环，因为每个阶段只剩下一个子问题。<sup>[1](https://doi.org/10.1145/3649839)</sup>

**枢轴选择决定最坏情况。** 采用固定的确定性规则，例如总是取第一个元素，对抗性输入或仅仅是已排序输入，都可能迫使每次划分只移除一个元素，从而给出 O(n²) 时间。均匀随机选择枢轴可在无论输入如何的情况下给出期望线性时间，并且也消除了攻击者可利用来反复触发二次时间路径的固定策略。<sup>[5](https://www.baeldung.com/cs/quickselect)</sup> 霍尔划分比洛穆托划分执行略多的比较，但平均交换更少；两种方案下的渐近复杂度相同。<sup>[5](https://www.baeldung.com/cs/quickselect)</sup>

## 中位数的中位数与有保证的线性时间

[中位数的中位数](https://www.edgepedia.org/zh/median-of-medians)技术由[曼努埃尔·布卢姆](https://www.edgepedia.org/zh/manuel-blum)（Manuel Blum）、[罗伯特·W·弗洛伊德](https://www.edgepedia.org/zh/robert-w-floyd)（Robert W. Floyd）、沃恩·普拉特（Vaughan Pratt）、罗纳德·L·里维斯特（Ronald L. Rivest）和罗伯特·E·塔扬（Robert E. Tarjan）于 1973 年发表；它选择的枢轴保证相当居中：所选枢轴小于剩余元素中最高的 30%，并大于最低的 30%，因此每一轮至少移除输入的一个固定比例。这给出最坏情况线性运行时间；他们的分析表明，所需比较次数从不超过 5.4305n 次。<sup>[6](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup><sup> • </sup><sup>[5](https://www.baeldung.com/cs/quickselect)</sup>

这一保证是有代价的。在 n = 20,000 个元素上进行的直接比较发现，随机选择为找到中位数每个元素约需 3.21 次比较，而中位数的中位数需要 8.15 次，对于同一答案大约是其两倍半。<sup>[12](https://www.algorithms-data-structures.com/essays/what-derandomising-costs/)</sup> 这一差距解释了为什么该确定性算法被视为理论里程碑和后备方案，而不是默认枢轴规则。<sup>[13](https://arxiv.org/abs/1606.00484)</sup>

## 弗洛伊德与里维斯特的 SELECT

1975 年，罗伯特·W·弗洛伊德与罗纳德·L·里维斯特发表了 SELECT；这一改进算法抽取一个小随机样本，并在划分之前用两个样本元素 u 和 v 夹住目标秩。其期望比较次数为 n + min(i, n − i) + o(n)，其中 i 是所请求的秩，作者还推导出与此值相差不超过 9% 的下界。<sup>[7](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup> 他们的实现功能上等价于霍尔的 FIND，但平均显著更快；实测求取 n 个元素的中位数所需的时间，当 n 为 500、1,000、5,000 和 10,000 时，耗时分别为 89、141、493 和 877 毫秒，而 FIND 为 104、197、1,029 和 1,964 毫秒。<sup>[14](https://doi.org/10.1145/360680.360694)</sup> 后来的分析确认，SELECT 的若干版本平均且以高概率至多需要 n + min(k, n − k) + o(n) 次比较，即使存在相等元素也是如此。<sup>[15](https://arxiv.org/abs/cs/0204033)</sup>

## 内省选择与库实现

**内省选择**结合了两者。戴维·R·马瑟（David R. Musser）1997 年的论文限制划分深度，并对超过限制的子问题切换到具有更好最坏情况界的算法；对于选择问题，霍尔 FIND 与布卢姆–弗洛伊德–普拉特–里维斯特–塔扬算法的混合在实践中与 FIND 一样快，却具有线性最坏情况时间界。<sup>[8](https://onlinelibrary.wiley.com/doi/abs/10.1002/%28SICI%291097-024X%28199708%2927%3A8%3C983%3A%3AAID-SPE117%3E3.0.CO;2-%23)</sup>

通过划分进行选择出现在若干语言的标准库中：

- NumPy 的 `numpy.partition`（在 1.8.0 版中加入）重排一个副本，使第 k 个元素处于其最终排序位置，更小的元素在它之前，相等或更大的元素在它之后；每个分区内部的顺序未定义。其 `introselect` 类型列出为最坏情况 O(n)、零工作空间且不稳定。<sup>[9](https://numpy.org/doc/2.2/reference/generated/numpy.partition.html)</sup>
- C++ 标准的 `nth_element` 要求非并行重载平均具有线性复杂度，实现通常使用内省选择。<sup>[10](https://eel.is/c++draft/alg.nth.element)</sup><sup> • </sup><sup>[16](https://en.cppreference.com/cpp/algorithm/ranges/nth_element)</sup>
- Rust 的 `select_nth_unstable` 于 1.49.0 版成为稳定功能，围绕选定索引就地重排切片，并返回三路划分；其当前实现是基于 ipnsort 的内省选择，回退算法是使用图基（Tukey）的 Ninther（九数取中）进行枢轴选择的中位数的中位数算法，保证所有输入下的线性运行时间。<sup>[11](https://doc.rust-lang.org/std/primitive.slice.html)</sup>

较早的 Rust 实现是普通快速选择，而一份 2022 年的缺陷报告记录了将其驱动到二次时间的输入，尽管文档声称具有线性最坏情况；这说明了内省选择回退为何重要。<sup>[17](https://github.com/rust-lang/rust/issues/102451)</sup>

## 枢轴采样与改进

采样改善平均情况。马丁内斯与罗拉（Martínez and Roura）表明，快速排序式算法中枢轴选择的最优样本量按 a·√n + o(√n) 增长，并给出了常数 a 的闭式表达式；他们对快速选择也得到了类似结果。<sup>[18](https://epubs.siam.org/doi/10.1137/S0097539700382108)</sup> 三数取中枢轴是生产系统中最常用的版本。<sup>[19](https://www.colibri.udelar.edu.uy/jspui/bitstream/20.500.12008/3505/1/TR0414.pdf)</sup> 对雅罗斯拉夫斯基（Yaroslavskiy）双枢轴划分下的快速选择进行分析——该划分可加速快速排序——发现这种改进并未延续：对于随机秩的顺序统计量，较旧划分方法下的快速选择略优于双枢轴变体。<sup>[20](https://arxiv.org/abs/1306.3819)</sup>

## 应用

快速选择及其变体被描述为实际生产实现中最常用的选择算法，包括图像处理中的中值滤波——其中像素窗口的中位数无需对窗口排序即可计算。<sup>[2](https://link.springer.com/article/10.1007/s00034-023-02370-x)</sup> 同样的划分思想还用于 k 近邻搜索、机器学习中基于百分位数的特征选择，以及数据库中的近似中位数与分位数查询。<sup>[21](https://mexicodev.org/en/dsa-course/quickselect/index.html)</sup>

## References

1. [Hopping Proofs of Expectation-Based Properties: Applications to Skiplists and Security Proofs, Proc. ACM Program. Lang. (2024)](https://doi.org/10.1145/3649839)
2. [Optimized Algorithms and Hardware Implementation of Median Filter for Image Processing, Circuits, Systems, and Signal Processing (2023)](https://link.springer.com/article/10.1007/s00034-023-02370-x)
3. [The Computer Journal: 65th Anniversary Perspectives on Hoare's Quicksort](https://academic.oup.com/comjnl/pages/65th-anniversary-perspective-ym2)
4. [Tony Hoare publication record, CSAuthors](https://www.csauthors.net/tony-hoare/)
5. [Complexity Analysis of QuickSelect, Baeldung on Computer Science](https://www.baeldung.com/cs/quickselect)
6. [Blum, Floyd, Pratt, Rivest, Tarjan (1973). Time bounds for selection. Journal of Computer and System Sciences 7(4), 448–461.](https://www.sciencedirect.com/science/article/pii/S0022000073800339)
7. [Floyd, R. W., Rivest, R. L. (1975). Expected time bounds for selection. Communications of the ACM 18(3), 165–172.](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)
8. [Musser, D. R. (1997). Introspective Sorting and Selection Algorithms. Software: Practice and Experience 27(8), 983–993.](https://onlinelibrary.wiley.com/doi/abs/10.1002/%28SICI%291097-024X%28199708%2927%3A8%3C983%3A%3AAID-SPE117%3E3.0.CO;2-%23)
9. [numpy.partition, NumPy v2.2 Manual](https://numpy.org/doc/2.2/reference/generated/numpy.partition.html)
10. [C++ Working Draft, [alg.nth.element]](https://eel.is/c++draft/alg.nth.element)
11. [slice::select_nth_unstable, Rust Standard Library Documentation](https://doc.rust-lang.org/std/primitive.slice.html)
12. [What derandomising costs, Algorithms & Data Structures](https://www.algorithms-data-structures.com/essays/what-derandomising-costs/)
13. [Alexandrescu, A. (2016). Fast Deterministic Selection. arXiv:1606.00484.](https://arxiv.org/abs/1606.00484)
14. [Floyd, R. W., Rivest, R. L. (1975). Algorithm 489: The Algorithm SELECT. Communications of the ACM 18(3), 173.](https://doi.org/10.1145/360680.360694)
15. [Kiwiel, K. C. (2002). Randomized selection revisited. arXiv:cs/0204033.](https://arxiv.org/abs/cs/0204033)
16. [std::ranges::nth_element, cppreference.com](https://en.cppreference.com/cpp/algorithm/ranges/nth_element)
17. [select_nth_unstable has quadratic worst-case time complexity; docs claim it should be linear, rust-lang/rust issue #102451](https://github.com/rust-lang/rust/issues/102451)
18. [Martínez, C., Roura, S. Optimal Sampling Strategies in Quicksort and Quickselect. SIAM Journal on Computing.](https://epubs.siam.org/doi/10.1137/S0097539700382108)
19. [PEDECIBA Informática technical report on quickselect pivot sampling](https://www.colibri.udelar.edu.uy/jspui/bitstream/20.500.12008/3505/1/TR0414.pdf)
20. [Analysis of Quickselect under Yaroslavskiy's Dual-Pivoting Algorithm. arXiv:1306.3819.](https://arxiv.org/abs/1306.3819)
21. [Quickselect and order statistics, Data Structures & Algorithms, From Scratch](https://mexicodev.org/en/dsa-course/quickselect/index.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 20, 2026; 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 › Selection and order statistics

Copyright 2026 Edgepedia

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

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

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