next up previous
Next: Up: Previous:

Selection in Expected Linear Time



Goal: Select ith smallest element from A[p..r].
Partition into A[p..q] and A[q+1..r]
If ith smallest element is in A[p..q]
then recurse on A[p..q]
else recurse on A[q+1..r]



Select(A, p, r, i)
1
$\;\;\;\;\;$if p = r
2
$\;\;\;\;\;$then return A[p]
3
$\;\;\;\;\;$else q = Partition(A, p, r)
4
$\;\;\;\;\;$ $\;\;\;\;\;$k = q - p + 1
5
$\;\;\;\;\;$ $\;\;\;\;\;$if i $\leq$ k
6
$\;\;\;\;\;$ $\;\;\;\;\;$then return Select(A, p, q, i)
7
$\;\;\;\;\;$ $\;\;\;\;\;$else return Select(A, q+1, r, i-k)


next up previous
Next: Up: Previous: