Lecture 10ppt
Lecture 10ppt
Algorithms – p. 165
Medians and Selection
• We will use a variation of the divide and
conquer strategy called the sieve technique.
• In divide and conquer, we break the problem
into a small number of smaller subproblems
which we solve recursively.
Algorithms – p. 166
Medians and Selection
• In the selection problem, we are looking for
an item.
• We will divide the problem into subproblems.
• However, we will discard those small
subproblems for which we determine that
they do not contain the desired answer (the
variation).
Algorithms – p. 167
Medians and Selection
Algorithms – p. 168
Medians and Selection
Algorithms – p. 169
Partitioning
pivot
p r
5 92 64 1 3 7 Before partitioning
p q r
3 12 4 69 5 7 After partitioning
<x x >x
Algorithms – p. 170
Medians and Selection
• The rank of the pivot x is q − p + 1 in A[p..r].
• Let rank_x = q − p + 1.
• If k = rank_x then the pivot is kth smallest.
• If k < rank_x then search A[p..q − 1]
recursively.
• If k > rank_x then search A[q + 1..r]
recursively. Find element of rank (k − q)
because we eliminated q smaller elements in
A.
Algorithms – p. 171
Select Algorithm
SELECT(array A, int p, int r, int k)
1 if (p = r)
2 then return A[p]
3 else x ← CHOOSE PIVOT(A, p, r)
4 q ← PARTITION(A, p, r, x)
5 rank_x ← q − p + 1
6 if k = rank_x
7 then return x
8 if k < rank_x
9 then return SELECT(A, p, q − 1, k)
10 else return SELECT(A, q + 1, r, k − q)
Algorithms – p. 172
Select Algorithm
Example: select the 6th smallest element of the set {5, 9, 2,
6, 4, 1, 3, 7}
rankx= 4
5 3
9 1
2 2
rankx= 3 rankx= 2
6 4
pivot 4 6 6 6 pivot 6 5
1 9 9 5 5 6
3 5 5 7
7 7 pivot 7 9
initial partition recurse partition recurse partition
k=6 pivot=4 k=(6-4)=2 pivot=7 k=2 pivot=6
DONE!!
Algorithms – p. 173