lecture_05

lecture_05 - Introduction to Algorithms 6.046J/18.401J...

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

Introduction to Algorithms 6.046J/18.401J Lecture 5 Prof. Piotr Indyk

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

View Full Document
Introduction to Algorithms September 22, 2004 L5.2 © Piotr Indyk and Charles Leiserson Today Order statistics (e.g., finding median) Two O(n) time algorithms: – Randomized: similar to Quicksort – Deterministic: quite tricky Both are examples of divide and conquer
Introduction to Algorithms September 22, 2004 L5.3 © Piotr Indyk and Charles Leiserson 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

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

View Full Document
Introduction to Algorithms September 22, 2004 L5.4 © Piotr Indyk and Charles Leiserson Randomized Algorithm for Finding the i th element Divide and Conquer Approach Main idea: P ARTITION x x x x pq k x If i<k , recurse on the left If i>k , recurse on the right Otherwise, output x
Introduction to Algorithms September 22, 2004 L5.5 © Piotr Indyk and Charles Leiserson 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 ] A [ q ] A [ q ] q pr k

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

View Full Document
Introduction to Algorithms September 22, 2004 L5.6 © Piotr Indyk and Charles Leiserson Example pivot i = 7 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. Select the i = 7 th smallest: 2 2 5 5 3 3 6 6 8 8 13 13 10 10 11 11 Partition:
Introduction to Algorithms September 22, 2004 L5.7 © Piotr Indyk and Charles Leiserson 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 ?

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.

This note was uploaded on 04/10/2008 for the course CSE 6.046J/18. taught by Professor Piotrindykandcharlese.leiserson during the Fall '04 term at MIT.

Page1 / 24

lecture_05 - Introduction to Algorithms 6.046J/18.401J...

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

View Full Document
Ask a homework question - tutors are online