6.046_Lecture05_MedianOrderStatistics

6.046_Lecture05_MedianOrderStatistics - Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms, Lecture 5 Feburary 16, 2005 Order statistics, Median 1 Introduction to Algorithms 6.046J/18.401J Lecture 5 Prof. Manolis Kellis Today • Order statistics (e.g., finding median) •Tw o O(n) time algorithms: – Randomized: similar to Quicksort – Deterministic: quite tricky • Both are examples of divide and conquer Order statistics Select the i th smallest of n elements (the element with rank i ). i = 1 : minimum ; i = n : maximum ; i = ( n +1)/2 or ( n +1)/2 : median . How fast can we solve the problem ? Min/max: O(n) General i : O(n log n) by sorting We will see how to do it in O(n) time Randomized Algorithm for Finding the i th element • Divide and Conquer Approach • Main idea: PARTITION x x pq k x If i<k , recurse on the left If i>k , recurse on the right Otherwise, output x Randomized Divide-and-Conquer R AND -S ELECT ( A , p, r, i ) if p = r then return A [ p ] q R AND -P ARTITION ( A , p, r ) k q p + 1 Æ k =rank( A [ q ]) if i = k then return A [ q ] if i < k then return R AND -S ELECT ( A , p, q – 1 , i ) else return R AND -S ELECT ( A , q + 1 , r, i – k ) A [ q ] A [ q ] q pr k Example pivot i = 7 6 10 13 5 8 3 2 11 k = 4 Select the 7 – 4 = 3 rd smallest recursively. Select the i = 7 th smallest: 2 5 3 6 8 13 10 11 Partition:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Introduction to Algorithms, Lecture 5 Feburary 16, 2005 Order statistics, Median 2 Analysis Lucky: 1 0 1 log 9 / 10 = = n n C ASE 3 T ( n )= T (9 n /10) + Θ ( n ) = Θ ( n ) What is the worst-case running time ?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

6.046_Lecture05_MedianOrderStatistics - Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online