Lecture 6
Lecture 6
Lecture 6
6
Sorting lower bounds on O(n)-time sorting
Sorting
• We’ve seen a few O(n log(n))-time algorithms.
• MERGESORT has worst-case running time O(nlog(n))
• QUICKSORT has expected running time O(nlog(n))
Can we do better?
Depends on who
you ask…
An O(1)-time algorithm for sorting:
StickSort
• Problem: sort these n sticks by length.
• Now they
are sorted
this way.
• Algorithm:
• Drop them on a table.
That may have been unsatisfying
• But StickSort does raise some important questions:
• What is our model of computation?
• Input: array
• Output: sorted array
• Operations allowed: comparisons
-vs-
• Input: sticks
• Output: sorted sticks in vertical order
• Operations allowed: dropping on tables
Is bigger than ?
5 etc.
Lower bound of Ω(n log(n)).
• Theorem:
• Any deterministic comparison-based sorting algorithm must
take Ω(n log(n)) steps.
• Any randomized comparison-based sorting algorithm must
take Ω(n log(n)) steps in expectation.
etc…
≤ ?
YES NO
≤ ?
YES NO
All comparison-based algorithms look like this
Pivot!
≤ ?
NO Example: Sort these
YES
three things using
QuickSort.
L R L R
etc... ≤ ?
YES NO Pivot!
L L R Now
R
Then we’re done Return
(after some base-
≤ ? recurse
on R
YES NO
case stuff)
L R L R
Return Return
In either case, we’re done
(after some base case stuff and
returning recursive calls).
All comparison-based algorithms
have an associated decision tree.
The leaves of this ? What does the decision
tree for MERGESORTING
tree are all possible YES NO four elements look like?
orderings of the
items: when we ? ?
reach a leaf we
YES NO YES NO
return it.
Ollie the
? ? ? ? over-achieving ostrich
? ?
YES NO YES NO
? ? ? ?
What’s the worst-case runtime?
At least Ω(length of the longest path).
?
YES NO
? ?
YES NO YES NO
? ? ? ?
being sloppy about
floors and ceilings!
• Proof:
• Any deterministic comparison-based algorithm can be
represented as a decision tree with n! leaves.
• The worst-case running time is at least the depth of the decision
tree.
• All decision trees with n! leaves have depth Ω(n log(n)).
• Proof:
• see lecture notes
• (same ideas as deterministic case) Try to prove this
yourself!
9 6 3 5 2 1 2
instead of
Pre-lecture exercise
• Sorting CS161 students by their month of birth.
• [Discussion on board]
1 1 4 5
Another model of computation
• The items you are sorting have meaningful values.
9 6 3 5 2 1 2
instead of
Implement the buckets as linked
Why might this help? lists. They are first-in, first-out.
This will be useful later.
BucketSort: 9 6 3 5 2 1 2
Note: this is a simplification of
what CLRS calls “BucketSort”
2
1 2 3 5 6 9
1 2 3 4 5 6 7 8 9
Concatenate SORTED!
the buckets!
In time O(n).
Issues
• Need to be able to know what bucket to put something in.
• That’s okay for now: it’s part of the model.
• Need to know what values might show up ahead of time.
345
234
101
50 21 13
0 1 2 3 4 5 6 7 8 9
1
345
234
101 13 21 50
0 1 2 3 4 5 6 7 8 9
50
21
13
234
345
1 101
0 1 2 3 4 5 6 7 8 9
101 1
01 13 21 234 345 50
Next array is sorted by all three digits.
1
001 13
013 21
021 50
050 101 234 345
Sorted array
Or at least a
little formally!
Formally…
Lucky the lackadaisical lemur
• Argue by induction.
• Inductive hypothesis:
Why does this work?
Original array:
101 1
01 13 21 234 345 50
Next array is sorted by all three digits.
1
001 13
013 21
021 50
050 101 234 345
Sorted array
Or at least a
little formally!
Formally…
Lucky the lackadaisical lemur
• Argue by induction.
• Inductive hypothesis:
• After the k’th iteration, the array is sorted by the first k
least-significant digits.
• Base case:
• “Sorted by 0 least-significant digits” means not sorted.
• Inductive step: This needs to use: (1) bucket sort
works, and (2) we treat each bucket
• (See lecture notes or CLRS) as a FIFO queue.*
Can we do better?
what if M = n?
Trade-offs…
• RadixSort works with any base.
• Before we did it base r=10.
• But we could do it base r=2 or r=20 just as easily.
• [On board]
Choosing r = n
is pretty good.
What’s the optimal
• If we choose r = n, running time is choice of r?
𝑻 𝒏 = 𝑶 𝒏 ⋅ 𝐥𝐨𝐠 𝐧 𝑴
• If M = O(n), T(n) = O(n). Awesome!
• If M = Ω(nn), T(n) = O(n2)…
9 6 3 5 2 1 2