Median of Medians (Deterministic Select)
Deterministic linear-time selection via median-of-5 pivoting.
Visualization
Interactive visualization for Median of Medians (Deterministic Select)
Interactive visualization with step-by-step execution
Implementation
1function medianOfMedians(arr: number[], k: number): number {
2 const select = (a: number[], k: number): number => {
3 if (a.length <= 5) return a.sort((x,y)=>x-y)[k];
4 const groups: number[][] = [];
5 for (let i = 0; i < a.length; i += 5) groups.push(a.slice(i, i+5));
6 const medians = groups.map(g => g.sort((x,y)=>x-y)[Math.floor(g.length/2)]);
7 const pivot = select(medians, Math.floor(medians.length/2));
8 const lows = a.filter(x => x < pivot);
9 const highs = a.filter(x => x > pivot);
10 const pivots = a.filter(x => x === pivot);
11 if (k < lows.length) return select(lows, k);
12 if (k < lows.length + pivots.length) return pivot;
13 return select(highs, k - lows.length - pivots.length);
14 };
15 return select([...arr], k);
16}Deep Dive
Theoretical Foundation
Blum-Floyd-Pratt-Rivest-Tarjan's median-of-medians algorithm chooses a pivot deterministically by grouping elements (usually in 5s), selecting medians of groups, and recursing to find the median of medians, guaranteeing linear-time selection.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Real-time systems requiring predictable latency
Robust pivot selection for quicksort
Deterministic selection routines
Use Cases
Related Algorithms
Binary Search
Binary Search is one of the most efficient searching algorithms with O(log n) time complexity. It works on sorted arrays by repeatedly dividing the search space in half, eliminating half of the remaining elements with each comparison. This divide-and-conquer approach makes it exponentially faster than linear search for large datasets.
Linear Search
Linear Search, also known as Sequential Search, is the simplest searching algorithm that checks each element in a list sequentially until the target element is found or the end is reached. Despite its O(n) time complexity, it's the only option for unsorted data and remains practical for small datasets or when simplicity is crucial.
Jump Search
Jump Search is an efficient algorithm for sorted arrays that combines the benefits of linear and binary search. Instead of checking every element (linear) or dividing the array (binary), it jumps ahead by fixed steps of √n and then performs linear search within the identified block. This approach achieves O(√n) time complexity, making it faster than linear search while being simpler than binary search for certain applications.
Interpolation Search
Interpolation Search is an improved variant of binary search specifically optimized for uniformly distributed sorted arrays. Instead of always checking the middle element, it estimates the target's position based on the target value relative to the range of values, similar to how humans search a phone book. Achieves O(log log n) average time for uniformly distributed data, significantly faster than binary search's O(log n).