lecture04 - Introduction to Algorithms...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 4 Prof. Charles E. Leiserson
Background image of page 1

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.2 Quicksort Proposed by C.A.R. Hoare in 1962. Divide-and-conquer algorithm. Sorts “in place” (like insertion sort, but not like merge sort). Very practical (with tuning).
Background image of page 2
Day 6 Introduction to Algorithms L4.3 Divide and conquer Quicksort an n -element array: 1. Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray x elements in upper subarray. 2. Conquer: Recursively sort the two subarrays. 3. Combine: Trivial. x x x x x x Key: Linear-time partitioning subroutine.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.4 x x Running time = O ( n ) for n elements. Running time = O ( n ) for n elements. Partitioning subroutine P ARTITION ( A , p , q ) A [ p . . q ] x A [ p ] pivot = A [ p ] i p for j p + 1 to q 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 q j Invariant:
Background image of page 4
Day 6 Introduction to Algorithms L4.5 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11
Background image of page 5

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.6 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11
Background image of page 6
Day 6 Introduction to Algorithms L4.7 Example of partitioning ij 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11
Background image of page 7

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.8 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Background image of page 8
Day 6 Introduction to Algorithms L4.9 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Background image of page 9

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.10 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Background image of page 10
Day 6 Introduction to Algorithms L4.11 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Background image of page 11

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.12 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11
Background image of page 12
Day 6 Introduction to Algorithms L4.13 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 2 2 8 8 13 13 10 10 11 11
Background image of page 13

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

View Full DocumentRight Arrow Icon
Day 6 Introduction to Algorithms L4.14 Example of partitioning 6 6 10 10 13 13 5 5 8 8 3 3 2 2 11 11 6 6 5 5 3 3 10 10 8 8 13 13 2 2 11 11 6 6 5 5 13 13 10 10 8 8 3 3 2 2 11 11 ij 6 6 5 5 3 3 2 2 8 8 13 13 10 10 11 11
Background image of page 14
Day 6 Introduction to Algorithms L4.15
Background image of page 15

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

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

Page1 / 47

lecture04 - Introduction to Algorithms...

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

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