lecture_06

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

Info iconThis preview shows pages 1–10. 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
Background image of page 1

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

View Full DocumentRight 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 ?
Background 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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Introduction to Algorithms September 27, 2004 L6.4 © Charles E. Leiserson and Piotr Indyk x 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 x x ? ? pi r j Invariant:
Background 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
Background image of page 5

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

View Full DocumentRight 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 123 1:3 1:3 132 132 312 312 1:3 1:3 213 213 2:3 2:3 231 231 321 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)
Background 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 123 1:3 1:3 132 132 312 312 1:3 1:3 213 213 2:3 2:3 231 231 321 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 :
Background image of page 7

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

View Full DocumentRight 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 123 1:3 1:3 132 132 312 312 1:3 1:3 213 213 2:3 2:3 231 231 321 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 :
Background 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 123 1:3 1:3 132 132 312 312 1:3 1:3 213 213 2:3 2:3 231 231 321 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 .
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 62

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online