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

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

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J Lecture 5 Prof. Piotr Indyk
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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:
Image of page 6
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 ?
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern