CS 332: Algorithms. Quicksort

Similar documents
Algorithms. Quicksort. Slide credit: David Luebke (Virginia)

CS 332: Algorithms. Linear-Time Sorting. Order statistics. Slide credit: David Luebke (Virginia)

Sorting Algorithms. Algorithms Kyuseok Shim SoEECS, SNU.

Analysis of Algorithms -Quicksort-

This Lecture. Divide and Conquer. Merge Sort: Algorithm. Merge Sort Algorithm. MergeSort (Example) - 1. MergeSort (Example) - 2

CS / MCS 401 Homework 3 grader solutions

CS583 Lecture 02. Jana Kosecka. some materials here are based on E. Demaine, D. Luebke slides

Algorithms and Data Structures Lecture IV

Design and Analysis of Algorithms

CS 270 Algorithms. Oliver Kullmann. Growth of Functions. Divide-and- Conquer Min-Max- Problem. Tutorial. Reading from CLRS for week 2

4.3 Growth Rates of Solutions to Recurrences

Data Structures and Algorithm. Xiaoqing Zheng

Divide & Conquer. Divide-and-conquer algorithms. Conventional product of polynomials. Conventional product of polynomials.

Merge and Quick Sort

Model of Computation and Runtime Analysis

Data Structures Lecture 9

A recurrence equation is just a recursive function definition. It defines a function at one input in terms of its value on smaller inputs.

Introduction to Algorithms

Matriculation number: You have 90 minutes to complete the exam of InformatikIIb. The following rules apply:

Department of Informatics Prof. Dr. Michael Böhlen Binzmühlestrasse Zurich Phone:

Introduction to Algorithms

Sequences, Mathematical Induction, and Recursion. CSE 2353 Discrete Computational Structures Spring 2018

CSE 4095/5095 Topics in Big Data Analytics Spring 2017; Homework 1 Solutions

Analysis of Algorithms. Introduction. Contents

Fundamental Algorithms

Lecture 3: Asymptotic Analysis + Recurrences

Model of Computation and Runtime Analysis

Test One (Answer Key)

A Probabilistic Analysis of Quicksort

Recursive Algorithms. Recurrences. Recursive Algorithms Analysis

Similar idea to multiplication in N, C. Divide and conquer approach provides unexpected improvements. Naïve matrix multiplication

Dotting The Dot Map, Revisited. A. Jon Kimerling Dept. of Geosciences Oregon State University

CSI 5163 (95.573) ALGORITHM ANALYSIS AND DESIGN

Infinite Sequences and Series

Lecture 7: Solving Recurrences

Average-Case Analysis of QuickSort

Divide and Conquer. 1 Overview. 2 Multiplying Bit Strings. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

CSE 202 Homework 1 Matthias Springer, A Yes, there does always exist a perfect matching without a strong instability.

Merge Sort. Outline and Reading. Divide-and-Conquer. Divide-and-conquer paradigm ( 4.1.1) Merge-sort ( 4.1.1)

Examples: data compression, path-finding, game-playing, scheduling, bin packing

An Introduction to Randomized Algorithms

) n. ALG 1.3 Deterministic Selection and Sorting: Problem P size n. Examples: 1st lecture's mult M(n) = 3 M ( È

Recurrence Relations

Introduction to Algorithms 6.046J/18.401J LECTURE 3 Divide and conquer Binary search Powering a number Fibonacci numbers Matrix multiplication

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, February 4/Tuesday, February 5

Problem Set 1 Solutions

Algorithm Analysis. Algorithms that are equally correct can vary in their utilization of computational resources

COMP285 Midterm Exam Department of Mathematics

Math 155 (Lecture 3)

Classification of problem & problem solving strategies. classification of time complexities (linear, logarithmic etc)

CS:3330 (Prof. Pemmaraju ): Assignment #1 Solutions. (b) For n = 3, we will have 3 men and 3 women with preferences as follows: m 1 : w 3 > w 1 > w 2

CS161 Design and Analysis of Algorithms. Administrative

CSE 5311 Notes 1: Mathematical Preliminaries

Complex Numbers Solutions

Hand Out: Analysis of Algorithms. September 8, Bud Mishra. In general, there can be several algorithms to solve a problem; and one is faced

Divide and Conquer. 1 Overview. 2 Insertion Sort. COMPSCI 330: Design and Analysis of Algorithms 1/19/2016 and 1/21/2016

Because it tests for differences between multiple pairs of means in one test, it is called an omnibus test.

Disjoint set (Union-Find)

w (1) ˆx w (1) x (1) /ρ and w (2) ˆx w (2) x (2) /ρ.

0 1 sum= sum= sum= sum= sum= sum= sum=64

OPTIMAL ALGORITHMS -- SUPPLEMENTAL NOTES

Math F215: Induction April 7, 2013

Algorithms Design & Analysis. Divide & Conquer

Math 475, Problem Set #12: Answers

(ii) Two-permutations of {a, b, c}. Answer. (B) P (3, 3) = 3! (C) 3! = 6, and there are 6 items in (A). ... Answer.

Sums, products and sequences

CS 5150/6150: Assignment 1 Due: Sep 23, 2010

WRITTEN ASSIGNMENT 1 ANSWER KEY

CS161: Algorithm Design and Analysis Handout #10 Stanford University Wednesday, 10 February 2016

Spectral Partitioning in the Planted Partition Model

Design and Analysis of Algorithms

Design and Analysis of ALGORITHM (Topic 2)

Chapter 22 Developing Efficient Algorithms

ECEN 655: Advanced Channel Coding Spring Lecture 7 02/04/14. Belief propagation is exact on tree-structured factor graphs.

CS 171 Lecture Outline October 09, 2008

The Binomial Theorem

ORIE 633 Network Flows September 27, Lecture 8

Chapter 2. Asymptotic Notation

Hashing and Amortization

2 DD2458 Popup HT Solution: Choose the activity which ends first and does not conflict with earlier chosen activities.

Math 609/597: Cryptography 1

Lecture 2: Monte Carlo Simulation

Lecture 4. Quicksort

1 Generating functions for balls in boxes

Lecture 4 Recursive Algorithm Analysis. Merge Sort Solving Recurrences The Master Theorem

Data Structures and Algorithms

Problem 4: Evaluate ( k ) by negating (actually un-negating) its upper index. Binomial coefficient

Sequences A sequence of numbers is a function whose domain is the positive integers. We can see that the sequence

6.046 Recitation 5: Binary Search Trees Bill Thies, Fall 2004 Outline

CSED233: Data Structures (2018F) Lecture13: Sorting and Selection

ECE-S352 Introduction to Digital Signal Processing Lecture 3A Direct Solution of Difference Equations

Basic Combinatorics. Math 40210, Section 01 Spring Homework 7 due Monday, March 26

Induction: Solutions

Chapter 8: STATISTICAL INTERVALS FOR A SINGLE SAMPLE. Part 3: Summary of CI for µ Confidence Interval for a Population Proportion p

COMP26120: More on the Complexity of Recursive Programs (2018/19) Lucas Cordeiro

The Binomial Theorem

Lecture 11: Pseudorandom functions

Riemann Sums y = f (x)

2.4 Sequences, Sequences of Sets

Shannon s noiseless coding theorem

Transcription:

CS 33: Aorithms Quicsort David Luebe //03

Homewor Assiged today, due ext Wedesday Will be o web page shortly after class Go over ow David Luebe //03

Review: Quicsort Sorts i place Sorts O( ) i the average case Sorts O( ) i the worst case But i practice, it s quic Ad the worst case does t happe ofte (but more o this later ) David Luebe 3 //03

Quicsort Aother divide-ad-coquer aorithm The array A[p..r] is partitioed ito two oempty subarrays A[p..q] ad A[q+..r] Ivariat: All elemets i A[p..q] are less tha all elemets i A[q+..r] The subarrays are recursively sorted by calls to quicsort Ulie merge sort, o combiig step: two subarrays form a already-sorted array David Luebe 4 //03

Quicsort Code Quicsort(A, p, r) { if (p < r) { q = Partitio(A, p, r); Quicsort(A, p, q); Quicsort(A, q+, r); } } David Luebe 5 //03

Partitio Clearly, all the actio taes place i the partitio() fuctio Rearrages the subarray i place Ed result: Two subarrays All values i first subarray all values i secod Returs the idex of the pivot elemet separatig the two subarrays How do you suppose we implemet this? David Luebe 6 //03

Partitio I Words Partitio(A, p, r): Select a elemet to act as the pivot (which?) Grow two regios, A[p..i] ad A[j..r] All elemets i A[p..i] <= pivot All elemets i A[j..r] >= pivot Icremet i util A[i] >= pivot Decremet j util A[j] <= pivot Swap A[i] ad A[j] Repeat util i >= j Retur j Note: slightly differet from boo s partitio() David Luebe 7 //03

Partitio Code Partitio(A, p, r) x = A[p]; i = p - ; j = r + ; while (TRUE) repeat j--; util A[j] <= x; repeat i++; util A[i] >= x; if (i < j) Swap(A, i, j); else retur j; Illustrate o A = {5, 3,, 6, 4,, 3, 7}; What is the ruig time of partitio()? David Luebe 8 //03

Partitio Code Partitio(A, p, r) x = A[p]; i = p - ; j = r + ; while (TRUE) repeat j--; util A[j] <= x; repeat i++; util A[i] >= x; if (i < j) Swap(A, i, j); else retur j; partitio() rus i O() time David Luebe 9 //03

Aalyzig Quicsort What will be the worst case for the aorithm? Partitio is always ubalaced What will be the best case for the aorithm? Partitio is perfectly balaced Which is more liely? The latter, by far, except... Will ay particular iput elicit the worst case? Yes: Already-sorted iput David Luebe 0 //03

Aalyzig Quicsort I the worst case: T() = () T() = T( - ) + () Wors out to T() = ( ) David Luebe //03

Aalyzig Quicsort I the best case: T() = T(/) + () What does this wor out to? T() = ( ) David Luebe //03

Improvig Quicsort The real liability of quicsort is that it rus i O( ) o already-sorted iput Boo discusses two solutios: Radomize the iput array, OR Pic a radom pivot elemet How will these solve the problem? By isurig that o particular iput ca be chose to mae quicsort ru i O( ) time David Luebe 3 //03

Aalyzig Quicsort: Average Case Assumig radom iput, average-case ruig time is much closer to O( ) tha O( ) First, a more ituitive explaatio/example: Suppose that partitio() always produces a 9-to- split. This loos quite ubalaced! The recurrece is thus: T() = T(9/0) + T(/0) + Use istead of O() for coveiece (how?) How deep will the recursio go? (draw it) David Luebe 4 //03

Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? David Luebe 5 //03

Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? We fail Eglish David Luebe 6 //03

Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? We ed up with three subarrays, size, (-)/, (-)/ Combied cost of splits = + - = - = O() No worse tha if we had good-split the root ode! David Luebe 7 //03

Aalyzig Quicsort: Average Case Ituitively, the O() cost of a bad split (or or 3 bad splits) ca be absorbed ito the O() cost of each good split Thus ruig time of alteratig bad ad good splits is still O( ), with slightly higher costats How ca we be more rigorous? David Luebe 8 //03

Aalyzig Quicsort: Average Case For simplicity, assume: All iputs distict (o repeats) Slightly differet partitio() procedure partitio aroud a radom elemet, which is ot icluded i subarrays all splits (0:-, :-, :-3,, -:0) equally liely What is the probability of a particular split happeig? Aswer: / David Luebe 9 //03

Aalyzig Quicsort: Average Case So partitio geerates splits (0:-, :-, :-3,, -:, -:0) each with probability / If T() is the expected ruig time, T 0 T T What is each term uder the summatio for? What is the () term for? David Luebe 0 //03

Aalyzig Quicsort: Average Case So T 0 T T 0 Note: this is just lie the boo s recurrece (p66), except that the summatio starts with =0 We ll tae care of that i a secod T Write it o the board David Luebe //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer What s the aswer? Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe 3 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe 4 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds What s the iductive hypothesis? Substitute it i for some value < Prove that it follows for David Luebe 5 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < Prove that it follows for David Luebe 6 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < What value? Prove that it follows for David Luebe 7 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < The value i the recurrece Prove that it follows for David Luebe 8 //03

Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < The value i the recurrece Prove that it follows for Grid through it David Luebe 9 //03

T Aalyzig Quicsort: Average Case T 0 0 b a b b a b a b a b The recurrece to be solved Plug What i iductive are we doig hypothesis here? Expad What out are the we =0 doig case here? b/ is just a costat, What are we doig here? so fold it ito () Note: leavig the same recurrece as the boo David Luebe 30 //03

Aalyzig Quicsort: Average Case T a b a a a b b b ( ) This summatio gets its ow set of slides later The recurrece to be solved Distribute What are the we summatio doig here? Evaluate the summatio: What are we doig here? b+b+ +b = b (-) Sice What -<, are we b(-)/ doig here? < b David Luebe 3 //03

Aalyzig Quicsort: Average Case T a a a a a a 4 b b b 8 b b b The recurrece to be solved What We ll the prove hell? this later Distribute What are the we (a/) doig term here? Remember, our goal is to get What are we doig here? T() a + b Pic a large eough that How did we do this? a/4 domiates ()+b David Luebe 3 //03 a 4

Aalyzig Quicsort: Average Case So T() a + b for certai a ad b Thus the iductio holds Thus T() = O( ) Thus quicsort rus i O( ) time o average (phew!) Oh yeah, the summatio David Luebe 33 //03

What are we doig here? The i the secod term is bouded by Tightly Boudig The Key Summatio What are we doig here? Split the summatio for a tighter boud David Luebe 34 //03 is bouded by What are we doig here? Move the outside the summatio

The summatio boud so far Tightly Boudig The Key Summatio What are we doig here? The i the first term is bouded by / David Luebe 35 //03 bouded by / What are we doig here? / = - What are we doig here? Move ( - ) outside the summatio

The summatio boud so far Tightly Boudig The Key Summatio What are we doig here? Distribute the ( - ) David Luebe 36 //03 ) ( What are we doig here? The summatios overlap i rage; combie them What are we doig here? The Guassia series

The summatio boud so far Tightly Boudig The Key Summatio ) ( What are we doig here? Rearrage first term, place upper boud o secod David Luebe 37 //03 4 8 upper boud o secod What are we doig? X Guassia series What are we doig? Multiply it all out

Tightly Boudig The Key Summatio 8 8 whe 4 Doe!!! David Luebe 38 //03