Median of medians algorithm why divide the array into blocks of size 5
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The Median of Medians algorithm is an efficient selection algorithm by Blum, Floyd, Pratt, Rivest, and Tarjan that finds an approximate median of a list to serve as a good pivot for quickselect operations. This technique often provides a robust pivot to improve the worst-case performance of algorithms like quickselect and quicksort by ensuring linear time complexity.
Understanding the Median of Medians
The key idea is to partition the array to find the i-th smallest element more efficiently. The algorithm arranges the elements into groups, computes the median of each group, and then recursively computes the median of those medians until it reaches one element.
Why Divide the Array into Blocks of Size 5?
The choice of dividing the array into groups of 5 is critical for ensuring that each median calculated is a good approximation of the final median. Here’s an explanation to understand this choice:
- Balance Between Accuracy and Simplicity: • Dividing into smaller groups ensures the medians are close to the actual median, while not being so small that there are too many medians to sort recursively. • A block size of 3 leads to inefficient recursion since the individual medians may not accurately represent the overall median. Larger block sizes result in more complex calculations and larger recursive trees.
- Computational Efficiency: • Groups of 5 allow for a balance between the overhead of computing the medians and maintaining the linear time complexity. Calculating the median of a small group can be done in constant time, specifically, sorting 5 elements requires at most 7 comparisons.
- Mathematical Assurance: • The median of medians can be shown to be within a constant factor of the true median, ensuring a tighter bound on recursive depth and efficient partitioning. • By grouping elements into 5, approximately of the elements are guaranteed to be greater than or less than the median of medians, providing a better partition.
Algorithm Steps
- Divide the Array: • Divide the array into groups of 5 (the last group may contain fewer elements).
- Find Medians of Each Group: • Sort each group and find their medians.
- Select Median of Medians: • Recursively select the median of these medians to use as a pivot.
- Partition and Recur: • Use this pivot to partition the array into elements less than the pivot, equal to the pivot, and greater than the pivot. • Recur into the part of the array that contains the i-th smallest element.
- Termination Condition: • If the array is small enough (typically 1 or 2 elements), return the result of direct comparison.
Example
Let's take an example to illustrate the process quickly:
Suppose you want to find the median of the list [10, 30, 20, 40, 50, 60, 70, 80, 90, 100]
:
• Split into groups: ([10, 30, 20, 40, 50], [60, 70, 80, 90, 100])
• Sort each group: ([10, 20, 30, 40, 50], [60, 70, 80, 90, 100])
• Find medians: [30, 80]
• Since we only have two medians, sort them and choose the median: 80
Use 80 as the pivot and partition accordingly. Continue recursively.
Comparative Analysis
| Block Size | Pros | Cons |
| 3 | Simplifies recursion More intuitive partition | Less accurate medians Inefficient recursion depth |
| 5 | Better balance between accuracy and complexity Contributes to worst-case linear time complexity | Slightly higher constant factors in recursive calls |
| Larger (>5) | More accurate medians | Increased overhead Complicates selection logic |
Conclusion
The Median of Medians algorithm's careful selection of block size ensures that it combines the benefits of efficient partitioning with assured linear performance. By maintaining a balance through groups of 5, this algorithm tackles the pivot selection problem with precision, enabling complex problems to be solved effectively.
This approach's mathematical foundation makes it a crucial part of understanding advanced data structures and algorithms, helping solve the selection problem with optimal efficiency.

