5 - Bucket Sort and Counting Sort-2
5 - Bucket Sort and Counting Sort-2
Counting Sort
• Assumptions:
– Sort n integers which are in the range [0 ... r]
– r is in the order of n, that is, r=O(n)
• Idea:
– For each element x, find the number of elements x
– Place x into its correct position in the output array
output array
2
Step 1
(i.e., frequencies) 3
Step 2
4
Algorithm
• Start from the last element of A
• Place A[i] at its correct place in the output array
• Decrease C[A[i]] by one
5
Example
1 2 3 4 5 6 7 8 0 1 2 3 4 5
A 2 5 3 0 2 3 0 3 C 2 2 4 7 7 8
0 1 2 3 4 5
(cumulative sums)
C 2 0 2 3 0 1
(frequencies)
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 3 B 0 3
0 1 2 3 4 5 0 1 2 3 4 5
C 2 2 4 6 7 8 C 1 2 4 6 7 8
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 3 3 B 0 2 3 3
0 1 2 3 4 5 0 1 2 3 4 5
C 1 2 4 5 7 8 C 1 2 3 5 7 8
6
Example (cont.)
1 2 3 4 5 6 7 8
A 2 5 3 0 2 3 0 3
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 0 2 3 3 B 0 0 2 3 3 3 5
0 1 2 3 4 5 0 1 2 3 4 5
C 0 2 3 5 7 8 C 0 2 3 4 7 7
1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
B 0 0 2 3 3 3 B 0 0 2 2 3 3 3 5
0 1 2 3 4 5
C 0 2 3 4 7 8
7
COUNTING-SORT
1 j n
A
Alg.: COUNTING-SORT(A, B, n, k) 0 k
1. for i ← 0 to r C
2. do C[ i ] ← 0 B
1 n
3. for j ← 1 to n
4. do C[A[ j ]] ← C[A[ j ]] + 1
5. C[i] contains the number of elements
equal to i
6. for i ← 1 to r
7. do C[ i ] ← C[ i ] + C[i -1]
8. C[i] contains the number of elements ≤ i
9. for j ← n downto 1
10. do B[C[A[ j ]]] ← A[ j ]
11. C[A[ j ]] ← C[A[ j ]] - 1 8
Analysis of Counting Sort
Alg.: COUNTING-SORT(A, B, n, k)
1. for i ← 0 to r (r)
2. do C[ i ] ← 0
3. for j ← 1 to n
(n)
4. do C[A[ j ]] ← C[A[ j ]] + 1
5. C[i] contains the number of elements equal to i
6. for i ← 1 to r
(r)
7. do C[ i ] ← C[ i ] + C[i -1]
8. C[i] contains the number of elements ≤ i
9. for j ← n downto 1
10. do B[C[A[ j ]]] ← A[ j ] (n)
11. C[A[ j ]] ← C[A[ j ]] - 1
Overall time: (n + r) 9
Analysis of Counting Sort
10
Bucket Sort
• Assumption:
– the input is generated by a random process that distributes
elements uniformly over [0, 1)
• Idea:
– Divide [0, 1) into n equal-sized buckets
– Distribute the n input values into the buckets
– Sort each bucket (e.g., using quicksort)
– Go through the buckets in order, listing elements in each one
5 .72 4 /
6 .94 5 /
7 .21 6 .68 /
9 .23 8 /
10 .68 9 .94 /
12
Example - Bucket Sort
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /
0 /
1 .12 .17 /
3 .39 /
4 /
5 /
6 .68 /
7 .72 .78 /
Concatenate the lists from
8 / 0 to n – 1 together, in order
9 .94 / 13
Correctness of Bucket Sort
• Consider two elements A[i], A[ j]
• Assume without loss of generality that A[i] ≤ A[j]
• Then nA[i] ≤ nA[j]
– A[i] belongs to the same bucket as A[j] or to a bucket
with a lower index than that of A[j]
• If A[i], A[j] belong to the same bucket:
– sorting puts them in the proper order
• If A[i], A[j] are put in different buckets:
– concatenation of the lists puts them in the proper order
14
Analysis of Bucket Sort
Alg.: BUCKET-SORT(A, n)
for i ← 1 to n O(n)
for i ← 0 to n - 1
(n)
do sort list B[i] with quicksort sort