04-Quicksort

# 04-Quicksort - Algorithms LECTURE 4 Quicksort Divide and...

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

Algorithms L4.1 Professor Ashok Subramanian L ECTURE 4 Quicksort Divide and conquer Partitioning Worst-case analysis Intuition Randomized quicksort Analysis Algorithms

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

View Full Document
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).
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. 1. Conquer: Recursively sort the two subarrays. 2. Combine: Trivial. x x x Key: Linear-time partitioning subroutine.

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

View Full Document
Algorithms L4.4 x 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 ? p i q j Invariant:
Algorithms L4.5 Example of partitioning i j 6 10 13 5 8 3 2 11

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

View Full Document
Algorithms L4.6 Example of partitioning i j 6 10 13 5 8 3 2 11
Algorithms L4.7 Example of partitioning i j 6 10 13 5 8 3 2 11

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

View Full Document
Algorithms L4.8 Example of partitioning 6 10 13 5 8 3 2 11 i j 6 5 13 10 8 3 2 11
Algorithms L4.9 Example of partitioning 6 10 13 5 8 3 2 11 i j 6 5 13 10 8 3 2 11

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

View Full Document
Algorithms L4.10 Example of partitioning 6 10 13 5 8 3 2 11 i j 6 5 13 10 8 3 2 11
Algorithms L4.11 Example of partitioning 6 10 13 5 8 3 2 11 i j 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11

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

View Full Document
Algorithms L4.12 Example of partitioning 6 10 13 5 8 3 2 11 i j 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11
Algorithms L4.13 Example of partitioning 6 10 13 5 8 3 2 11 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11 i j 6 5 3 2 8 13 10 11

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

View Full Document
Algorithms L4.14 Example of partitioning 6 10 13 5 8 3 2 11 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11 i j 6 5 3 2 8 13 10 11
Algorithms L4.15 Example of partitioning 6 10 13 5 8 3 2 11 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11 i j 6 5 3 2 8 13 10 11

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

View Full Document
Algorithms L4.16 Example of partitioning 6 10 13 5 8 3 2 11 6 5 3 10 8 13 2 11 6 5 13 10 8 3 2 11 6 5 3 2 8 13 10 11 i 2 5 3 6 8 13 10 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 47

04-Quicksort - Algorithms LECTURE 4 Quicksort Divide and...

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

View Full Document
Ask a homework question - tutors are online