Understanding median of medians algorithm
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of computer science and algorithms, the "median of medians" algorithm stands out as a deterministic approach to the selection problem, particularly useful for finding the -th smallest element in an unsorted array in linear time. This algorithm was introduced by Blum, Floyd, Pratt, Rivest, and Tarjan in 1973, and it fundamentally enhances the efficiency of selecting order statistics in practice. Here's a detailed dive into this ingenious algorithm.
1. Problem Definition
Selection Problem: Given an unsorted array, find the -th smallest element.
Traditional Approaches
- Sorting: Sort the array and pick the -th element. While effective, this runs in time.
- Quickselect: A more efficient partition-based approach using the median pivot. It generally works in expected time but can degrade to in the worst case.
2. Median of Medians Algorithm
The median of medians algorithm improves upon Quickselect by guaranteeing linear worst-case performance.
Algorithm Steps
- Divide the array into groups of at most five elements each.
- Find the median of each group by sorting and selecting the middle element.
- Recursively determine the median of these medians, which will serve as the pivot.
- Partition the original array around this pivot.
- Recursively select the -th smallest element in the appropriate partition.
Detailed Explanation
- The grouping into sets of `five` is crucial; it ensures that the median of medians can be found with a tight bound on subproblem sizes. Groups of five are optimal for the method due to the even distribution properties that arise during the median selection processes.
- To select the median of each group, sorting each group of five takes time due to the constant size.
Partitioning
The partitioning step divides the original array into three sections using the median of medians as pivot: elements less than the pivot, the pivot itself, and elements greater than the pivot. If the pivot is the -th smallest, the process stops. Otherwise, it recursively continues in the relevant section.
Pseudocode
- Grouping the elements: , sorting each group takes constant time.
- Median of medians selection: recursive call on reduced elements.
- Partitioning: .
- Robust Statistics: Calculating robust measures like the median which are less affected by outliers.
- Streaming Algorithms: Real-time analysis where sorting is computationally expensive.
- Computational Geometry: Problems like minimum enclosing ball, where linear-time selection is critical.

