Professional Documents
Culture Documents
Count Radix Bucket PDF
Count Radix Bucket PDF
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
Counting sort Counting sort example (1)
Counting-Sort(A, B, k) 1 2 3 4 5 6 7 8
1. for i 0 to k A[1..n] is the input array n=8
B [1..n] is the output array A= 2 5 3 0 2 3 0 3 k=6
2. do C[i] 0
3. for j 1 to length[A] C [0..k] is a counting array 0 1 2 3 4 5
C[A[j]] C[A[j]] +1
4. do C[A[j]] C[A[j]] +1 C= 2 0 2 3 0 1 after line 4
5. /* now C contains the number of elements equal to i 0 1 2 3 4 5
C[i] C[i] + C[i –1]
6. for i 1 to k C= 2 2 4 7 7 8
7. do C[i] C[i] + C[i –1]
after line 7
1 2 3 4 5 6 7 8
8. /* now C contains the number of elements to i 3
B= B[C[A[j]]] A[j]
9. for j length[A] downto 1 C[A[j]] C[A[j]] – 1
0 1 2 3 4 5
10. do B[C[A[j]]] A[j] /* place element after line 11
C= 2 2 4 76 7 8
11. C[A[j]] C[A[j]] – 1 /* reduce by one
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
B= 0 3 B= 0 3 3
0 1 2 3 4 5 0 1 2 3 4 5
C= 21 2 4 6 7 8 C= 1 2 4 65 7 8
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
the algorithm is optimal!! For example, we can use Counting-Sort for each
call, with k = 9. In general, k << n, so k = O(n).
• This does not work if we do not assume k = O(n). At the end, the numbers will be sorted!!
Wasteful if k >> n and is not sorting in place.
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
Radix sort: example Radix-Sort
Radix-Sort(A, d)
329 720 720 329 1. for i 1 to d
457 355 329 355 2. do use a stable sort to sort array A on digit d
657 436 436 436 Notes:
839 457 839 457 • Complexity: T(n) = (d(n+k)) (n) for
All the digits that have been sorted are the same. Sort will take ((b/r)(n+2r))
By induction, x and y remain in the same order they • Take d = b/r digits of r bits each in the range
appeared before the ith iteration, and snde the ith iteration [0..2r–1], so we can use Counting-Sort with
is stable, they will remain so after the additional iteration. k = 2r –1. Each pass of Counting-Sort takes (n+k)
This completes the proof! so we get (n+2r) and there are d passes, so the
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
Bucket sort Bucket sort: example
Input: n real numbers in the interval [0..1) uniformly .78 0 .12
distributed (numbers have equal probability) .17 1 .12 .17 .17
The idea: Divide the interval [0..1) into n buckets .39 2 .21 .23 .26 .21
0, 1/n, 2/n. … (n–1)/n. Put each element ai into its .26 3 .39 .23
matching bucket 1/i ai 1/(i+1). Since the
.72 4 .26
numbers are uniformly distributed, not too many .94 5 .39
elements will be placed in each bucket. If we insert
. 21 6 .68 .68
them in order (using Insertion-Sort), the buckets
and the elements in them will always be in sorted .12 7 .72 .78 .72
order. .23 8 .78
.68 9 .94 .94
Data Structures, Spring 2004 © L. Joskowicz Data Structures, Spring 2004 © L. Joskowicz
Summary
With additional assumptions, we can sort n
elements in optimal time and space (n).