Lecture 12
Even faster median finding
CSE 421 Autumn 2026
1
Previously…
2
“Selection” finding
Input: pair .
Output: The -th element when .
3
Median finding is just Selection finding for .
Can solve Selection finding in time by sorting, and then selecting the -th element.
Is achievable?
The recurrence relation we are shooting for:
Crucially, there is a single branch in this recursion!
Selection finding
Find the 6th element
4
The idea:
Inspired by the Quicksort “pivot” approach:
?
The good news: only one branch!
But is our instance size shrinking fast enough?
Depends on how lucky/unlucky we are with the pivot..
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Selection finding with pivot (examples)
Find the 6th element
5
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Selection finding with pivot (examples)
Find the 6th element
6
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
7
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
8
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
3
3
3
3
0
1
1
2
0
1
2
2
2
7
4
4
8
6
6
5
Where is the 6th element?
Randomly select a pivot
In , since
length 9
length 4
length 7
Recurse on this set
Selection finding with pivot (examples)
Find the 6th element
9
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Selection finding with pivot (examples)
Find the 6th element
10
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
11
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
12
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
length 2
length 3
length 15
Where is the 6th element?
In , since
Recurse on this set
Selection finding with pivot (examples)
Find the 6th element
13
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Selection finding with pivot (examples)
Find the 6th element
14
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
15
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
Selection finding with pivot (examples)
Find the 6th element
16
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
Randomly select a pivot
0
0
1
1
1
2
2
2
2
7
3
3
3
3
4
4
8
6
6
5
length 5
length 4
length 11
Where is the 6th element?
In , since and
In fact, in this case, the 6th element is exactly the pivot! So we just output it right away.
Selection finding
Randomized algorithm :
17
Note that the original -th element will now be the -th!
Runtime analysis
18
Runtime analysis
19
Runtime analysis
Expected runtime analysis:
20
Runtime analysis
Expected runtime analysis:
Total expected runtime: .
21
Today
22
Selection finding
23
.. but is it?
Blum-Floyd-Pratt-Rivest-Tarjan (1973): Selection/median finding can be done in deterministic time .
Pivot selection algorithm (BFPRT ‘73)
24
Pivot selection algorithm (BFPRT ‘73)
25
Pivot selection algorithm
26
Pivot selection algorithm
27
This should give you pause: this pivot algorithm is supposed to be used as a subroutine within an algorithm that computes the median! So how can we compute this median?
Recursion as usual!
Pivot selection algorithm
Proof of correctness
28
i.e. that the pivot is the -th element for some .
Pivot selection algorithm
Proof of correctness
29
i.e. that the pivot is the -th element for some .
Pivot selection algorithm
Proof of correctness
30
i.e. that the pivot is the -th element for some .
Pivot selection algorithm
Proof of correctness
31
i.e. that the pivot is the -th element for some .
Pivot selection algorithm
Proof of correctness
32
So, is in the middle elements and a good pivot!
With this pivot, the next instance�would be of size at most
i.e. that the pivot is the -th element for some .
Pivot selection algorithm
Runtime analysis
33
Not really a step, just an interpretation
per column;� total
The last runtime is in terms of (time to compute median on size )! This is a creative approach! But still unclear if this actually helps us..
Median/Selection finding algorithm (BFPRT ’73)
Selection:
34
Runtime:
this is recursive: will make more calls to Selection()
Median/Selection finding algorithm (BFPRT ’73)
Selection:
35
this is recursive: will make more calls to Selection()
Runtime:
Why?
Median/Selection finding algorithm (BFPRT ’73)
36
Runtime:
Why?
Let the term be bounded by for some constant . We’ll check directly by substitution that is a solution for a large enough constant .
So the runtime is at most .
(We are implicitly assuming that a recurrence relation has a unique solution - which is the case once you specify the initial condition, since subsequent runtimes are determined uniquely by the prior ones)
Note: to get a linear time, it’s crucial that the two fractions sum to less than 1!