CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.

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

Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
1 More Sorting; Searching Dan Barrish-Flood. 2 Bucket Sort Put keys into n buckets, then sort each bucket, then concatenate. If keys are uniformly distributed.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
CS 3343: Analysis of Algorithms Lecture 14: Order Statistics.
Medians and Order Statistics
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Spring 2015 Lecture 5: QuickSort & Selection
CSL758 Instructors: Naveen Garg Kavitha Telikepalli Scribe: Manish Singh Vaibhav Rastogi February 7 & 11, 2008.
Median/Order Statistics Algorithms
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
WS Algorithmentheorie 03 – Randomized Algorithms (Overview and randomised Quicksort) Prof. Dr. Th. Ottmann.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Linear-Time Selection Randomized Selection (Algorithm) Design and Analysis of Algorithms I.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Selection: Find the ith number
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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.
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.
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.
Order Statistics David Kauchak cs302 Spring 2012.
Order Statistics(Selection Problem)
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
1 Medians and Order Statistics CLRS Chapter 9. upper median lower median The lower median is the -th order statistic The upper median.
Lecture 10. Paradigm #8: Randomized Algorithms Back to the “majority problem” (finding the majority element in an array A). FIND-MAJORITY(A, n) while (true)
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
CSC317 1 Hiring problem-review Cost to interview (low C i ) Cost to fire/hire … (expensive C h ) n number of candidates m hired O (c i n + c h m) Independent.
Randomized Quicksort (8.4.2/7.4.2) Randomized Quicksort –i = Random(p, r) –swap A[p]  A[i] –partition A(p, r) Average analysis = Expected runtime –solving.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Analysis of Algorithms CS 477/677
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Order Statistics.
Order Statistics Comp 122, Spring 2004.
Randomized Algorithms
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
Order Statistics(Selection Problem)
Randomized Algorithms
Lecture 3 / 4 Algorithm Analysis
Medians and Order Statistics
CS 3343: Analysis of Algorithms
Order Statistics Comp 550, Spring 2015.
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Tomado del libro Cormen
Chapter 9: Medians and Order Statistics
Order Statistics Comp 122, Spring 2004.
Chapter 9: Selection of Order Statistics
CS 583 Analysis of Algorithms
The Selection Problem.
Quicksort and Randomized Algs
CS200: Algorithm Analysis
Medians and Order Statistics
Presentation transcript:

CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing pivot Average as good as best case! Most work of Quicksort in comparisons Each call to partition is constant plus number of comparisons in for loop Let X = total number of comparisons in all calls to Partition

CSC317 2 Quicksort on average run time Let X = total number of comparisons in all calls to Partition Rename smallest and largest elements in A as z 1, z 2, …, z n Consider z i, z j with indices i < j Example: [ ] z 1 =1; z 2 =2; z 3 =3; z 4 =4 if z i and z j compared otherwise

CSC317 3 Quicksort on average run time How many times will z i and z j be compared? Example: A=[ ] z i = 3, z j = 9 When will two elements be compared? Only if one of them (3 or 9) is chosen as a pivot, since in Partition, each element compared only with pivot. They will then never be compared again, since pivot is not in the subsequent recursions to Partition

CSC317 4 Quicksort on average run time How many times will z i and z j be compared? Example: A=[ ] z i = 3, z j = 9 When will two elements NOT be compared? If pivot=5, then none of [8 6 9] will be compared to [ ], so z i and z j not compared. Not in this call of partition, or any other call, since these sets will then be separated

CSC317 5 Quicksort on average run time Probability that z i and z j be compared? Consider i < j and set Z ij = z i, z i+1, …, z j This set will remain together in calls to Partition, unless one of them is a pivot Because otherwise pivot will be smaller than i or larger than j, so these will remain together (Not necessarily in order in array A)

CSC317 6 Quicksort on average run time Probability that z i and z j be compared? Consider i < j and set Z ij = z i, z i+1, …, z j If i or j chosen first as pivot within this set, they’ll be compared Otherwise, another element will be chosen as pivot, and they will be separated Since (j-i+1) elements in this set, the probability of any element as pivot: (Not necessarily in order in array A)

CSC317 7 Quicksort on average run time Pr {z i compared to z j } = Pr { z i or z j chosen first as pivot in Z ij } = (since mutually exclusive) Pr { z i first element chosen from Z ij } + Pr { z j first element chosen from Z ij } =

CSC317 8 Quicksort on average run time Let X = total number of comparisons in all calls to Partition if z i and z j compared otherwise All possible comparisons i and j Prob z i compared to z j

CSC317 9 Quicksort on average run time Prob z i compared to z j k= j-i

CSC Quicksort on average run time We showed that average run time with random pivots for any input array is O(n log n) Randomness is in choosing pivot Average as good as best case!

CSC Order statistics: another use of partition Array n unsorted Find k th smallest entries k = 1: Minimum : Median

CSC Order statistics: another use of partition Array n unsorted 1 st, 2 nd, 3 rd smallest to largest; median … Another use of Partition

CSC Order statistics: Let’s start simple (1 st order) Minimum(A) 1. Min = A[1] 2. for i=2 to A.length 3. if min>A[i] 4. min = A[i] 5. return min Worst case? Θ(n) Best case? Θ(n)

CSC Order statistics Input: set A with n distinct numbers Output: find i th smallest values Selection problem – more general problem Upper bound? We can solve in O(n log n) since we can always sort and find i th index in array But we want to do BETTER!!!

CSC Order statistics Input: set A with n distinct numbers Output: find i th smallest values Selection problem – more general problem O(n) on average with randomized algorithm Amazing given that (at least on average) similar to finding just minimum!

CSC Selection problem Randomized‐Select(A,p,r,i) 1 if p==r //base case 2return A[p] 3 q = Randomized-partition(A, p, r) 4 k = q-p+1 //number elements from left up to pivot 5 if i==k // pivot is the i th smallest 6return A[q] 7 elseif i<k 8return Randomized-Select(A,p,q-1, i) //ith smallest left 9 else return Randomized-Select(A,q+1,r,i-k) //on right p r q < pivot> pivot

CSC p r q < pivot> pivot Randomized-partition (A, p, r) 1. i = Random(p, r) 2. swap A[r] with A[i] 3. x = A[r] 4. i=p-1 5. for j = p to r-1 6. if A[ j ] <= x 7. i = i + 1; 8. swap A[ i ] with A[ j ] 9. swap A[ i+1 ] with A[ r ] 10. return i+1

CSC Selection problem Randomized‐Select(A,p,r,i) 1 if p==r //base case 2return A[p] 3 q = Randomized-partition(A, p, r) 4 k = q-p+1 //number elements from left up to pivot 5 if i==k // pivot is the i th smallest 6return A[q] 7 elseif i<k 8return Randomized-Select(A,p,q-1, i) //ith smallest left 9 else return Randomized-Select(A,q+1,r,i-k) //on right p r q < pivot> pivot Example: A = [ ], find the 3 rd smallest value

CSC Selection problem p r q < pivot> pivot How is this different from randomized version of Quicksort? Answer: Only one recursion (left or right); not two

CSC Selection problem p r q < pivot > pivot Analysis: Worst case: When we always partition around largest remaining Element, and recursion on array size n‐1 (which takes θ(n) ) Worse than a good sorting scheme.

CSC Selection problem p r q < pivot > pivot Analysis: However a 1/10 and 9/10 scheme isn’t bad … Remember master theorem: Therefore: Average solution isn’t bad either ( θ(n), Proof similar to quicksort).