Theoretical Bounds on Sorting
1
Lecture 35 (Sorting 4)
CS61B, Fall 2025 @ UC Berkeley
Slides credit: Josh Hug
Sorting
Sorting is a foundational problem.
Today we’ll discuss the fundamental nature of the sorting problem itself: How hard is it to sort?
Sorts Summary
| Memory | # Compares | Notes | Stable? |
Heapsort | Θ(1) | Θ(N log N) | Bad caching (61C) | No |
Insertion | Θ(1) | Θ(N2) | Best for almost sorted and N < 15 | Yes |
Mergesort | Θ(N) | Θ(N log N) | Fastest stable sort | Yes |
Quicksort LTHS | Θ(log N) | Θ(N log N) expected | Fastest sort | No |
You can create a stable Quicksort. However, using unstable partitioning schemes (like Hoare partitioning) and using randomness to avoid bad pivots tend to yield better runtimes.
This is due to the cost of tracking recursive calls by the computer, and is also an “expected” amount. The difference between log N and constant memory is trivial.
Math Problem Warmup
Lecture 35, CS61B, Fall 2025
Math Problem Warmup
Theoretical Bounds on Sorting
A Math Problem out of Nowhere
Consider the functions N! and (N/2)N/2
Is N! ∈ Ω((N/2)N/2)? Prove your answer.
A Math Problem out of Nowhere
Consider the functions N! and (N/2)N/2
Is N! ∈ Ω((N/2)N/2)? Prove your answer.
10!
55
N! > (N/2)N/2, for large N, therefore N! ∈ Ω((N/2)N/2)
Another Math Problem
Given: N! > (N/2)N/2, which we used to prove our answer to the previous problem.
Show that log(N!) ∈ Ω(N log N).
Another Math Problem
Given that N! > (N/2)N/2
Show that log(N!) ∈ Ω(N log N).
We have that N! > (N/2)N/2
Since log(N/2) is the same thing asymptotically as log(N).
In other words, log(N!) grows at least as quickly as N log N.
Last Math Problem
In the previous problem, we showed that log(N!) ∈ Ω(N log N).
Now show that N log N ∈ Ω(log(N!)).
Last Math Problem
Show that N log N ∈ Ω(log(N!))
Proof:
Omega and Theta: yellkey.com/purpose
Given:
Which of the following can we say?
Omega and Theta
Given:
Which of the following can we say?
Informally: N log N ≥ log(N!)
Informally: N log N = log(N!)
Informally: log(N!) ≥ N log N
Summary
We’ve shown that log(N!) ∈ Θ(N log N).
As for why we did this, we will see in a little while...
Simple Bounds for TUCS (the ultimate comparison sort)
Lecture 35, CS61B, Fall 2025
Math Problem Warmup
Theoretical Bounds on Sorting
Sorting
We have shown several sorts to require Θ(N log N) worst case time.
Let the ultimate comparison sort (TUCS) be the asymptotically fastest possible comparison sorting algorithm, possibly yet to be discovered, and let R(N) be its worst case runtime.
Give the best Ω and O bounds you can for R(N).
It might seem strange to give Ω and O bounds for an algorithm whose details are completely unknown, but you can, I promise!
By comparison sort, I mean that it uses e.g. the compareTo method in Java to make decisions.
Sorting
We have shown several sorts to require Θ(N log N) worst case time.
Let the ultimate comparison sort (TUCS) be the asymptotically fastest possible comparison sorting algorithm, possibly yet to be discovered, and let R(N) be its worst case runtime.
O(N log N)
Ω(1)
TUCS Worst
Case Θ Runtime
By comparison sort, I mean that it uses e.g. the compareTo method in Java to make decisions.
Sorting
Let TUCS be the asymptotically fastest possible comparison sorting algorithm, possibly yet to be discovered.
O(N log N)
Ω(N)
TUCS Worst
Case Θ Runtime
Sorting
We know that TUCS “lives” between N and N log N.
O(N log N)
Ω(N)
TUCS Worst
Case Θ Runtime
Puppy Cat Dog (N = 3, N = 4)
Lecture 35, CS61B, Fall 2025
Math Problem Warmup
Theoretical Bounds on Sorting
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
a < b | b < c | | Which is which? |
Yes | Yes | | |
| | | |
| | | |
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
a < b | b < c | | Which is which? |
Yes | Yes | | a: puppy, b: cat, c: dog (sorted order: abc) |
No | No | | |
| | | |
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
a < b | b < c | | Which is which? |
Yes | Yes | | a: puppy, b: cat, c: dog (sorted order: abc) |
No | No | | c: puppy, b: cat, a: dog (sorted order: cba) |
| | | |
The Game of Puppy, Cat, Dog: http://yellkey.com/surface
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
Which is which?
a < b | b < c | | Which is which? |
Yes | Yes | | a: puppy, b: cat, c: dog (sorted order: abc) |
No | No | | c: puppy, b: cat, a: dog (sorted order: cba) |
Yes | No | | |
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
Which is which? How do we resolve the ambiguity?
a < b | b < c | | Which is which? |
Yes | Yes | | a: puppy, b: cat, c: dog (sorted order: abc) |
No | No | | c: puppy, b: cat, a: dog (sorted order: cba) |
Yes | No | | |
a? c? b
c? a? b
The Game of Puppy, Cat, Dog
Suppose we have a puppy, a cat, and a dog, each in an opaque soundproof box labeled A, B, and C. We want to figure out which is which using a scale.
Which is which? How do we resolve the ambiguity? Ask if a < c.
a < b | b < c | a < c? | Which is which? |
Yes | Yes | N/A | a: puppy, b: cat, c: dog (sorted order: abc) |
No | No | N/A | c: puppy, b: cat, a: dog (sorted order: cba) |
Yes | No | Yes | a: puppy, c: cat, b: dog (sorted order: acb) |
Puppy, Cat, Dog - A Graphical Picture for N = 3
The full decision tree for puppy, cat, dog:
Is a < b?
Is b < c?
No
Yes
Yes
No
Is a < c?
a c b
Yes
c a b
No
Is a < c?
Is b < c?
b a c
b c a
c b a
Yes
No
No
Yes
a b c
a: puppy
b: cat
c: dog
a: puppy
c: cat
b: dog
c: puppy
a: cat
b: dog
The Game of Puppy, Cat, Dog, yellkey.com/worker
How many questions would you need to ask to definitely solve the “puppy, cat, dog, walrus” problem?
The Game of Puppy, Cat, Dog
How many questions would you need to ask to definitely solve the “puppy, cat, dog, walrus” problem?
Proof:
Puppy Cat Dog for Any N
Lecture 35, CS61B, Fall 2025
Arrays.sort in Java
Math Problem Warmup
Theoretical Bounds on Sorting
Sound of Sorting
Generalizing Puppy, Cat, Dog
How many questions would you need to ask to definitely solve the generalized “puppy, cat, dog” problem for N items?
Hint: For N=4, we said the answer was 5 based on the following argument:
Generalizing Puppy, Cat, Dog
How many questions would you need to ask to definitely solve the generalized “puppy, cat, dog” problem for N items?
Answer: Ω(log(N!))
Hint: For N, we have the following argument:
Generalizing Puppy, Cat, Dog
Finding an optimal decision tree for the generalized version of puppy, cat, dog (e.g. N=6: puppy, cat, dog, monkey, walrus, elephant) is an open problem in mathematics.
Deriving a sequence of yes/no questions to identify puppy, cat, dog is hard. An alternate approach to solving the puppy, cat, dog problem:
Sorting, Puppies, Cats, and Dogs
Why do we care about these (no doubt adorable) critters?
A solution to the sorting problem also provides a solution to puppy, cat, dog.
Physics analogy: Climbing a hill with your legs (CAHWYL) is one way to solve the problem of getting up a hill (GUAH).
The Sorting Lower Bound
Lecture 35, CS61B, Fall 2025
Math Problem Warmup
Theoretical Bounds on Sorting
Sorting Lower Bound
We have a lower bound on puppy, cat, dog, namely that it takes Ω(log(N!)) comparisons to solve such a puzzle in the worst case.
Since sorting with comparisons can be used to solve puppy, cat, dog, then sorting also takes Ω(log(N!)) comparisons in the worst case.
Or in other words:
O(N log N)
Ω(log(N!))
TUCS Worst
Case Θ Runtime
Another Math Problem
Earlier, we showed that log(N!) ∈ Ω(N log N) using the proof below.
Proof from earlier that log(N!) ∈ Ω(N log N):
Recall that changing base is just multiplying by a constant.
The Sorting Lower Bound (Finally)
Since TUCS is Ω(lg N!) and lg N! is Ω(N log N), we have that TUCS is Ω(N log N).
Any comparison based sort requires at least order N log N comparisons in its worst case.
O(N log N)
Ω(N log N)
TUCS Worst
Case Θ Runtime
The Sorting Lower Bound (Finally)
Since TUCS is Ω(lg N!) and lg N! is Ω(N log N), we have that TUCS is Ω(N log N).
Any comparison based sort requires at least order N log N comparisons in its worst case.
Proof summary:
Informally: TUCS ≥ puppy, cat, dog ≥ log N! ≥ N log N
O(N log N)
Ω(N log N)
TUCS Worst
Case Θ Runtime
Optimality
The punchline:
| Memory | # Compares | Notes | Stable? |
Heapsort | Θ(1) | Θ(N log N) | Bad caching (61C) | No |
Insertion | Θ(1) | Θ(N2) | Best for almost sorted and N < 15 | Yes |
Mergesort | Θ(N) | Θ(N log N) | Fastest stable sort | Yes |
Quicksort LTHS | Θ(log N) | Θ(N log N) expected | Fastest sort | No |
Next Time...
Today we proved that any sort that uses comparisons has runtime Ω(N log N).
Next time we’ll discuss how we can sort in Θ(N) time.
Citations
Title image: http://www.angelfire.com/blog/ronz/Articles/999SortingNetworksReferen.html
Cubes: http://www.clker.com/cliparts/6/9/3/2/1197122947130754155jean_victor_balin_Cubes.svg.hi.png
Scale: http://www.clipartbest.com/cliparts/94T/bAe/94TbAejig.png
Cat: http://animalia-life.com/cat.html
Dog: http://animalia-life.com/dogs.html
Attendance: www.yellkey.com/half