# 选择算法

选择算法在n个无序元素的集合中找出给定秩的元素，即第k小或第i小的元素，理想情况下无需对整个集合排序。在比较模型中，唯一能揭示输入信息的操作是两两比较，任务是用尽可能少的比较次数完成这一工作。第k小的元素称为顺序统计量，而中位数——即中间秩——是研究最多的情形。<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 问题 | 给定n个互不相同的数和整数i，用尽可能少的比较确定第i小的元素；第i小的元素恰好大于其他i − 1个元素<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup> |
| 平凡下界 | 任何算法都必须使用多于n − 1次比较，因为每个未被选中的元素都必须被证明小于或大于答案<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup> |
| 首个最坏情况线性算法 | PICK，由Blum、Floyd、Pratt、Rivest和Tarjan（1973）提出，至多用5.4305n次比较选出第i小的元素<sup>[2](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup> |
| 最佳实际期望界 | Floyd和Rivest的SELECT（1975）平均使用n + min(i, n − i) + o(n)次比较，并证明了与该值相差不超过9%的下界<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup> |
| 快速选择 | Hoare的FIND（1961）期望线性时间运行，但最坏情况为Θ(n²)<sup>[3](https://refs.drheap.org/hoare1961algorithm/)</sup><sup> • </sup><sup>[4](https://www.eng.biu.ac.il/~wimers/files/courses/Data_Structures_and_Algorithms_I/Materials/Introduction_to_algorithms-3rd%20Edition.pdf)</sup> |
| 库支持状况 | C++的`std::nth_element`平均线性；NumPy的`partition`使用内省选择，最坏情况O(n)<sup>[5](https://www.inf.pucrs.br/flash/progeng2/cppreference/w/cpp/algorithm/nth_element.html)</sup><sup> • </sup><sup>[6](https://numpy.org/doc/1.25/reference/generated/numpy.partition.html)</sup> |

## 问题

选择问题要求找出如果数组被排序后将位于位置k的元素，而不必付出完整排序的代价。基于划分的变体还会重排数组，使位置k之前的每个元素不大于答案，位置k之后的每个元素不小于答案；这正是C++ `nth_element`和NumPy `partition`所实现的形式。<sup>[5](https://www.inf.pucrs.br/flash/progeng2/cppreference/w/cpp/algorithm/nth_element.html)</sup><sup> • </sup><sup>[6](https://numpy.org/doc/1.25/reference/generated/numpy.partition.html)</sup>该问题在数据组织中具有基础性地位，应用包括统计学中的中位数、数据库中的前N查询以及信号处理中的中值滤波。<sup>[7](https://pmc.ncbi.nlm.nih.gov/articles/PMC4217089/)</sup>

由于选择算法必须对除答案外的每个元素确定其位于秩i之下还是之上，因此至少需要n − 1次比较；Floyd和Rivest将其表述为平凡下界f(i, n) > n − 1。<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup>

## 快速选择

**Hoare的FIND。** C. A. R. Hoare是一位英国计算机科学家，1959年在Elliott Brothers公司发明了[快速排序](https://www.edgepedia.org/zh/quicksort)，后来获得1980年[图灵奖](https://www.edgepedia.org/zh/turing-award)。1961年，他在《ACM通讯》上发表了[算法](https://www.edgepedia.org/zh/algorithm)65——FIND，作为快速排序的副产品。<sup>[3](https://refs.drheap.org/hoare1961algorithm/)</sup><sup> • </sup><sup>[8](https://academic.oup.com/comjnl/pages/65th-anniversary-perspective-ym2)</sup>该算法现在通常称为快速选择，它选择一个枢轴元素，将数组划分为小于等于枢轴的元素和大于等于枢轴的元素，然后只在已知包含第k小元素的那一侧递归，这与快速排序不同，后者在两侧都递归。<sup>[9](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)</sup>

枢轴的选择决定了性能在O(n)和O(n²)之间的变化。使用随机枢轴时期望运行时间为Θ(n)，但最坏情况为Θ(n²)——即使是在寻找最小值时，如果算法不幸每次都围绕剩余最大元素进行划分，也可能出现这种情况。<sup>[4](https://www.eng.biu.ac.il/~wimers/files/courses/Data_Structures_and_Algorithms_I/Materials/Introduction_to_algorithms-3rd%20Edition.pdf)</sup>常见的枢轴启发式方法，例如取1到9个元素的小样本的中位数，平均效果良好，但方差较大，且不提供最坏情况保证。<sup>[9](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)</sup>

## 最坏情况线性时间：中位数之中位数

第一个具有保证线性最坏情况的选择方法来自[Manuel Blum](https://www.edgepedia.org/zh/manuel-blum)、Robert Floyd、Vaughan Pratt、Ronald Rivest和Robert Tarjan于1973年发表的论文《选择的时间界》。他们的算法PICK通过逐步丢弃输入中已知对目标秩而言过大或过小的子集来工作，每个被丢弃的子集至少包含剩余元素的四分之一。<sup>[2](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup><sup> • </sup><sup>[10](https://www.cs.wmich.edu/gupta/teaching/cs6310/lectureNotes_cs6310/BFPRT73.pdf)</sup>

枢轴是精心选择的，而非采样的。数组被排列成固定长度c的列，每列排序，算法递归地选择列中位数的中位数作为划分元素；分析表明c必须至少为5才能保证线性时间。<sup>[10](https://www.cs.wmich.edu/gupta/teaching/cs6310/lectureNotes_cs6310/BFPRT73.pdf)</sup>以五个元素为一组，已知枢轴每一侧都至少有3⌊n/10⌋个元素，因此递归调用最多作用于3n/4个元素，得到递推式T(n) = T(n/5) + T(7n/10) + Θ(n)，解得线性时间。<sup>[11](https://courses.csail.mit.edu/6.046/spring03/handouts/lec6.pdf)</sup>PICK的调优版本从不需要超过5.4305n次比较，同一篇论文还证明了所需比较次数的新下界。<sup>[2](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup>

尽管有这一保证，中位数之中位数算法在实践中很少使用，因为它大量进行元素比较尤其是交换，而且由于n前面的常数很大，运行缓慢。<sup>[9](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)</sup><sup> • </sup><sup>[11](https://courses.csail.mit.edu/6.046/spring03/handouts/lec6.pdf)</sup>

## Floyd和Rivest的SELECT

1975年，斯坦福大学的[Robert W. Floyd](https://www.edgepedia.org/zh/robert-w-floyd)和Ronald L. Rivest发表了SELECT，该算法在理论上和实践上都被证明平均非常高效。平均而言，从n个元素中选出秩为i的元素需要n + min(i, n − i) + o(n)次比较，两位作者还证明了一个与该表达式相差不超过9%的下界。<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup>SELECT通过使用采样来确定划分所依据的元素，改进了FIND；在寻找中位数的实验中，SELECT在n = 10,000时耗时877毫秒，而FIND耗时1,964毫秒。<sup>[12](https://doi.org/10.1145/360680.360694)</sup>Floyd和Rivest认为SELECT在期望情况意义下是渐近最优的，NIST的《算法与数据结构词典》将选择n个数中第i小元素的比较次数记录为n + min(i, n − i) + o(n)，即Θ(n)。<sup>[1](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)</sup><sup> • </sup><sup>[13](https://xlinux.nist.gov/dads/HTML/select.html)</sup>

## 混合与现代实现

**内省选择。** Musser的内省选择算法运行一种启发式引导的快速选择，它监控自身性能，仅在进展缓慢时切换到中位数之中位数，从而将快速选择的平均速度与线性最坏情况结合起来。<sup>[9](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)</sup>这是NumPy用于`partition`和`argpartition`的算法，其特点是平均速度指标为1、最坏情况O(n)、无额外工作空间。<sup>[6](https://numpy.org/doc/1.25/reference/generated/numpy.partition.html)</sup>C++的`std::nth_element`将本应占据排序范围第n个位置的元素放在该处，其前面的一切不大于它，后面的一切不小于它，平均时间为线性。<sup>[5](https://www.inf.pucrs.br/flash/progeng2/cppreference/w/cpp/algorithm/nth_element.html)</sup>

**更快的确定性选择。** D语言基金会的Andrei Alexandrescu提出了MedianOfNinthers，一种基于中位数之中位数精化定义的快速线性时间确定性选择算法，以及一个互补的算法MedianOfExtrema；这一对算法在保证线性时间内解决选择问题，无需随机化、启发式或回退方法，实测比最先进的基线更快。<sup>[9](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)</sup>

## 下界与精确小情形

中位数选择的最佳已知上界与下界之间的差距推动了数十年的研究。Dorit Dor和Uri Zwick表明中位数选择需要(2 + ε)n次比较，其中ε > 0；Bent和John在1985年证明了寻找中位数需要2n次比较。<sup>[14](https://epubs.siam.org/doi/10.1137/S0097539795288611)</sup><sup> • </sup><sup>[15](https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.SEA.2025.16)</sup>2025年，斯图加特大学的Dörrer、Gendle、Betz、von Smercek、Steding和Stober通过计算机搜索，找到了n不超过15时任意秩的最优选择算法，以及n = 16且i ≤ 6时的最优算法，确立了精确值V₇(14) = 25、V₆(15) = V₇(15) = 26和V₈(15) = 27，而此前只知道一个范围。<sup>[15](https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.SEA.2025.16)</sup>

## 相关度量与场景

**脆弱复杂度。** 2018年由奥胡斯大学的Peyman Afshani及其同事发起的一系列工作研究了基于比较的算法的脆弱复杂度，定义为任何单个元素参与的最大比较次数，并给出了确定性和随机化场景下最小值、选择、排序和堆构造的上界和下界。<sup>[16](https://pure.itu.dk/en/publications/fragile-complexity-of-comparison-based-algorithms/)</sup>

**并行选择。** [IBM Research](https://www.edgepedia.org/zh/ibm-research)报告称最优并行选择的复杂度为O(log log N)，完善了一幅图景：在随机和确定性模型中，所有比较任务——选择、归并和排序——都具有相同阶的上界和下界。<sup>[17](https://research.ibm.com/publications/optimal-parallel-selection-has-complexity-olog-log-n)</sup>

**流式与外存。** 当数据以流的形式到达或超出主存时，问题的形式发生变化。在并行磁盘系统上，Rajasekaran的递归采样选择在不对输入规模作假设的情况下，以不超过2 + ε遍解决精确外存选择，在实验中优于快速选择。<sup>[7](https://pmc.ncbi.nlm.nih.gov/articles/PMC4217089/)</sup>对于流上持续更新的中位数，标准结构维护一个最大堆用于下半部分，一个最小堆用于上半部分，保持平衡，使下部堆容纳的值的数量与上部堆相同或多一个。<sup>[18](https://algorithms.alexli.ca/online-median/)</sup>

## References

1. [Robert W. Floyd and Ronald L. Rivest (1975). Expected Time Bounds for Selection. Communications of the ACM 18(3):165-173.](https://people.csail.mit.edu/rivest/pubs/FR75a.pdf)
2. [Manuel Blum, Robert W. Floyd, Vaughan Pratt, Ronald L. Rivest, and Robert E. Tarjan (1973). Time Bounds for Selection. Journal of Computer and System Sciences 7(4):448-461.](https://www.sciencedirect.com/science/article/pii/S0022000073800339)
3. [C. A. R. Hoare (1961). Algorithm 65: Find. Communications of the ACM 4(7):321-322.](https://refs.drheap.org/hoare1961algorithm/)
4. [Cormen, Leiserson, Rivest, and Stein. Introduction to Algorithms, Third Edition, Chapter 9.](https://www.eng.biu.ac.il/~wimers/files/courses/Data_Structures_and_Algorithms_I/Materials/Introduction_to_algorithms-3rd%20Edition.pdf)
5. [std::nth_element, Cppreference.](https://www.inf.pucrs.br/flash/progeng2/cppreference/w/cpp/algorithm/nth_element.html)
6. [numpy.partition, NumPy v1.25 Manual.](https://numpy.org/doc/1.25/reference/generated/numpy.partition.html)
7. [Sanguthevar Rajasekaran. A Two-Pass Exact Algorithm for Selection on Parallel Disk Systems.](https://pmc.ncbi.nlm.nih.gov/articles/PMC4217089/)
8. [Yannis Manolopoulos. The Computer Journal: 65th Anniversary Perspectives on Hoare's Quicksort.](https://academic.oup.com/comjnl/pages/65th-anniversary-perspective-ym2)
9. [Andrei Alexandrescu (2017). Fast Deterministic Selection. SEA 2017, LIPIcs.](https://drops.dagstuhl.de/opus/volltexte/2017/7612/pdf/LIPIcs-SEA-2017-24.pdf)
10. [Time Bounds for Selection (BFPRT 1973), full text PDF.](https://www.cs.wmich.edu/gupta/teaching/cs6310/lectureNotes_cs6310/BFPRT73.pdf)
11. [MIT 6.046 Introduction to Algorithms, Lecture 6: Order Statistics (2003).](https://courses.csail.mit.edu/6.046/spring03/handouts/lec6.pdf)
12. [Robert W. Floyd and Ronald L. Rivest (1975). Algorithm 489: The Algorithm SELECT. Communications of the ACM 18(3):173.](https://doi.org/10.1145/360680.360694)
13. [Paul E. Black, "Select", Dictionary of Algorithms and Data Structures, NIST.](https://xlinux.nist.gov/dads/HTML/select.html)
14. [Dorit Dor and Uri Zwick. Selecting the Median. SIAM Journal on Computing 28(5):1722-1758.](https://epubs.siam.org/doi/10.1137/S0097539795288611)
15. [Josua Dörrer, Konrad Gendle, Johanna Betz, Julius von Smercek, Andreas Steding, and Florian Stober (2025). Exact Lower Bounds for the Number of Comparisons in Selection. SEA 2025, LIPIcs Volume 338.](https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.SEA.2025.16)
16. [Peyman Afshani and colleagues. Fragile Complexity of Comparison-Based Algorithms. IT University of Copenhagen.](https://pure.itu.dk/en/publications/fragile-complexity-of-comparison-based-algorithms/)
17. [Optimal parallel selection has complexity O(Log Log N), IBM Research.](https://research.ibm.com/publications/optimal-parallel-selection-has-complexity-olog-log-n)
18. [Online Median, Elementary Algorithms.](https://algorithms.alexli.ca/online-median/)

---
*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 › 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/selection-algorithm

English original: https://www.edgepedia.org/selection-algorithm
