Linear Time Median Finding
Simplest Median
The most straightforward way to find the median is to sort the list and just pick the median by its index. The fastest comparison-based sort is O(nlogn)
, so that dominates the runtime
1 |
|
Although this algorithm is the fastest to implement, its efficiency is limited by the sorting step.
Finding Median in average of O(n)
This quickselect algorithm runs recursively in finding any number of order k in a given list.
Steps
- Pick an index in the list. It doesn’t matter how you pick it, but choosing one at random works well in practice. The element at this index is called the pivot.
- Split the list into 2 groups:
- Elements less than or equal to the pivot,
lesser_elems
- Elements strictly greater than the pivot,
greater_elems
- Elements less than or equal to the pivot,
- We know that one of these groups contains the median. Suppose we’re looking for the kth element:
- If there are k or more elements in
lesser_elems
, recurse on listlesser_elems
, searching for the kth element. - If there are fewer than k elements in
lesser_elems
, recurse on listgreater_elems
. Instead of searching for k, we search fork-len(lesser_elems)
.
- If there are k or more elements in
Quickselect for Median
Therefore, once we know the length of the given array, we can use quickselect to find the number in the middle.
1 |
|