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

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

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J Lecture 6 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 27, 2004 L6.2 © Charles E. Leiserson and Piotr Indyk Today: sorting Show that Θ ( n lg n ) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in O(n) time. Hint: different models ?
Image of page 2
Introduction to Algorithms September 27, 2004 L6.3 © Charles E. Leiserson and Piotr Indyk Comparison sort All the sorting algorithms we have seen so far are comparison sorts : only use comparisons to determine the relative order of elements. E.g ., insertion sort, merge sort, quicksort, heapsort.
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 27, 2004 L6.4 © Charles E. Leiserson and Piotr Indyk x Partitioning subroutine P ARTITION ( A , p , r ) A [ p . . r ] x A [ p ] pivot = A [ p ] i p for j p + 1 to r do if A[ j] x then i i + 1 exchange A [ i ] A [ j ] exchange A [ p ] A [ i ] return i x x ? p i r j Invariant:
Image of page 4
Introduction to Algorithms September 27, 2004 L6.5 © Charles E. Leiserson and Piotr Indyk Comparison sort All of our algorithms used comparisons All of our algorithms have running time (n lg n) Is it the best that we can do using just comparisons ? Answer: YES, via decision trees
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 27, 2004 L6.6 © Charles E. Leiserson and Piotr Indyk Decision-tree example 1:2 1:2 2:3 2:3 123 1:3 1:3 132 312 1:3 1:3 213 2:3 2:3 231 321 Each internal node is labeled i : j for i , j {1, 2,…, n } . The left subtree shows subsequent comparisons if a i a j . The right subtree shows subsequent comparisons if a i a j . Sort a 1 , a 2 , …, a n (n=3)
Image of page 6
Introduction to Algorithms September 27, 2004 L6.7 © Charles E. Leiserson and Piotr Indyk Decision-tree example 1:2 1:2 2:3 2:3 123 1:3 1:3 132 312 1:3 1:3 213 2:3 2:3 231 321 Each internal node is labeled i : j for i , j {1, 2,…, n } . The left subtree shows subsequent comparisons if a i a j . The right subtree shows subsequent comparisons if a i a j . Sort a 1 , a 2 , a 3 = ⟨ 9, 4, 6 :
Image of page 7

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

View Full Document Right Arrow Icon
Introduction to Algorithms September 27, 2004 L6.8 © Charles E. Leiserson and Piotr Indyk Decision-tree example 1:2 1:2 2:3 2:3 123 1:3 1:3 132 312 1:3 1:3 213 2:3 2:3 231 321 Each internal node is labeled i : j for i , j {1, 2,…, n } . The left subtree shows subsequent comparisons if a i a j . The right subtree shows subsequent comparisons if a i a j . 9 4 Sort a 1 , a 2 , a 3 = ⟨ 9, 4, 6 :
Image of page 8
Introduction to Algorithms September 27, 2004 L6.9 © Charles E. Leiserson and Piotr Indyk Decision-tree example 1:2 1:2 2:3 2:3 123 1:3 1:3 132 312 1:3 1:3 213 2:3 2:3 231 321 Each internal node is labeled i : j for i , j {1, 2,…, n } . The left subtree shows subsequent comparisons if a i a j . The right subtree shows subsequent comparisons if a i a j . 9 6 Sort a 1 , a 2 , a 3 = ⟨ 9, 4, 6 :
Image of page 9

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

View Full Document Right Arrow Icon
Introduction to Algorithms September 27, 2004 L6.10 © Charles E. Leiserson and Piotr Indyk Decision-tree example 1:2 1:2 2:3 2:3 123 1:3 1:3 132 312 1:3 1:3 213 2:3 2:3 231 321 Each internal node is labeled i : j for i , j {1, 2,…, n } .
Image of page 10
Image of page 11
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