Вы находитесь на странице: 1из 2

Quick-Sort 10/27/2006 6:56 AM

The Selection Problem


Given an integer k and n elements x1, x2, …, xn,
Selection taken from a total order, find the k-th smallest
element in this set.
Of course, we can sort the set in O(n log n) time
and then index the k-th element.
k=3 7 4 9 6 2 → 2 4 6 7 9

Can we solve the selection problem faster?


Ideas?
Selection 1 Selection 2

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

Quick-Select Visualization Expected Running Time


An execution of quick-select can be visualized by a Consider a recursive call of quick-select on a sequence of size s
recursion path „ Good call: the sizes of L and G are each less than 3s/4
„ Bad call: one of L and G has size greater than 3s/4
„ Each node represents a recursive call of quick-select, and
stores k and the remaining sequence, e.g. “select 5th”: 7 2 9 43 7 6 19 7 2 9 43 7 6 1

k=5, S=(7 4 9 3 2 6 5 1 8) 2 4 3 1 7 9 7 1 → 1 1 7294376

Good call Bad call


k=2, S=(7 4 9 6 5 8)
A call is good with probability 1/2
k=2, S=(7 4 6 5) „ 1/2 of the possible pivots cause good calls:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
k=1, S=(7 6 5)
Bad pivots Good pivots Bad pivots
5
Selection 5 Selection 6

1
Quick-Sort 10/27/2006 6:56 AM

Expected Running Time,


Part 2 Deterministic Selection
(Probabilistic Fact #1: The expected number of coin tosses required We can do selection in O(n) worst-case time.
in order to get one head is 2)
(Probabilistic Fact #2: Expectation is a linear function:) Main idea: recursively use the selection algorithm itself to find a
„ E(X + Y ) = E(X ) + E(Y ) good pivot for quick-select:
„ E(cX ) = cE(X ) „ Divide S into n/5 sets of 5 each
Let T(n) denote the expected running time of quick-select, with cost c „ Find a median in each set
per recursive invocation.
By Fact #2, „ Recursively find the median of the “baby” medians.

„ T(n) < T(3n/4) + cn*(expected # of calls before a good call)

By Fact #1, Min size 1 1 1 1 1 1 1 1 1 1 1


„ T(n) < T(3n/4) + 2cn for L 2 2 2 2 2 2 2 2 2 2 2
That is, T(n) is a geometric series: 3 3 3 3 3 3 3 3 3 3 3
Min size
4 4 4 4 4 4 4 4 4 4 4
„ T(n) < 2cn + 2c(3/4)n + 2c(3/4)2n + 2c(3/4)3n + …
5 5 5 5 5 5 5 5 5 5 5 for G
So T(n) is O(n).
We can solve selection problem in O(n) expected time! (See Exercise C-10.23 for details of analysis.)

Selection 7 Selection 8

Вам также может понравиться