{
  "id": "median-of-medians",
  "title": "Median of medians",
  "lang": "en",
  "source_lang": "en",
  "summary": "Median of medians is a deterministic pivot-selection method that enables selection of an element of any given rank in worst-case linear time.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Median-of-medians algorithm",
    "BFPRT algorithm",
    "Deterministic linear-time selection"
  ],
  "generated_at": "2026-11-06",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/median-of-medians/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/median-of-medians/",
      "json": "https://aiwiki.page/zh/median-of-medians.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Median of medians is a deterministic method for choosing a pivot in a selection algorithm, allowing the element of any specified rank to be found in worst-case linear time. Its standard form divides the input into groups of five, finds each group's median, and recursively selects the exact median of those medians. This pivot need not be the median of the original input, but it guarantees that a fixed fraction of the elements can be discarded at each selection step. The method is commonly associated with the BFPRT algorithm, named after Manuel Blum, Robert W. Floyd, Vaughan Pratt, Ronald L. Rivest, and Robert E. Tarjan."
    },
    {
      "heading": "The selection problem",
      "text": "Given \\(n\\) elements with a consistent ordering and an integer \\(k\\), where \\(1 \\leq k \\leq n\\), the selection problem asks for the \\(k\\)-th smallest element. This value is an order statistic. Repeated values occupy separate ranks: in the sequence \\(2,2,5,9\\), the second-smallest element is \\(2\\).\n\nSelection includes finding a minimum, a maximum, or a median. For an even number of numerical values, a statistical median is often defined as the arithmetic mean of the two central order statistics; a selection algorithm returns either central element directly, and both can be obtained if their mean is required.\n\nOne approach is to sort the entire input and then retrieve the requested position. A comparison-based sorting algorithm generally takes \\(O(n\\log n)\\) time, whereas selection does not require a complete ordering. Median of medians achieves worst-case \\(\\Theta(n)\\) time complexity under the usual assumption that comparisons and element operations take constant time."
    },
    {
      "heading": "Algorithm",
      "text": "Median of medians supplies a pivot for a partition-based selection procedure closely related to Quickselect. The procedure uses recursion in two places: to select a pivot from the group medians, and to continue searching the relevant partition of the original input.\n\nFor a nonempty input sequence \\(A\\) and a valid one-based rank \\(k\\):\n\n- Handle a small input directly. Sort it and return the element at rank \\(k\\).\n- Form groups. Divide the input into groups of five, with at most one smaller group.\n- Find the group medians. Sort each group and extract its middle element. For an even-sized final group, either central element can be used consistently.\n- Select the pivot. Recursively select the exact median of the sequence of group medians.\n- Partition the input. Separate the elements into those less than, equal to, and greater than the pivot.\n- Continue only where necessary. Return the pivot if the requested rank falls in its equal partition; otherwise recurse on the appropriate strict partition with an adjusted rank.\n\nThe group size is fixed, so sorting every group costs only \\(O(n)\\) time in total. A simple method such as insertion sort is sufficient for these tiny groups.\n\nPseudocode\n\nThe following pseudocode uses one-based ranks and chooses the lower median when a sequence has even length:\n\nSELECT(A, k):\n    require 1 <= k <= length(A)\n\n    if length(A) <= 5:\n        sort A\n        return A[k]\n\n    M = empty sequence\n\n    for each consecutive group G of at most 5 elements in A:\n        sort G\n        append G[ceil(length(G) / 2)] to M\n\n    p = SELECT(M, ceil(length(M) / 2))\n\n    L = elements of A strictly less than p\n    E = elements of A equal to p\n    R = elements of A strictly greater than p\n\n    if k <= length(L):\n        return SELECT(L, k)\n\n    if k <= length(L) + length(E):\n        return p\n\n    return SELECT(R, k - length(L) - length(E))\n\nThree-way partitioning is important for repeated values. If all input elements equal the pivot, the algorithm terminates immediately after partitioning. An implementation that sends equal elements into a recursive branch without controlling its size can lose the intended worst-case guarantee."
    },
    {
      "heading": "Why the pivot is sufficiently balanced",
      "text": "Consider the full groups of five. Each group median has at least three elements in its group that are no greater than it, and at least three that are no less than it, counting the median itself.\n\nBecause the pivot \\(p\\) is the median of the group medians:\n\n- roughly half the group medians are no greater than \\(p\\);\n- roughly half are no less than \\(p\\).\n\nEach qualifying full group therefore contributes three elements to a corresponding bound. Accounting for rounding and the possible incomplete group, at least\n\n\\[\\frac{3n}{10}-O(1)\\]\n\ninput elements are no greater than \\(p\\), and at least the same number are no less than \\(p\\).\n\nConsequently, each strict partition contains at most\n\n\\[\\frac{7n}{10}+O(1)\\]\n\nelements. Once the input has been partitioned, selection can discard approximately 30 percent of it even in the least favorable case.\n\nFor distinct elements, this is often described as choosing a pivot between approximately the 30th and 70th percentiles. With duplicates, the non-strict counting argument and the sizes of the strict partitions are the more precise statements.\n\nAn illustrative case\n\nSuppose there are \\(25\\) distinct elements, arranged into five groups of five. Sort the five group medians conceptually as\n\n\\[m_1 < m_2 < m_3 < m_4 < m_5.\\]\n\nThe chosen pivot is \\(p=m_3\\).\n\nThe three groups represented by \\(m_1,m_2,m_3\\) each contain at least three elements no greater than \\(p\\), giving at least nine such elements. Similarly, the groups represented by \\(m_3,m_4,m_5\\) give at least nine elements no less than \\(p\\). Neither strict partition can therefore contain more than \\(16\\) elements.\n\nThe example illustrates why the pivot is useful without requiring it to be the exact median of all \\(25\\) elements."
    },
    {
      "heading": "Worst-case complexity",
      "text": "Let \\(T(n)\\) denote the worst-case running time. Selecting the median of the group medians processes approximately \\(n/5\\) elements. The subsequent selection call processes at most approximately \\(7n/10\\) elements. Grouping, finding constant-sized group medians, and partitioning take linear time, giving\n\n\\[T(n) \\leq T\\!\\left(\\left\\lceil\\frac{n}{5}\\right\\rceil\\right) + T\\!\\left(\\frac{7n}{10}+O(1)\\right) + O(n).\\]\n\nThe decisive relationship is\n\n\\[\\frac{1}{5}+\\frac{7}{10}=\\frac{9}{10}<1.\\]\n\nIgnoring bounded rounding effects, the total size of the recursive subproblems contracts by a fixed factor. The resulting work is bounded by a geometric series:\n\n\\[n+\\frac{9n}{10} +\\left(\\frac{9}{10}\\right)^2n+\\cdots =O(n).\\]\n\nWith sufficiently large base cases, the rounding terms do not change this bound. Partitioning itself examines the input, so the standard implementation takes \\(\\Theta(n)\\) worst-case time. This matches the asymptotic worst-case lower bound for general comparison-based selection.\n\nWhy groups of five?\n\nFive is a small odd group size for which the straightforward analysis yields a strict contraction.\n\nWith groups of three, the analogous basic estimate is\n\n\\[T(n)\\leq T(n/3)+T(2n/3)+O(n),\\]\n\napart from rounding. The recursive fractions sum to one, so this recurrence gives an \\(O(n\\log n)\\) upper bound rather than establishing linear time. This does not show that all algorithms using groups of three require superlinear time: modified grouping strategies can obtain stronger guarantees.\n\nLarger fixed odd groups, such as seven or nine, also permit linear-time selection through the same general argument. Their practical costs depend on how group medians are computed and on implementation details."
    },
    {
      "heading": "Historical significance",
      "text": "Blum, Floyd, Pratt, Rivest, and Tarjan published their selection result in the 1973 paper Time Bounds for Selection. Their algorithm, called PICK, established deterministic worst-case linear-time selection.\n\nThe result was important in computational complexity because it showed that finding a median does not require sorting the input. Selection and sorting have different information requirements: a selected order statistic can be identified without determining the relative order of every pair of elements.\n\nModern presentations commonly use the simpler groups-of-five formulation to explain the BFPRT result. The original paper included a more detailed analysis of comparison counts; the standard textbook procedure should not be assumed to reproduce all of its constants."
    },
    {
      "heading": "Relationship to other algorithms",
      "text": "Quickselect\n\nQuickselect partitions around a pivot and continues in only one partition. With a uniformly randomized pivot, it has expected linear running time, but its worst-case running time is quadratic.\n\nMedian of medians replaces this probabilistic balance with a deterministic size guarantee. It needs neither random sampling nor an assumption about the input's initial arrangement. The trade-off is additional work to construct a guaranteed pivot. In practice, a randomized algorithm or a simple pivot-selection heuristic may be faster on ordinary inputs.\n\nMedian-of-three pivot selection\n\nMedian of medians is not the same as median-of-three, a common pivot heuristic in Quicksort. Median-of-three selects the median of three sampled elements, often the first, middle, and last elements of a range. Those samples alone do not guarantee that a fixed fraction of the entire range lies on either side.\n\nLikewise, repeatedly taking medians of small groups until only one representative remains is not automatically the standard median-of-medians algorithm. The essential step in the standard method is to select the exact median of the complete sequence of group medians.\n\nIntroselect and hybrid methods\n\nIntroselect combines a fast selection procedure with a fallback intended to prevent pathological behavior. Median of medians can serve as a linear-time fallback.\n\nThe fallback alone does not establish a linear bound for the whole hybrid. The trigger must also limit work performed before switching: allowing many nearly full-size partitioning passes can accumulate superlinear work before the fallback begins."
    },
    {
      "heading": "Implementation and applications",
      "text": "The pseudocode above allocates separate sequences for the group medians and partitions, giving a straightforward implementation with \\(O(n)\\) auxiliary space complexity. Implementations based on an array can rearrange elements in place, move group medians into a prefix, and partition without full-sized auxiliary arrays. A straightforward recursive in-place implementation uses \\(O(\\log n)\\) call-stack space.\n\nMedian of medians is useful when worst-case selection guarantees matter, including exact quantile computation, partitioning around a specified rank, and algorithms in computational geometry that require balanced splits. It also provides a standard example of divide-and-conquer analysis with unequal recursive subproblem sizes.\n\nIts principal practical limitation is overhead: repeated group processing and recursive pivot selection can cost more than simpler pivot strategies. Its linear-time bound also concerns the number of elementary operations under a specified computation model. Expensive comparisons, large records, and memory-access costs can materially affect actual running time without changing the asymptotic comparison-based guarantee."
    }
  ],
  "links": [
    "median",
    "sorting-algorithm",
    "time-complexity",
    "quickselect",
    "recursion",
    "insertion-sort",
    "pseudocode",
    "computational-complexity",
    "randomized-algorithm",
    "quicksort",
    "space-complexity",
    "array-data-structure",
    "call-stack",
    "computational-geometry",
    "divide-and-conquer"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}