CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any...

21
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

description

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

Transcript of CSC317 1 Quicksort on average run time Well prove that average run time with random pivots for any...

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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).