Analysis of Algorithms - Medians and Order Statistics
Analysis of Algorithms - Medians and Order Statistics
1st and nth Order Statistics The 1st and nth order statistics can be got in using n-1 comparisons using the following algorithm and its variants minimum(A) 1 min=A[1] 2 for i=2 to n 3 if min>A[i] i >A[i] 4 min=A[i] 5 return min Exercise: 1. Analyze the algorithm to get its running time 2. Modify the algorithm to get the 2nd order statistic
3
Selection in Expected Linear Time There is a divide and conquer algorithm for the selection problem Works in the same way as quick sort in partitioning the array of numbers While as quick sort is expected to run in (nlogn), RANDOMIZED SELECT RANDOMIZED-SELECT is (n) Uses RANDOMIZED-PARTITION to increase the likelihood of items being partitioned into two equal sets The algorithm is given in the next slide
Selection in Expected Linear Time RANDOMIZED-SELECT(A, p, r, i) 1 if p = r 2 return A[p] 3 q RANDOMIZED-PARTITION(A, p, r) 4 kq-p+1 5 if i = k // the pivot value is the answer 6 return A[q] 7 elseif i < k 8 return RANDOMIZED-SELECT(A, p, q - 1, i) 9 else return RANDOMIZED-SELECT(A, q + 1, r, i - k)
6/15/2011
PARTITION(A, p, r) 1 x A[r] 2 ip-1 3 for j p to r - 1 4 if A[j] x 5 ii+1 6 exchange A[i] A[j] 7 exchange A[i + 1] A[r] 8 return i + 1
medians and order statistics
7
x
i =0
1 = 1 x , if | x |< 1
n ( 1 )i 2
i =0
log n
< n( 11 .5 ) 0 = 2n = ( n )