- Randomized Selection
1.1 R Select (array A, length n, order statistic i)
(0): if n =1 return A
(1):choose pivot p from A uniformly at random.
(2):partition 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(1st part of A, j-1, i)
(6):if j < i: return RSelect(2nd part of A, n-j, i-j)
- Running Time of RSelect
2.1 RSelect Theorem: for every input array of length n, the average running time of RSelect is .
2.2 Tracking Progress via Phases.
Note: RSelect uses operations outside of the recursive call [for some constant c > 0]
Notation: RSelect is in phase j if current array size between and .
numbers of recursive calls during phase j.
Note: running time of RSelect
(1) : <= array size during phase
(2) : work per phase j subproblem.
(3):of phase -j subproblems.
2.3 Proof II: Reduction to Coin Flipping
:# of recursive calls during phase
Note: if RSelect choose a pivot giving a spltit (or better): then current phase ends!(new subarray length at most 75% of old length).
Recall: probability of split or better is .
So: expected number of times you need to flip a fair coin to get one “head”.
2.4 Proof III: Coin Flipping Analysis
Let number of coin flips until you get heads
(a “geometric random varible”)
: 1st coin flip.
: probablity of tails
# of further coin flips needed in this case.
Solution: (Recall )
expected running time of RSelect: