Linear Time Median
::
2025-01-09Using the Divide and Conquer approach to find the median of an unsorted list in linear time
Summary
We can find the median of a list of numbers without sorting by combining ideas of QuickSort and Sample Sort.
Contents
Problem
Given an unsorted list of n
numbers, find the median
of .
median
th smallest element
When n is odd:
More generally,
Given an unsorted list of n numbers and an integer k where , find the kth smallest of A.
Basic Approach
QuickSort style
- Choose a pivot
p
- Partition A into
- Recursively sort
In order to make our median selecting algorithm instead of , we only need to recursively search one of the sub lists, not both.
Psuedocode
QuickSelect(A,k):
- Choose a pivot
p
- Partition A into
- Recurse
- If then return QuickSelect()
- If then return p
- If then return QuicSelect()
Reverse Engineering
good pivot
If we randomly select a pivot, there’s a 50% chance its good. We can instead
choose a representative sample
to choose the pivot from to gaurantee it’s
good.
Representative Sample
We want to choose a subset S, then find a median of S, and have that median be an approximation of the median of A.
for each , we want a few (2) elements of A are and a few (2) elements that are
Break A into groups of 5 elements each. Then sort each group and find the median of the middle group.
FastSelect
- Break A into groups,
- For i = 1 ->
- Sort and let
- let
- p = FastSelect()
- Partition A into
- Recurse
- If then return FastSelect()
- If then return p
- If then return FastSelect()