# lec6 - Introduction to Algorithms 6.046J/18.401J LECTURE 6...

This preview shows pages 1–8. Sign up to view the full content.

September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 6 Order Statistics Randomized divide and conquer Analysis of expected time Worst-case linear-time order statistics Analysis Prof. Erik Demaine

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

View Full Document
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.2 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 . Naive algorithm : Sort and index i th element. Worst-case running time = Θ ( n lg n ) + Θ (1) = Θ ( n lg n ) , using merge sort or heapsort ( not quicksort).
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.3 Randomized divide-and- conquer algorithm R AND -S ELECT ( A , p, q, i ) i th smallest of A [ p .. q ] if p = q then return A [ p ] r R AND -P ARTITION ( A , p, q ) k r p + 1 k = rank( A [ r ]) if i = k then return A [ r ] if i < k then return R AND -S ELECT ( A , p, r – 1 , i ) else return R AND -S ELECT ( A , r + 1 , q, i – k ) A [ r ] A [ r ] A [ r ] A [ r ] r pq k

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

View Full Document
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.4 Example Select the i = 7 th smallest: i = 7 pivot 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 k = 4 Select the 7 – 4 = 3 rd smallest recursively. 2 2 5 5 3 3 6 6 8 8 13 13 10 10 11 11 Partition:
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.5 Intuition for analysis (All our analyses today assume that all elements are distinct.) Lucky: 1 0 1 log 9 / 10 = = n n C ASE 3 T ( n )= T (9 n /10) + Θ ( n ) = Θ ( n ) Unlucky: T ( n T ( n –1) ±+± Θ ( n ) = Θ ( n 2 ) arithmetic series Worse than sorting!

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

View Full Document
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.6 Analysis of expected time The analysis follows that of randomized quicksort, but it’s a little different. Let T ( n ) = the random variable for the running time of R AND -S ELECT on an input of size n , assuming random numbers are independent. For k = 0, 1, …, n –1 , define the indicator random variable X k = 1 if P ARTITION generates a k : n k –1 split, 0 otherwise.
September 28, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L6.7 Analysis (continued) T ( n ) = T (max{0, n –1}) + Θ ( n ) if 0: n –1 split, T (max{1, n –2}) + Θ ( n ) if 1: n –2 split, M T (max{ n –1, 0}) + Θ ( n ) if n –1 : 0 split, () = Θ + = 1 0 ) ( }) 1 , (max{ n k k n k n k T X .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.