1 QuickSort Worst time: (n 2 ) Expected time: (nlgn) – Constants in the expected time are small Sorts in place
2 QuickSort (cont) DIVIDE – Partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is A[q] each element of A[q+1..r] Conquer – Sort the two subarrays by recursive calls to Quicksort Combine – Since subarrays are sorted in place, they are already sorted
3 QuickSort (cont) To sort entire array: QuickSort( A, 1, length(A) ) QuickSort( A, p, r ) 1.if p < r 2.q Partition( A, p, r ) 3.QuickSort( A, p, q-1 ) 4.QuickSort( A, q+1, r )
4 QuickSort (cont) Partition( A, p, r ) x A[ r ] i p – 1 for j p to r-1 if A[ j ] x i i + 1 Exchange( A[ i ], A[ j ] ) Exchange( A[ i+1 ], A[ r ] ) return i+1
5 QuickSort (cont)
6 Return i+1 which is 4
7 Performance of QuickSort Partition function’s running time - (n) Running time of QuickSort depends on the balance of the partitions – If balanced, QuickSort is asymptotically as fast as MergeSort – If unbalanced, it is asymptotically as bad as Insertion Sort
8 Performance of QuickSort (cont) Worst-case Partitioning – Partitions always of size n-1 and 0 – Occurs when array is already sorted – Recurrence for the running time:
9 Performance of QuickSort (cont) cn c(n-1) c(n-2) n cn c(n-1) c(n-2) c Total: (n 2 )
10 Performance of QuickSort (cont) Best-case Partitioning – Partitions always of size n/2 and n/2 -1 – The recurrence for the running time: Case 2 of Master Method
11 Performance of QuickSort (cont) Balanced Partitioning – Average-case is closer to best-case – Any split of constant proportionality (say 99 to 1) will have a running time of (nlgn) The recurrence will be Because it yields a recursion tree of depth (lgn), where cost at each level is (n) See page 151 (new book) for picture – Or next slide
12 Performance of QuickSort (cont) c(n/100)c(99n/100) cn c(n/10000)c(99n/10000) c(9801n/10000) T(1) Total: (nlgn) log 100 n cn cn log 100/99 n
13 Performance of QuickSort (cont) Intuition for the average case – The behavior depends on the relative ordering of the values Not the values themselves – We will assume (for now) that all permutations are equally likely – Some splits will be balanced, and some will be unbalanced
14 Performance of QuickSort (cont) – In a recursion tree for an average-case, the “good” and “bad” splits are distributed randomly throughout the tree – For our example, suppose Bad splits and good splits alternate Good splits are best-case splits Bad splits are worst-case splits Boundary case (subarray size 0) has cost of 1
15 Performance of QuickSort (cont) – The (n-1) cost of the bad split can be absorbed into the (n) cost of the good split, and the resulting split is good – Thus the running time is (nlgn), but with a slightly larger constant n 0n - 1 (n-1) / 2 (n-1) / 2 -1 (n)(n) n (n-1) / 2 (n)(n)
16 Randomized QuickSort How do we increase the chance that all permutations are equally likely? – Random Sampling Don’t always use last element in subarray Swap it with a randomly chosen element from the subarray – Pivot now is equally likely to be any of the r – p + 1 elements We can now expect the split to be reasonably well- balanced on average
17 Randomized QuickSort (cont) Randomized-Partition( A, p, r ) 1.i Random( p, r ) 2.Exchange( A[ r ], A[ i ] ) 3.return Partition( A, p, r ) Note that Partition( ) is same as before
18 Randomized QuickSort (cont) Randomized-QuickSort( A, p, r ) 1.if p < r 2.q Randomized-Partition( A, p, r ) 3.Randomized-QuickSort( A, p, q-1 ) 4.Randomized-QuickSort( A, q+1, r )
19 Analysis of QuickSort A more rigorous analysis Begin with worst-case – We intuited that worst-case running time is (n 2 ) – Use substitution method to show this is true
20 Analysis of QuickSort (cont) – Guess: (n 2 ) – Show: for some c > 0 – Substitute: q 2 +(n-q-1) 2 is max at endpoints of range. Therefore it is (n-1) 2 = n 2 – 2n +1
21 Analysis of QuickSort (cont) – Problem has you show that – Thus the worst-case running time of QuickSort is (n 2 )
22 Analysis of QuickSort (cont) We need to show that the upper-bound on expected running time is (nlgn) We’ve already shown that the best-case running time is (nlgn) Combined, these will give an expected running time of (nlgn)
23 Analysis of QuickSort (cont) Expected Running Time – Work done is dominated by Partition – Each time a pivot is selected, this element is never included in subsequent calls to QuickSort And the pivot is in its correct place in the array – Therefore, at most n calls to Partition will be made Each call to Partition involves (1) work plus the amount of work done in the for loop Count the total number of times line 4 is executed, we can bound the amount of time spent in the for loop Line 4: if A[j] x