Linear-‐Time
Selec.on
Determinis.c
Selec.on
(Algorithm)
Design
and
Analysis
of
Algorithms
I
The
Problem
For
simplicity
Input
:
array
A
with
n
dis.nct
numbers
and
a
number
Output
:
ith
order
sta.s.c
(i.e.,
ith
smallest
element
of
input
array)
Example
:
median.
3rd
order
sta.s.c
(
i
=
(n+1)/2
for
n
odd,
i
=
n/2
for
n
even
)
Tim
Roughgarden
Randomized
Selec.on
Rselect
(array
A,
length
n,
order
sta.s.c
i)
0)
if
n
=
1
return
A[1]
1) Choose
pivot
p
from
A
uniformly
1st
part
2nd
part
at
random
jth
posi.on
2) Par..on
A
around
p
let
j
=
new
index
of
p
3) If
j
=
i,
return
p
4) If
j
>
i,
return
Rselect(1st
part
of
A,
j-‐1,
i)
5) [if
j<i]
return
Rselect
(2nd
part
of
A,
n-‐j,
i-‐j)
Tim
Roughgarden
Guaranteeing
a
Good
Pivot
Recall
:
“best”
pivot
=
the
median
!
(seems
circular!)
Goal
:
find
pivot
guaranteed
to
be
pre_y
good.
Key
Idea
:
use
“median
of
medians”!
Tim
Roughgarden
A
Determinis.c
ChoosePivot
ChoosePivot(A,n)
-‐-‐
logically
break
A
into
n/5
groups
of
size
5
each
-‐-‐
sort
each
group
(e.g.,
using
Merge
Sort)
-‐-‐
copy
n/5
medians
(i.e.,
middle
element
of
each
sorted
group)
into
new
array
C
-‐-‐
recursively
compute
median
of
C
(!)
-‐-‐
return
this
as
pivot
Tim
Roughgarden
The
DSelect
Algorithm
o o s ePi vot
Ch
DSelect(array
A,
length
n,
order
sta.s.c
i)
1. Break
A
into
groups
of
5,
sort
each
group
2. C
=
the
n/5
“middle
elements”
3. p
=
DSelect(C,n/5,n/10)
[recursively
computes
median
of
C]
4. Par..on
A
around
p
Same
as
5. If
j
=
i
return
p
before
6. If
j
<
i
return
DSelect(1st
part
of
A,
j-‐1,
i)
7. [else
if
j
>
i]
return
DSelect(2nd
part
of
A,
n-‐j,
i-‐j)
Tim
Roughgarden
Tem
ver
How
many
recursive
calls
does
DSelect
make?
Running
Time
of
DSelect
Dselect
Theorem
:
for
every
input
array
of
length
n,
Dselect
runs
in
O(n)
.me.
Warning
:
not
as
good
as
Rselect
in
prac.ce
1) Worse
constraints
2)
not-‐in-‐place
History
:
from
1973
Blum
–
Floyd
–
Pra_
–
Rivest
–
Tarjan
(‘95)
(‘78)
(‘02)
(‘86)
Tim
Roughgarden