# 中位数的中位数

**中位数的中位数**是一种用于选择问题的确定性枢轴选择算法：它选出的元素保证能将集合划分成两侧各至少包含3n/10个元素，这使得快速选择式的选择算法在最坏情况下也能以O(n)时间运行。该算法于1973年由[曼努埃尔·布卢姆](https://www.edgepedia.org/zh/manuel-blum)（Manuel Blum）、罗伯特·W·弗洛伊德（Robert W. Floyd）、沃恩·普拉特（Vaughan Pratt）、罗纳德·L·里维斯特（Ronald L. Rivest）和[罗伯特·E·塔扬](https://www.edgepedia.org/zh/robert-e-tarjan)（Robert E. Tarjan）以算法PICK之名发表于《计算机与系统科学杂志》，并常以其作者姓氏首字母组合BFPRT相称。<sup>[1](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup>

| 关键事实 | 详情 |
|---|---|
| 解决的问题 | 在最坏情况线性时间内选出n个元素中第i小的元素（顺序统计量，包括中位数）<sup>[1](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup> |
| 枢轴保证 | 至少有3n/10个元素不大于所选枢轴，且至少有3n/10个元素不小于所选枢轴，因此任何递归调用最多处理7n/10个元素<sup>[2](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)</sup> |
| 递归式 | T(n) = T(n/5) + T(7n/10) + O(n)；各分数之和为9/10 < 1，这正是解为线性的原因<sup>[3](https://naruhodotutoring.com/slides/text/cs3000/quicksort-selection-median-of-medians.html)</sup> |
| 分组大小 | 五是能导出经典线性时间证明的最小分组大小；三元素分组所得分数之和恰好为1，无法给出线性界<sup>[4](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)</sup><sup> • </sup><sup>[3](https://naruhodotutoring.com/slides/text/cs3000/quicksort-selection-median-of-medians.html)</sup> |
| 原始界 | 1973年的论文表明所需比较次数不超过5.4305n；更早的1971年报告给出的上界为5.73n<sup>[1](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup><sup> • </sup><sup>[5](https://people.csail.mit.edu/rivest/pubs/BFPRT72.pdf)</sup> |
| 实际地位 | 很少直接使用，因为它及其变体比基于采样的方法慢；它作为内省选择实现（如C++的`std::nth_element`）中的兜底方案而存续<sup>[6](https://arxiv.org/abs/1606.00484)</sup><sup> • </sup><sup>[4](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)</sup> |

## 算法如何工作

该算法通过五个步骤找出n个元素中第k小的元素。<sup>[2](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)</sup><sup> • </sup><sup>[7](https://www.ic.unicamp.br/~bit/guerreiro/Introduction%20to%20Algorithms%20-%20Cormen%20Leiserson%20Rivest%20Stein%20-%202009%20-%20InstructorsManual3rdEd.pdf)</sup>

1. 将元素分成每五个一组（剩余一组最多四个元素，在分析中可忽略）。
2. 用暴力法找出每组的中位数。由于五元素组大小固定，一个预先确定的比较网络可以用不超过6次比较找出其中位数。<sup>[4](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)</sup>
3. 对约n/5个组中位数构成的集合递归地应用同一选择算法，找出它们的中位数，即中位数的中位数。<sup>[2](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)</sup>
4. 以此枢轴对原始元素进行划分，将枢轴与每个元素比较。
5. 如同快速选择那样，递归进入包含第k个元素的那一侧；若枢轴本身的秩恰为k则停止。

递归之所以终止，是因为每次调用处理的集合都严格更小；当集合较小时（例如少于五个键），则改用排序等其他过程。<sup>[8](https://math.mit.edu/~djk/18.310/Lecture-Notes/median_finding_2007.html)</sup> 该过程实际上解决的是寻找秩为m的元素的更一般问题，而不仅仅是中位数，这正是递归调用所需要的。<sup>[8](https://math.mit.edu/~djk/18.310/Lecture-Notes/median_finding_2007.html)</sup>

## 枢轴为何优良

该方法的价值在于它所保证的划分。假设元素互不相同。约有一半的n/5个组中位数不大于中位数的中位数，而每个这样的中位数所在组中还有两个元素也不大于它。这就给出了至少3n/10个小于枢轴的元素；对称的论证给出至少3n/10个大于枢轴的元素。因此枢轴的秩满足3n/10 ≤ R(M, S) ≤ 7n/10，于是划分至少丢弃剩余元素的30%，递归调用最多处理其中7n/10个。<sup>[2](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)</sup>

该算法只需丢弃固定比例的元素，而不必找到精确的中位数，这正是该构造所实现的目标。<sup>[9](https://quidproquo.cc/posts/learning/2026-08-21-stanford-cs161-lecture-04-median-selection-en/)</sup>

## 递归式与为何用五元素分组

运行时间满足递归式

\[ T(n) = T(n/5) + T(7n/10) + O(n), \]

其中T(n/5)是递归搜索中位数的中位数，T(7n/10)是划分后的递归选择，线性项则涵盖分组、寻找组中位数和划分。<sup>[2](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)</sup> 代入论证可验证T(n)是线性的：假设对更小的输入有T(n′) ≤ an′，则得T(n) ≤ cn + an(1/5) + an(7/10) = cn + an(9/10) ≤ an，只要c < a/10。<sup>[10](https://www.cs.yale.edu/homes/aspnes/pinewiki/MedianFinding.html?highlight=%28CategoryAlgorithmNotes%29)</sup>

**分组大小是关键。** 采用五元素分组时，递归式中的两个分数为1/5和7/10，之和为9/10，严格小于一，因此总工作量逐层收缩。采用三元素分组时，分数为1/3和2/3，之和恰好为一，无法给出线性界。<sup>[3](https://naruhodotutoring.com/slides/text/cs3000/quicksort-selection-median-of-medians.html)</sup> 五是能奏效的最小分组大小，而非最大：七元素分组同样可行，得到T(n) ≤ T(n/7) + T(5n/7) + cn，其中1/7 + 5/7 = 6/7 < 1，代价是每组开销更大。<sup>[11](https://cs.shivi.io/01-Semesters-%28BSc%29/Semester-1/Algorithms-and-Datastructures/Lecture-Notes/15-Quick-Select-and-Finding-the-Median)</sup> 仅凭分组大小为奇数只能保证每组有唯一确定的中位数；它本身并不能使分数之和降到一以下。<sup>[3](https://naruhodotutoring.com/slides/text/cs3000/quicksort-selection-median-of-medians.html)</sup>

CLRS中的教科书论述更仔细地追踪了常数，给出n ≥ 140时的递归式T(⌈n/5⌉) + T(7n/10 + 6) + O(n)，并用代入法证明其线性。<sup>[7](https://www.ic.unicamp.br/~bit/guerreiro/Introduction%20to%20Algorithms%20-%20Cormen%20Leiserson%20Rivest%20Stein%20-%202009%20-%20InstructorsManual3rdEd.pdf)</sup> 对直接构造的比较次数分析估计其代价约为16n次比较；1973年的原始论文将其算法的常数调校得远低于教科书论述。<sup>[4](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)</sup>

## 历史

1971年，布卢姆、弗洛伊德、普拉特、里维斯特和塔扬的技术报告《中位数计算的线性时间界》确立了从n个数中选出第i大数的比较次数上界f(i, n) ≤ 5.73n，改进了哈迪安（Hadian）和索贝尔（Sobel）1969年的此前最佳界，并指出更精确地确定f(i, n)仍是一个未决问题。<sup>[5](https://people.csail.mit.edu/rivest/pubs/BFPRT72.pdf)</sup> 期刊版本《选择的时间界》（《计算机与系统科学杂志》，第7卷，第4期，第448–461页，1973年8月）分析了算法PICK，表明所需比较次数不超过5.4305n，改进了i取极端值时的界，并证明了一个新的下界。<sup>[1](https://www.sciencedirect.com/science/article/pii/S0022000073800339)</sup> 发表的算法使用15个元素的有序列，并包含合并步骤，复用先前迭代的部分排序结果以降低常数。<sup>[12](https://people.csail.mit.edu/rivest/pubs/BFPRT73.pdf)</sup>

## 实际应用

尽管具有理论重要性，中位数的中位数很少作为独立的选择算法使用，因为它及其变体比基于采样的简单方法慢。<sup>[6](https://arxiv.org/abs/1606.00484)</sup> 它的主要实际角色是作为安全网。大卫·穆瑟（David Musser）1997年提出的内省选择（introselect）——内省排序（introsort）的选择部分，也是常见工具链中C++ `std::nth_element`背后的引擎——以普通快速选择开始，只有当深层递归暴露出连续糟糕的枢轴选择时，才回退到中位数的中位数；由此将快速选择的典型情况效率与中位数的中位数的最坏情况界结合起来。<sup>[4](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)</sup> 一个高质量的`std::nth_element`实现可以使用快速选择，并在划分效果不佳时切换到中位数的中位数，因为C++标准规定的是平均情况线性复杂度。<sup>[13](https://stackoverflow.com/questions/11068429/nth-element-implementations-complexities)</sup>

**作为快速排序的枢轴。** 使用中位数的中位数作为枢轴选择可使快速排序的最坏情况复杂度为O(n ln n)，但由于其常数因子，该方法常被认为对此目的而言过于昂贵。<sup>[14](https://arxiv.org/abs/1608.04852)</sup> 黑泽则之（Noriyuki Kurosawa）表明，对输入进行抽稀，即仅对数组中1/s的元素应用BFPRT，即使s > 1也能保持O(n ln n)的最坏情况排序界，因为每次划分仍至少移除元素的0.3/s比例。在随机序列上的实验中，s = 40的抽稀BFPRT拟合出的比较系数为1.5371n ln n，优于三数取中（1.710），与伪九数取中（1.568）相当，在该配置下s约在20到50之间可能是最佳选择。<sup>[14](https://arxiv.org/abs/1608.04852)</sup>

后来的确定性选择算法直接建立在这一思想之上。以C++和D语言设计工作闻名的程序员安德烈·亚历山德雷斯库（Andrei Alexandrescu）于2016年提出了QuickselectAdaptive，基于对MedianOfMedians的精炼定义，将确定性选择——具有可复现的运行、可预测的运行时间以及对病态输入的免疫力——带入实用范围；测量表明，在独立同分布和正态分布输入上，它比最先进的基线更快。<sup>[6](https://arxiv.org/abs/1606.00484)</sup>

## References

1. [Blum, M., Floyd, R. W., Pratt, V., Rivest, R. L., Tarjan, R. E. (August 1973). "Time bounds for selection". Journal of Computer and System Sciences 7(4): 448–461.](https://www.sciencedirect.com/science/article/pii/S0022000073800339)
2. [Lecture notes, Algorithm Analysis and Design (CSL356), IIT Delhi, section 2.2.2 "Median of medians".](https://www.cse.iitd.ac.in/~sSen/csl356/root.pdf)
3. [Quicksort, Selection & Median of Medians, CS3000 Algorithms, Naruhodo Tutoring.](https://naruhodotutoring.com/slides/text/cs3000/quicksort-selection-median-of-medians.html)
4. [Worst-case linear selection (median of medians), distillcourse, Algorithms and Data Structures.](https://distillcourse.com/courses/algorithms-and-data-structures/lessons/median-of-medians)
5. [Blum, M., Floyd, R. W., Pratt, V., Rivest, R. L., Tarjan, R. E. (August 1971). "Linear Time Bounds for Median Computations". MIT technical report.](https://people.csail.mit.edu/rivest/pubs/BFPRT72.pdf)
6. [Alexandrescu, A. (2016). "Fast Deterministic Selection". arXiv:1606.00484.](https://arxiv.org/abs/1606.00484)
7. [Instructor's Manual, Introduction to Algorithms (CLRS, 2009), Chapter 9: Medians and Order Statistics.](https://www.ic.unicamp.br/~bit/guerreiro/Introduction%20to%20Algorithms%20-%20Cormen%20Leiserson%20Rivest%20Stein%20-%202009%20-%20InstructorsManual3rdEd.pdf)
8. [Finding the Median, MIT 18.310 lecture notes (2007).](https://math.mit.edu/~djk/18.310/Lecture-Notes/median_finding_2007.html)
9. [Stanford CS161 Lecture 4: How Median of Medians Guarantees Linear-Time Selection.](https://quidproquo.cc/posts/learning/2026-08-21-stanford-cs161-lecture-04-median-selection-en/)
10. [MedianFinding, Yale CS notes (James Aspnes).](https://www.cs.yale.edu/homes/aspnes/pinewiki/MedianFinding.html?highlight=%28CategoryAlgorithmNotes%29)
11. [Quick Select and Finding the Median, lecture notes.](https://cs.shivi.io/01-Semesters-%28BSc%29/Semester-1/Algorithms-and-Datastructures/Lecture-Notes/15-Quick-Select-and-Finding-the-Median)
12. [Blum, M., Floyd, R. W., Pratt, V., Rivest, R. L., Tarjan, R. E. "Time bounds for selection" (full paper PDF, S0022-0000(73)80033-9).](https://people.csail.mit.edu/rivest/pubs/BFPRT73.pdf)
13. [nth_element implementations complexities, Stack Overflow.](https://stackoverflow.com/questions/11068429/nth-element-implementations-complexities)
14. [Kurosawa, N. (2016). "Quicksort with median of medians is considered practical". arXiv:1608.04852.](https://arxiv.org/abs/1608.04852)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › 算法与计算方法 › 排序、搜索与选择 › 选择与次序统计量

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/median-of-medians

English original: https://www.edgepedia.org/median-of-medians
