Algorithms · Visualized

Order: Median of Medians (BFPRT)

Order Statistics

Find the k-th smallest, medians, and top-k without full sorting.

A more advanced selection algorithm that guarantees O(n) worst-case time for finding the k-th smallest element (unlike Quickselect's O(n²) worst case). Time: O(n) average, O(n) — guaranteed, unlike plain Quickselect worst; space: O(log n) — recursion stack.

Big-O: Quickselect O(n) average; heaps O(n log k).

Median of Medians (BFPRT)

Order Statistics

A more advanced selection algorithm that guarantees O(n) worst-case time for finding the k-th smallest element (unlike Quickselect's O(n²) worst case). It picks a smarter pivot by dividing the array into small groups, finding the median of each group, then recursively finding the median of those medians — this 'median of medians' pivot is provably good enough to guarantee balanced partitions every time.

How Median of Medians (BFPRT) Works

  1. Divide the array into small groups (commonly of 5 elements each).
  2. Find the median of each small group (trivial for a group of 5 — just sort it).
  3. Recursively apply the same median-of-medians process to this list of group medians, to find the 'median of medians' — this becomes the pivot.
  4. Partition the original array around this pivot, just like in Quickselect.
  5. Recurse into only the side of the partition that contains the target rank k, exactly as in Quickselect, but now with the guarantee that the pivot always yields a well-balanced partition.

Median of Medians (BFPRT) Worked Example

Input: {array: [3, 7, 1, 9, 2, 8, 5, 6, 4, 10, 11, 15, 13, 14, 12], k: 8}

Split into groups of 5: [3,7,1,9,2], [8,5,6,4,10], [11,15,13,14,12]. Medians of each group: 3, 6, 13. Median of these medians: 6 — this becomes the pivot. Partition the full array around 6, then recurse into whichever side contains the 8th smallest element, repeating the same process on that smaller sub-array.

Order: Median of Medians (BFPRT)O(n)
Deterministic linear-time selection via group medians.
3
0
7
1
1
2
9
3
2
4
8
5
5
6
6
7
4
8
10
9
11
10
15
11
13
12
14
13
12
14
target8
Step 1 / 28
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Median of Medians (BFPRT): guarantee a good pivot for worst-case O(n) selection of the 8-th smallest.
Python · Order Statistics Algorithms in Code
def median_of_medians(arr, k):
    # deterministic quickselect: kth smallest, 0-indexed
    if len(arr) <= 5:
        return sorted(arr)[k]
    medians = [sorted(arr[i:i + 5])[len(arr[i:i + 5]) // 2]
               for i in range(0, len(arr), 5)]
    pivot = median_of_medians(medians, len(medians) // 2)
    less = [v for v in arr if v < pivot]
    equal = [v for v in arr if v == pivot]
    greater = [v for v in arr if v > pivot]
    if k < len(less):
        return median_of_medians(less, k)
    if k < len(less) + len(equal):
        return pivot
    return median_of_medians(greater, k - len(less) - len(equal))

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Median of Medians (BFPRT) Time & Space Complexity

BestO(n)
AverageO(n)
WorstO(n) — guaranteed, unlike plain Quickselect
SpaceO(log n) — recursion stack

When to Use Median of Medians (BFPRT)

  • Situations requiring a guaranteed worst-case linear time selection, such as real-time systems where occasional O(n²) spikes from plain Quickselect would be unacceptable.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.