Sept 28, 2022
Base Case: T(1)≤c⋅1
c0≤c
True for any c>c0
Assume: T(n2)≤cn2log(n2)
Show: T(n)≤cnlog(n)
2⋅T(n2)+c1+c2n≤cnlog(n)
By the assumption and transitivity, showing the following inequality suffices:
2cn2log(n2)+c1+c2n≤cnlog(n)
cnlog(n)−cnlog(2)+c1+c2n≤cnlog(n)
c1+c2n≤cnlog(2)
c1nlog(2)+c2log(2)≤c
True for any n0≥c1log(2) and c>c2log(2)+1
All of the "work" is in the combine step.
Can we put the work in the divide step?
Idea 1: Partition the data on the median value.
Idea 2: Partition the data in-place.
def idealizedQuickSort(arr: Array[Int], from: Int, until: Int): Unit =
{
if(until - from < 1){ return }
val pivot = ???
var low = from, high = until -1
while(low < high){
while(arr(low) <= pivot && low < high){ low ++ }
if(low < high){
while(arr(high) > pivot && low < high){ high ++ }
swap(arr, low, high)
}
}
idealizedQuickSort(arr, from = 0, until = low)
idealizedQuickSort(arr, from = low, until = until)
}
If we can obtain a pivot in O(1), what's the complexity?
Tquicksort(n)={Θ(1)if n=12⋅T(n2)+Θ(n)+0otherwise
Contrast with MergeSort: Tmergesort(n)={Θ(1)if n=12⋅T(n2)+Θ(1)+Θ(n)otherwise
Problem: Finding the median value of an unsorted collection is O(nlog(n))
(We'll talk about heaps later)
Idea: If we pick a value at random,
on average half the values will be lower.
What's the worst-case runtime?
What if we always pick the worst pivot?
[8, 7, 6, 5, 4, 3, 2, 1]
[7, 6, 5, 4, 3, 2, 1], 8, []
[6, 5, 4, 3, 2, 1], 7, [], 8
[5, 4, 3, 2, 1], 6, [], 7, 8
...
Tquicksort(n)∈O(n2)
Is the worst case runtime representative?
No! (it'll almost always be faster)
Is there something we can say about the runtime?
Let's say we pick the Xth largest element as pivot,
What's the recursive runtime for T(n)?
{T(0)+T(n−1)+Θ(n)if X=1T(1)+T(n−2)+Θ(n)if X=2T(2)+T(n−3)+Θ(n)if X=3..T(n−2)+T(1)+Θ(n)if X=n−1T(n−1)+T(0)+Θ(n)if X=n
How likely are we to pick X=k for any specific k?
P[X=k]=1n
... a brief aside...
If I roll d6 (a 6-sided die 🎲) k times,
what is the average over all possible outcomes?
If I roll d6 (a 6-sided die 🎲) 1 time...
Roll | Probability | Contribution |
---|---|---|
⚀ | 16 | 1 |
⚁ | 16 | 2 |
⚂ | 16 | 3 |
⚃ | 16 | 4 |
⚄ | 16 | 5 |
⚅ | 16 | 6 |
If X is a random variable representing the outcome of the roll, we call this the expectation of X, or E[X]
E[X]=∑iPi⋅Xi
If I roll d6 (a 6-sided die 🎲) 2 times...
Does the outcome of one roll affect the other?
No: Each roll is an independent event.
If X and Y are random variables representing the outcome of each roll (i.e., independent random variables), E[X+Y]=E[X]+E[Y]
=3.5+3.5=7