Title: Mergesort and Quicksort
1Mergesort and Quicksort
2Complexity Analysis of DC
l number of subinstances n original instance
size n/b size of subinstances k polynomial
order of g(n) O(nk) where g(n) cost
of doing the divide and recombination
3Complexity Analysis of DC
Can only be applied when t(n) l t(n/b) g(n)
and g(n) in ? (nk).
4Complexity of Mergesort
- t(n) 2t(n/2) g(n)
- so Equation 7.1 applies
- l 2, b 2, k 1
5Complexity of Quicksort
probability that Tn will be chosen as the pivot
consider the cost of each possible pivot selection
6Complexity of Quicksort
cost of sorting each subarray
3 1 4 1 5 9 2 6 5 3 5 8 9 2 1 3 1 3 9 5 6 5 4 5
8 9
pivot around 3...
l 5
l-14
n-l8
7Complexity of Quicksort
8Complexity of Quicksort
Equation 7.1 does not apply. What to do? Guess
that t(n) is in O(n log n), which means that
t(n) lt c n log n for some constant c. Or, ?
c. ?n. t(n) lt c n log n
9Constructive Induction
? c. ?n. t(n) lt c n log n Proof idea This
constant c exists, but we dont know a value.
So induct on the problem size n and construct a
bound on c as we go.
10Basis case
- Easy, just pick c so that
- for any value of n between 2 and the threshold
n0.
t(n) lt c n log n or c gt t(n)/n log n
11Inductive step