lecture04

# lecture04 - Introduction to Algorithms...

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

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 4 Prof. Charles E. Leiserson

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

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

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

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

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

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

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

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

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

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

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

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

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

View Full Document
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
Day 6 Introduction to Algorithms L4.15

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.

## lecture04 - Introduction to Algorithms...

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

View Full Document
Ask a homework question - tutors are online