lecture_06

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

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

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

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

View Full Document
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 ?
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.

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

View Full Document
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:
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

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

View Full Document
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)
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 :

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

View Full Document
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 :
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 .

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 / 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
Ask a homework question - tutors are online