median of medians
algorithm
data structures
selection algorithm
computational analysis

Understanding median of medians algorithm

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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 kk-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 kk-th smallest element.

Traditional Approaches

  • Sorting: Sort the array and pick the kk-th element. While effective, this runs in O(nlogn)O(n \log n) time.
  • Quickselect: A more efficient partition-based approach using the median pivot. It generally works in O(n)O(n) expected time but can degrade to O(n2)O(n^2) 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

  1. Divide the array into groups of at most five elements each.
  2. Find the median of each group by sorting and selecting the middle element.
  3. Recursively determine the median of these medians, which will serve as the pivot.
  4. Partition the original array around this pivot.
  5. Recursively select the kk-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 O(1)O(1) 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 kk-th smallest, the process stops. Otherwise, it recursively continues in the relevant section.

Pseudocode

  • Grouping the elements: O(n)O(n), sorting each group takes constant time.
  • Median of medians selection: O(n)O(n) recursive call on reduced elements.
  • Partitioning: O(n)O(n).
  • 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.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.