Introduction to Algorithms Prof. Charles E. Leiserson

Slides:



Advertisements
Similar presentations
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Advertisements

Analysis of Algorithms
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
CSE 3101: Introduction to the Design and Analysis of Algorithms
Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Introduction to Algorithms Jiafen Liu Sept
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Quicksort Lecture 4. Quicksort Divide and Conquer.
Foundations II: Data Structures and Algorithms
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Sorting.
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Lecture 2 Sorting.
CS 3343: Analysis of Algorithms
Order Statistics.
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Algorithms and Data Structures Lecture VI
Linear-Time Sorting Continued Medians and Order Statistics
Randomized Algorithms
Divide-And-Conquer-And-Combine
CSC 413/513: Intro to Algorithms
Order Statistics(Selection Problem)
CS 3343: Analysis of Algorithms
مرتب سازي سريع Quicksort
CO 303 Algorithm Analysis And Design Quicksort
Randomized Algorithms
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
Introduction to Algorithms
Introduction to Algorithms
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Algorithms: Design and Analysis
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
CS200: Algorithm Analysis
The Selection Problem.
Design and Analysis of Algorithms
Quicksort Quick sort Correctness of partition - loop invariant
CS200: Algorithm Analysis
Sorting Popular algorithms:
Presentation transcript:

Introduction to Algorithms Prof. Charles E. Leiserson 6.046J/18.401J LECTURE 4 Quicksort •Divide and conquer •Partitioning •Worst-case analysis •Intuition •Randomized quicksort •Analysis Prof. Charles E. Leiserson September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.1

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). September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.2

Divide and conquer Quicksort an n-element array: Divide: Partition the array into two subarrays around a pivot x such that elements in lower subarray ≤ x ≤ elements in upper subarray. ≤ x x ≥ x 2.Conquer: Recursively sort the two subarrays. Combine: Trivial. Key: Linear-time partitioning subroutine. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.3

Partitioning subroutine PARTITION(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 Invariant: x ≤ x ≥ x ? \p i j q September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.4

Example of partitioning 6 10 13 5 8 3 2 11 i j September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.5

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.6

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.7

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.8

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.9

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.10

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.11

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.12

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.13

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.14

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.15

Example of partitioning September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.16

Pseudocode for quicksort QUICKSORT(A, p, r) if p < r then q ← PARTITION(A, p, r) QUICKSORT(A, p, q–1) QUICKSORT(A, q+1, r) Initial call: QUICKSORT(A, 1, n) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.17

Analysis of quicksort • Assume all input elements are distinct. • In practice, there are better partitioning algorithms for when duplicate input elements may exist. • Let T(n) = worst-case running time on an array of n elements. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.18

Worst-case of quicksort • Input sorted or reverse sorted. • Partition around min or max element. • One side of partition always has no elements. (arithmetic series) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.19

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.20

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn T(n) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.21

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn cn T(0) T(n–1) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.22

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn cn T(0) c(n–1) T(0) T(n–2) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.23

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn cn T(0) c(n–1) T(0) c(n–2) T(0) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.24

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn cn T(0) c(n–1) T(0) c(n–2) T(0) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.25

Worst-case recursion tree T(n) = T(0) + T(n–1) + cn September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.26

Best-case analysis (For intuition only!) If we’re lucky, PARTITION splits the array evenly: T(n) = 2T(n/2) + (n) = (same as merge sort) What if the split is always ? What is the solution to this recurrence? September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.27

Analysis of “almost-best” case T (n) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.28

Analysis of “almost-best” case September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.29

Analysis of “almost-best” case September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.30

Analysis of “almost-best” case leaves September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.31

Analysis of “almost-best” case leaves September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.32

More intuition Suppose we alternate lucky, unlucky, lucky, unlucky, lucky, …. L(n) = 2U(n/2) +  (n) lucky U(n)= L(n –1) +  (n) unlucky Solving: L(n) = 2(L(n/2 – 1) +  (n/2)) +  (n) = 2L(n/2 – 1) +  (n) =  (n lg n) Lucky! How can we make sure we are usually lucky? September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.33

Randomized quicksort IDEA: Partition around a random element. • Running time is independent of the input order. • No assumptions need to be made about the input distribution. • No specific input elicits the worst-case behavior. • The worst case is determined only by the output of a random-number generator. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.34

Randomized quicksort analysis Let T(n) = the random variable for the running time of randomized quicksort on an input of size n, assuming random numbers are independent. For k = 0, 1, …, n–1, define the indicator random variable if PARTITION generates a k : n–k–1 split, otherwise. E[Xk] = Pr{Xk = 1} = 1/n, since all splits are equally likely, assuming elements are distinct. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.35

Analysis (continued) September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.36

Calculating expectation Take expectations of both sides. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.37

Calculating expectation Linearity of expectation. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.38

Calculating expectation Independence of Xk from other random choices. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.39

Calculating expectation Linearity of expectation; E[Xk] = 1/n . September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.40

Calculating expectation Summations have identical terms. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.41

Hairy recurrence (The k = 0, 1 terms can be absorbed in the  (n).) Prove: E[T(n)] ≤ anlg n for constant a > 0 . • Choose a large enough so that an lg n dominates E[T(n)] for sufficiently small n ≥ 2. Use fact: (exercise). September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.42

Substitution method Substitute inductive hypothesis. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.43

Substitution method Use fact. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.44

Substitution method Express as desired – residual. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.45

Substitution method if a is chosen large enough so that an/4 dominates the (n). September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.2

Quicksort in practice • Quicksort is a great general-purpose sorting algorithm. • Quicksort is typically over twice as fast as merge sort. • Quicksort can benefit substantially from code tuning. • Quicksort behaves well even with caching and virtual memory. September 21, 2005 Copyright © 2001-5 by Erik D. Demaine and Charles E. Leiserson L4.47