Professional Documents
Culture Documents
Question
Can we do better?
Question
Can we do better?
Goal
We will prove that any comparison-based sorting algorithm has a
worst-case running time Ω(n log n).
Proof.
A decision tree to sort n elements must have at least n!
leaves, since each of the n! orderings is a possible answer.
Proof.
A decision tree to sort n elements must have at least n!
leaves, since each of the n! orderings is a possible answer.
A binary tree of height h has at most 2h leaves
Proof.
A decision tree to sort n elements must have at least n!
leaves, since each of the n! orderings is a possible answer.
A binary tree of height h has at most 2h leaves
Thus, n! ≤ 2h
⇒ h ≥ log n! = Ω(n log n) (Stirling’s approximation)
Proof.
A decision tree to sort n elements must have at least n!
leaves, since each of the n! orderings is a possible answer.
A binary tree of height h has at most 2h leaves
Thus, n! ≤ 2h
⇒ h ≥ log n! = Ω(n log n) (Stirling’s approximation)
Corollary
Heapsort and merge sort are asymptotically optimal
comparison-based sorting algorithms.
Sorting: Lower Bounds and Linear Time Last Revision: September
9 /8,36201
Lower Bound for Average Running Time of Sorting
Theorem
When all input permutations are equally likely, any
comparison-based sorting algorithm requires Ω(n log n)
comparisons on average.
Proof.
The External Path Length (EPL) of a tree is the sum over all leaves
of the tree, of the length of the paths from the root to the leaves.
Proof.
The External Path Length (EPL) of a tree is the sum over all leaves
of the tree, of the length of the paths from the root to the leaves.
Average number of comparisons used by a sorting algorithm is EPL
of its associated comparison tree divided by n!.
Proof.
The External Path Length (EPL) of a tree is the sum over all leaves
of the tree, of the length of the paths from the root to the leaves.
Average number of comparisons used by a sorting algorithm is EPL
of its associated comparison tree divided by n!.
The EPL of a binary tree with m leaves is at least m log2 m + O(m).
The comparison tree has m = n! leaves
⇒ its external path length is n! log2 (n!) + O(n!)
⇒ average number of comparisons used is log2 (n!) + O(1).
Proof.
The External Path Length (EPL) of a tree is the sum over all leaves
of the tree, of the length of the paths from the root to the leaves.
Average number of comparisons used by a sorting algorithm is EPL
of its associated comparison tree divided by n!.
The EPL of a binary tree with m leaves is at least m log2 m + O(m).
The comparison tree has m = n! leaves
⇒ its external path length is n! log2 (n!) + O(n!)
⇒ average number of comparisons used is log2 (n!) + O(1).
We already saw log2 (n!) = Ω(n log n).
Counting sort
Assumes items are in set {1, 2, . . . , k}.
Is a stable sort (defined soon).
Counting sort
Assumes items are in set {1, 2, . . . , k}.
Is a stable sort (defined soon).
Radix sort
Assumes items are stored in fixed size words using finite
alphabet
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 0 0 0 0
for i ← 1 to k do
C[i] ← 0;
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 0 0 0 1
for j ← 1 to n do
C[A[j]] ← C[A[j]] + 1; // C[i] = |{key = i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 0 1 0 1
for j ← 1 to n do
C[A[j]] ← C[A[j]] + 1; // C[i] = |{key = i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 1 0 1
for j ← 1 to n do
C[A[j]] ← C[A[j]] + 1; // C[i] = |{key = i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 1 0 2
for j ← 1 to n do
C[A[j]] ← C[A[j]] + 1; // C[i] = |{key = i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 2 0 2
for j ← 1 to n do
C[A[j]] ← C[A[j]] + 1; // C[i] = |{key = i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 2 0 2
B C0 1 3 0 2
for i ← 2 to k do
C[i] ← C[i] + C[i − 1]; // C[i] = |{key ≤ i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 2 0 2
B C0 1 3 3 2
for i ← 2 to k do
C[i] ← C[i] + C[i − 1]; // C[i] = |{key ≤ i}|
end
1 2 3 4 5 1 2 3 4
A 4 2 1 4 2 C 1 2 0 2
B C0 1 3 3 5
for i ← 2 to k do
C[i] ← C[i] + C[i − 1]; // C[i] = |{key ≤ i}|
end
B 2 C0 1 2 3 5
for j ← n to 1 do
B[C[A[j]]] ← A[j];
C[A[j]] ← C[A[j]] − 1;
end
B 2 4 C0 1 2 3 4
for j ← n to 1 do
B[C[A[j]]] ← A[j];
C[A[j]] ← C[A[j]] − 1;
end
B 1 2 4 C0 0 2 3 4
for j ← n to 1 do
B[C[A[j]]] ← A[j];
C[A[j]] ← C[A[j]] − 1;
end
B 1 2 2 4 C0 0 1 3 4
for j ← n to 1 do
B[C[A[j]]] ← A[j];
C[A[j]] ← C[A[j]] − 1;
end
B 1 2 2 4 4 C0 0 1 3 3
for j ← n to 1 do
B[C[A[j]]] ← A[j];
C[A[j]] ← C[A[j]] − 1;
end
Total: O(n + k)
Sorting: Lower Bounds and Linear Time Last Revision: September
29 /8,36201
Running Time
But didn’t we prove that sorting must take Ω(n log n) time?
But didn’t we prove that sorting must take Ω(n log n) time?
But didn’t we prove that sorting must take Ω(n log n) time?
But didn’t we prove that sorting must take Ω(n log n) time?
4 2 1 4 2
1 2 2 4 4
4 2 1 4 2
1 2 2 4 4
Exercise
What other sorts have this property?
2 32 9 272 0 272 0 2 32 9 2 32 9
5 45 7 535 5 232 9 5 35 5 2 72 0
3 65 7 343 6 343 6 3 43 6 3 43 6
5 83 9 545 7 583 9 5 45 7 3 65 7
3 43 6 365 7 535 5 3 65 7 5 35 5
2 72 0 232 9 545 7 2 72 0 5 45 7
5 35 5 583 9 365 7 5 83 9 5 83 9
Sorting: Lower Bounds and Linear Time Last Revision: September
32 /8,36201
Radix Sort: Correctness
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Application:
Sorting numbers in the range from 0 to nb − 1, where b is a
constant
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Application:
Sorting numbers in the range from 0 to nb − 1, where b is a
constant
b log n bits for each number
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Application:
Sorting numbers in the range from 0 to nb − 1, where b is a
constant
b log n bits for each number
each number can be viewed as having O(b) digits of log n bits
each
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Application:
Sorting numbers in the range from 0 to nb − 1, where b is a
constant
b log n bits for each number
each number can be viewed as having O(b) digits of log n bits
each
running time is O(d(n + k)) = O(b(n + 2log n )) = O(bn)
Lemma
Given n d-digit numbers in which each digit can take on up to k
possible values, radix sort correctly sorts these numbers in
O(d(n + k)) time if the stable sort it uses takes O(n + k) time.
Application:
Sorting numbers in the range from 0 to nb − 1, where b is a
constant
b log n bits for each number
each number can be viewed as having O(b) digits of log n bits
each
running time is O(d(n + k)) = O(b(n + 2log n )) = O(bn)
since b is a constant, the running time is O(n).