Академический Документы
Профессиональный Документы
Культура Документы
6.046J/18.401J/SMA5503
Lecture 5
Prof. Erik Demaine
How fast can we sort?
All the sorting algorithms we have seen so far
are comparison sorts: only use comparisons to
determine the relative order of elements.
• E.g., insertion sort, merge sort, quicksort,
heapsort.
The best worst-case running time that we’ve
seen for comparison sorting is O(n lg n) .
Is O(n lg n) the best we can do?
Decision trees can help us answer this question.
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.2
Decision-tree example
Sort 〈a1, a2, …, an〉 1:2
1:2
2:3
2:3 1:3
1:3
123
123 1:3 213
213 2:3
1:3 2:3
132
132 312
312 231
231 321
321
A: 44 11 33 44 33 C:
B:
A: 44 11 33 44 33 C: 00 00 00 00
B:
for i ← 1 to k
do C[i] ← 0
A: 44 11 33 44 33 C: 00 00 00 11
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
A: 44 11 33 44 33 C: 11 00 00 11
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
A: 44 11 33 44 33 C: 11 00 11 11
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
A: 44 11 33 44 33 C: 11 00 11 22
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
A: 44 11 33 44 33 C: 11 00 22 22
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
A: 44 11 33 44 33 C: 11 00 22 22
B: C': 11 11 22 22
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
A: 44 11 33 44 33 C: 11 00 22 22
B: C': 11 11 33 22
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
A: 44 11 33 44 33 C: 11 00 22 22
B: C': 11 11 33 55
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
A: 44 11 33 44 33 C: 11 11 33 55
B: 33 C': 11 11 22 55
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.23
Loop 4
1 2 3 4 5 1 2 3 4
A: 44 11 33 44 33 C: 11 11 22 55
B: 33 44 C': 11 11 22 44
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.24
Loop 4
1 2 3 4 5 1 2 3 4
A: 44 11 33 44 33 C: 11 11 22 44
B: 33 33 44 C': 11 11 11 44
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.25
Loop 4
1 2 3 4 5 1 2 3 4
A: 44 11 33 44 33 C: 11 11 11 44
B: 11 33 33 44 C': 00 11 11 44
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.26
Loop 4
1 2 3 4 5 1 2 3 4
A: 44 11 33 44 33 C: 00 11 11 44
B: 11 33 33 44 44 C': 00 11 11 33
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.27
Analysis
for i ← 1 to k
Θ(k) do C[i] ← 0
for j ← 1 to n
Θ(n)
do C[A[ j]] ← C[A[ j]] + 1
for i ← 2 to k
Θ(k) do C[i] ← C[i] + C[i–1]
for j ← n downto 1
Θ(n) do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
Θ(n + k)
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.28
Running time
If k = O(n), then counting sort takes Θ(n) time.
• But, sorting takes Ω(n lg n) time!
• Where’s the fallacy?
Answer:
• Comparison sorting takes Ω(n lg n) time.
• Counting sort is not a comparison sort.
• In fact, not a single comparison between
elements occurs!
© 2001 by Charles E. Leiserson Introduction to Algorithms Day 8 L5.29
Stable sorting
Counting sort is a stable sort: it preserves
the input order among equal elements.
A: 44 11 33 44 33
B: 11 33 33 44 44
Replica of punch
card from the
1900 U.S. census:
[Howells 2000]
system
•Pantograph card
punch
•Hand-press reader
•Dial counters
•Sorting box