Академический Документы
Профессиональный Документы
Культура Документы
Quick-Select Partition
Quick-select is a randomized We partition an input Algorithm partition(S, p)
selection algorithm based on sequence as in the quick-sort Input sequence S, position p of pivot
x algorithm: Output subsequences L, E, G of the
the prune-and-search elements of S less than, equal to,
We remove, in turn, each
paradigm: element y from S and
or greater than the pivot, resp.
L, E, G ← empty sequences
Prune: pick a random element x We insert y into L, E or G,
x ← S.remove(p)
(called pivot) and partition S into depending on the result of
while ¬S.isEmpty()
L elements less than x
x the comparison with the
pivot x y ← S.remove(S.first())
E elements equal x if y < x
G elements greater than x Each insertion and removal is
L E G L.insertLast(y)
at the beginning or at the
Search: depending on k, either k < |L| k > |L|+|E| else if y = x
end of a sequence, and
answer is in E, or we need to k’ = k - |L| - |E| E.insertLast(y)
hence takes O(1) time
recur on either L or G else { y > x }
|L| < k < |L|+|E| Thus, the partition step of G.insertLast(y)
quick-select takes O(n) time return L, E, G
(done)
Selection 3 Selection 4
1
Quick-Sort 10/27/2006 6:56 AM
Selection 7 Selection 8