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 p q 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 ] q p r 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 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:
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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern