![Page 1: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/1.jpg)
1CSC317
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
![Page 2: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/2.jpg)
2CSC317
Quicksort on average run time
• Let X = total number of comparisons in all calls to Partition
• Rename smallest and largest elements in A as z1, z2, …, zn
• Consider zi, zj with indices i < j
Example: [4 1 2 3 ]z1=1; z2=2; z3=3; z4=4
if zi and zj comparedotherwise
![Page 3: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/3.jpg)
3CSC317
Quicksort on average run time
How many times will zi and zj be compared?
Example:A=[8 1 6 4 0 3 9 5] zi = 3, zj = 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, sincepivot is not in the subsequent recursions to Partition
![Page 4: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/4.jpg)
4CSC317
Quicksort on average run time
How many times will zi and zj be compared?
Example:A=[8 1 6 4 0 3 9 5] zi = 3, zj = 9
When will two elements NOT be compared?
If pivot=5, then none of [8 6 9] will be compared to [1 4 0 3], so zi and zj not compared. Not in this call of partition, or any other call, since these sets will then be separated
![Page 5: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/5.jpg)
5CSC317
Quicksort on average run time
Probability that zi and zj be compared?
Consider i < j and set Zij = zi, zi+1, …, zj
• 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)
![Page 6: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/6.jpg)
6CSC317
Quicksort on average run time
Probability that zi and zj be compared?
Consider i < j and set Zij = zi, zi+1, …, zj
• 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)
![Page 7: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/7.jpg)
7CSC317
Quicksort on average run time
Pr {zi compared to zj} =
Pr { zi or zj chosen first as pivot in Zij } =
(since mutuallyexclusive)
Pr { zi first element chosen from Zij } +Pr { zj first element chosen from Zij } =
![Page 8: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/8.jpg)
8CSC317
Quicksort on average run time
Let X = total number of comparisons in all calls to Partition
if zi and zj comparedotherwise
All possible comparisons i and j
Prob zi compared to zj
![Page 9: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/9.jpg)
9CSC317
Quicksort on average run time
Prob zi compared
to zj
k= j-i
![Page 10: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/10.jpg)
10CSC317
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!
![Page 11: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/11.jpg)
11CSC317
Order statistics: another use of partition
• Array n unsorted
• Find kth smallest entries
• k = 1: Minimum
• : Median
![Page 12: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/12.jpg)
12CSC317
Order statistics: another use of partition
• Array n unsorted
• 1st, 2nd, 3rd smallest to largest; median …
• Another use of Partition
![Page 13: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/13.jpg)
13CSC317
Order statistics: Let’s start simple (1st order)
Minimum(A)1. Min = A[1]2. for i=2 to A.length3. if min>A[i]4. min = A[i]5. return min
Worst case? Θ(n)Best case? Θ(n)
![Page 14: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/14.jpg)
14CSC317
Order statistics
• Input: set A with n distinct numbers
• Output: find ith smallest values
Selection problem – more general problem
Upper bound?
We can solve in O(n log n) since we can always sortand find ith index in array
But we want to do BETTER!!!
![Page 15: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/15.jpg)
15CSC317
Order statistics
• Input: set A with n distinct numbers
• Output: find ith smallest values
Selection problem – more general problem
O(n) on average with randomized algorithm
Amazing given that (at least on average) similar to findingjust minimum!
![Page 16: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/16.jpg)
16CSC317
Selection problem
Randomized Select(A,p,r,i)‐1 if p==r //base case2 return A[p]3 q = Randomized-partition(A, p, r)4 k = q-p+1 //number elements from left up to pivot5 if i==k // pivot is the ith smallest6 return A[q]7 elseif i<k8 return Randomized-Select(A,p,q-1, i) //ith smallest left9 else return Randomized-Select(A,q+1,r,i-k) //on right
p rq< pivot > pivot
![Page 17: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/17.jpg)
17CSC317
p rq< 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-15. for j = p to r-16. if A[ j ] <= x7. i = i + 1;8. swap A[ i ] with A[ j ]9. swap A[ i+1 ] with A[ r ]10. return i+1
![Page 18: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/18.jpg)
18CSC317
Selection problem
Randomized Select(A,p,r,i)‐1 if p==r //base case2 return A[p]3 q = Randomized-partition(A, p, r)4 k = q-p+1 //number elements from left up to pivot5 if i==k // pivot is the ith smallest6 return A[q]7 elseif i<k8 return Randomized-Select(A,p,q-1, i) //ith smallest left9 else return Randomized-Select(A,q+1,r,i-k) //on right
p rq< pivot > pivot
Example:
A = [4 1 6 5 3], find the 3rd smallest value
![Page 19: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/19.jpg)
19CSC317
Selection problem
p rq< pivot > pivot
How is this different from randomized version ofQuicksort?Answer: Only one recursion (left or right); not two
![Page 20: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/20.jpg)
20CSC317
Selection problem
p rq< pivot > pivot
Analysis:
Worst case:
When we always partition around largest remainingElement, and recursion on array size n 1 (which takes ‐ θ(n))
Worse than a good sorting scheme.
![Page 21: CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing](https://reader034.vdocuments.us/reader034/viewer/2022051202/5a4d1b687f8b9ab0599b1c8f/html5/thumbnails/21.jpg)
21CSC317
Selection problem
p rq< 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).