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 StatisticsA 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
- Divide the array into small groups (commonly of 5 elements each).
- Find the median of each small group (trivial for a group of 5 — just sort it).
- Recursively apply the same median-of-medians process to this list of group medians, to find the 'median of medians' — this becomes the pivot.
- Partition the original array around this pivot, just like in Quickselect.
- 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.
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
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
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.