Title: Counting Sort
1Counting Sort
Non-comparison sort.
Precondition
n numbers in the range 1..k.
Key ideas
For each x count the number C(x) of elements x
Insert x at output position C(x) and decrement
C(x).
2An Example
1 2 3 4 5
6 7 8 9
10 11
Input
7
1
3
1
2
4
5
7
2
4
3
A1..n
k
for i 2 to 7 do Ci Ci Ci1
two 1s in A
6 elements 3
3Example (continued)
A1..n
3
B6 BC3 BCA11 A11 3
CA11 CA11 1
4Example (contd)
1 2 3 4 5
6 7 8 9
10 11
4
3
B
BCA10 A10 4
1 2 3 4 5
6 7
C
11
2
4
5
8
9
9
CA10 CA10 1
5Example (contd)
1 2 3 4 5
6 7 8 9
10 11
2
3
4
4
5
7
B
BCA6 A6 4
1 2 3 4 5
6 7
C
10
2
3
5
7
8
9
CA6 CA6 1
1 2 3 4 5
6 7
2
10
9
8
6
5
3
C
6Analysis of Counting Sort
Counting-Sort(A, B, k) for i 1 to k do
Ci 0 // ?(k) for j 1 to
lengthA do CAj CAj 1 //
?(n) // Ci now contains the number of elements
i for i 2 to k do Ci Ci
Ci1 // ?(k) // Ci now contains the number of
elements i for j lengthA downto
1 do BCAj Aj CAj CAj
1 // ?(n)
Running time is ?(nk) (or ?(n) if k O(n))!
7Stability
Counting sort is stable
Input order maintained among items with equal
keys.
Stability is important because
data are often carried with the keys being sorted.
radix sort (which uses counting sort as a
subroutine) relies on it to work correctly.
How is stability realized?
for j lengthA downto 1 do BCAj
Aj CAj CAj 1
8Radix Sort
Sort a set of numbers in multiple passes,
starting from the rightmost digit, then the 10s
digit, then the 100s digit, etc.
Example sort 23, 45, 7, 56, 20, 19, 88, 77, 61,
13, 52, 39, 80, 2, 99
99
77
13
39
80
2
7
88
23
45
56
20
19
61
52
Pass 2
88
23
56
7
19
80
61
77
99
20
52
2
45
39
13
9Analysis of Radix Sort
Correctness follows by induction on the number of
passes.
Sort n d-digit numbers. Let k be the range of
each digit.
Each pass takes time ?(nk). // use counting
sort There are d passes in total.
The running time for radix sort is ?(dndk).
Linear running time when d is a constant and k
O(n).
10Breaking into Digits
b bits
Key
b/r digits
r-bit digit
11Bucket Sort
Assumption input elements are uniformly
distributed over 0, 1)
In case the input range is larger than 1,
normalize each element over the maximum first.
n buckets are used for n input numbers.
Insert Ai into bucket ?nAi? , 1 ? i ? n .
Bucket sort runs in ?(n) expected time.
12An Example
n inputs dropped into n equal-sized subintervals
of 0, 1).
Step 2 Concatenate all lists
Step 1 equivalent of insertion sort
within each list
13Comparison of Sorting Methods (II)
Method Space Average
Max n16 n10000
Counting sort 2n 1000? 22n 10010
22n 10362 32010
Radix sort n ?(n200) 32n
32n 4838 4250
36838
D. E. Knuth, The Art of Computer Programming,
Vol 3, 2nd ed., p.382, 1998.
Implemented on the MIX Computer.
14Comparison of Sorting Algorithms
Insertion sort suitable only for small n.
Merge sort guaranteed to be fast even in
its worst case stable.
Heapsort requiring minimum memory and
guaranteed to run fast
average and maximum time both roughly twice
the average time
of quicksort.
Quicksort most useful general-purpose
sorting for very little memory
requirement and fastest average time. (choose the
median of three elements as
pivot in practice -)
Counting sort very useful when the keys have
small range stable
memory space for counters and for 2n records.
Radix sort appropriate for keys either
rather short or with a lexicographic
collating sequence.
Bucket sort assuming keys to have uniform
distribution.