Ch5
Ch5
6.046J/18.401J
LECTURE 5
Sorting Lower Bounds
• Decision trees
Linear-Time Sorting
• Counting sort
• Radix sort
Appendix: Punched cards
2:3 1:3
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.3
Decision-tree example
Sort 〈a1, a2, a3〉 1:2 9≥4
= 〈 9, 4, 6 〉:
2:3 1:3
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.4
Decision-tree example
Sort 〈a1, a2, a3〉 1:2
= 〈 9, 4, 6 〉:
2:3 1:3 9≥6
123 1:3 213 2:3
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.5
Decision-tree example
Sort 〈a1, a2, a3〉 1:2
= 〈 9, 4, 6 〉:
2:3 1:3
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.6
Decision-tree example
Sort 〈a1, a2, a3〉 1:2
= 〈 9, 4, 6 〉:
2:3 1:3
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.10
Sorting in linear time
Counting sort: No comparisons between elements.
• Input: A[1 . . n], where A[ j]∈{1, 2, …, k} .
• Output: B[1 . . n], sorted.
• Auxiliary storage: C[1 . . k] .
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.11
Counting sort
for i ← 1 to k
do C[i] ← 0
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.12
Counting-sort example
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C:
B:
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.13
Loop 1
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 0 0 0
B:
for i ← 1 to k
do C[i] ← 0
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.14
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 0 0 1
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.15
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 0 1
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.16
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 1 1
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.17
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 1 2
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.18
Loop 2
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B:
for j ← 1 to n
do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.19
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 2 2
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.20
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 3 2
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.21
Loop 3
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 0 2 2
B: C': 1 1 3 5
for i ← 2 to k
do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.22
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 3 5
B: 3 C': 1 1 2 5
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.23
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 2 5
B: 3 4 C': 1 1 2 4
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.24
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 2 4
B: 3 3 4 C': 1 1 1 4
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.25
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 1 1 1 4
B: 1 3 3 4 C': 0 1 1 4
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.26
Loop 4
1 2 3 4 5 1 2 3 4
A: 4 1 3 4 3 C: 0 1 1 4
B: 1 3 3 4 4 C': 0 1 1 3
for j ← n downto 1
do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 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)
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson 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!
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.29
Stable sorting
Counting sort is a stable sort: it preserves
the input order among equal elements.
A: 4 1 3 4 3
B: 1 3 3 4 4
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.30
Radix sort
• Origin: Herman Hollerith’s card-sorting
machine for the 1890 U.S. Census. (See
Appendix .)
• Digit-by-digit sort.
• Hollerith’s original (bad) idea: sort on
most-significant digit first.
• Good idea: Sort on least-significant digit
first with auxiliary stable sort.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.31
Operation of radix sort
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.32
Correctness of radix sort
Induction on digit position
720 329
• Assume that the numbers
are sorted by their low-order 329 355
t – 1 digits. 436 436
839 457
• Sort on digit t
355 657
457 720
657 839
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.33
Correctness of radix sort
Induction on digit position
720 329
• Assume that the numbers
are sorted by their low-order 329 355
t – 1 digits. 436 436
839 457
• Sort on digit t
Two numbers that differ in 355 657
digit t are correctly sorted. 457 720
657 839
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.34
Correctness of radix sort
Induction on digit position
720 329
• Assume that the numbers
are sorted by their low-order 329 355
t – 1 digits. 436 436
839 457
• Sort on digit t
Two numbers that differ in 355 657
digit t are correctly sorted. 457 720
Two numbers equal in digit t 657 839
are put in the same order as
the input ⇒ correct order.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.35
Analysis of radix sort
• Assume counting sort is the auxiliary stable sort.
• Sort n computer words of b bits each.
• Each word can be viewed as having b/r base-2r
digits. 8 8 8 8
Example: 32-bit word
r = 8 ⇒ b/r = 4 passes of counting sort on
base-28 digits; or r = 16 ⇒ b/r = 2 passes of
counting sort on base-216 digits.
How many passes should we make?
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.36
Analysis (continued)
Recall: Counting sort takes Θ(n + k) time to
sort n numbers in the range from 0 to k – 1.
If each b-bit word is broken into r-bit pieces,
each pass of counting sort takes Θ(n + 2r) time.
Since there are b/r passes, we have
T (n, b) = Θ b (n + 2 r ) .
r
Choose r to minimize T(n, b):
• Increasing r means fewer passes, but as
r >> lg n, the time grows exponentially.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.37
Choosing r
T (n, b) = Θ b (n + 2 r )
r
Minimize T(n, b) by differentiating and setting to 0.
Or, just observe that we don’t want 2r >> n, and
there’s no harm asymptotically in choosing r as
large as possible subject to this constraint.
Choosing r = lg n implies T(n, b) = Θ(b n/lg n) .
• For numbers in the range from 0 to n d – 1, we
have b = d lg n ⇒ radix sort runs in Θ(d n) time.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.38
Conclusions
In practice, radix sort is fast for large inputs, as
well as simple to code and maintain.
Example (32-bit numbers):
• At most 3 passes when sorting ≥ 2000 numbers.
• Merge sort and quicksort do at least lg 2000 =
11 passes.
Downside: Unlike quicksort, radix sort displays
little locality of reference, and thus a well-tuned
quicksort fares better on modern processors,
which feature steep memory hierarchies.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.39
Appendix: Punched-card
technology
• Herman Hollerith (1860-1929)
• Punched cards
• Hollerith’s tabulating system
• Operation of the sorter
• Origin of radix sort
• “Modern” IBM card
• Web resources on punched-card Return to last
technology slide viewed.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.40
Herman Hollerith
(1860-1929)
• The 1880 U.S. Census took almost
10 years to process.
• While a lecturer at MIT, Hollerith
prototyped punched-card technology.
• His machines, including a “card sorter,” allowed
the 1890 census total to be reported in 6 weeks.
• He founded the Tabulating Machine Company in
1911, which merged with other companies in 1924
to form International Business Machines.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.41
Punched cards
• Punched card = data record.
• Hole = value.
• Algorithm = machine + human operator.
Replica of punch
card from the
1900 U.S. census.
[Howells 2000]
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.42
Hollerith’s
Figure from
tabulating [Howells 2000].
system
•Pantograph card
punch
•Hand-press reader
•Dial counters
•Sorting box
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.43
Operation of the sorter
• An operator inserts a card into
the press.
• Pins on the press reach through
the punched holes to make
electrical contact with mercury-
filled cups beneath the card.
• Whenever a particular digit
value is punched, the lid of the
corresponding sorting bin lifts.
• The operator deposits the card Hollerith Tabulator, Pantograph, Press, and Sorter
into the bin and closes the lid.
• When all cards have been processed, the front panel is opened, and
the cards are collected in order, yielding one pass of a stable sort.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.44
Origin of radix sort
Hollerith’s original 1889 patent alludes to a most-
significant-digit-first radix sort:
“The most complicated combinations can readily be
counted with comparatively few counters or relays by first
assorting the cards according to the first items entering
into the combinations, then reassorting each group
according to the second item entering into the combination,
and so on, and finally counting on a few counters the last
item of the combination for each group of cards.”
Least-significant-digit-first radix sort seems to be
a folk invention originated by machine operators.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.45
“Modern” IBM card
• One character per column.
Produced by
the WWW
Virtual Punch-
Card Server.
September 26, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.47