L14 Linearsort RadixBucket
L14 Linearsort RadixBucket
6
Analysis of Radix Sort
• Given n d-digit numbers, where each digit may take up to k
possible values, RADIX-SORT correctly sorts the numbers in
Θ(d n + k ) if the stable sort it uses takes Θ(𝑛 + 𝑘) time
Proof
• One pass of sorting per digit takes Θ(𝑛 + 𝑘) assuming that
we use counting sort
• There are d passes (for each digit)
Θ(d n + k )
• Assuming d=O(1) and k=O(n), running time is O(n)
one
• Input: A[1 . . n], where 0 ≤ A[i] < 1 for all i
• Output: elements A[i] sorted
• Auxiliary array: B[0 . . n - 1] of linked lists, each list initially
empty
10
Example - Bucket Sort
A 1 .78 B 0 Step 1: Initialize B[i] into
an empty list
2 .17 1
3 .39 2
4 .26 3
5 .72 4
6 .94 5
7 .21 6
8 .12 7
9 .23 8
10 .68 9
11
Example - Bucket Sort
A 1 .78 B 0 Step 1: Initialize B[i] into
an empty list
2 .17 1
5 .72 4
6 .94 5
7 .21 6
8 .12 7
9 .23 8
10 .68 9
12
Example - Bucket Sort
A 1 .78 B 0 /
5 .72 4 /
6 .94 5 /
7 .21 6 .68 /
9 .23 8 /
10 .68 9 .94 /
13
Example - Bucket Sort
.12 .17 .21 .23 .26 .39 .68 .72 .78 .94 /
0 /
1 .12 .17 / Step 1: Initialize B[i] into
an empty list
2 .21 .23 .26 /
Step 2: Insert A[i] into
.39 /
3 list B[ 𝑛𝐴[𝑖] ]
4 /
5 / Step 3: Sort each list
6 .68 /
.72 .78 /
Step 4: Concatenate the
7
lists from 0 to n – 1
8 / together, in order
9 .94 /
14
Bucket Sort: Algorithm
O(n)
(n)
O(n)
(n)
(n)
19
Problems
• Show how you can sort n integers in the range 1 to
n2 in O(n) time.
20
Conclusion
• Any comparison sort will take at least nlgn to sort an array
of n numbers
• We can achieve a better running time for sorting if we can
make certain assumptions on the input data:
▪ Counting sort: each of the n input elements is an integer
21
Acknowledgement
• Dr George Bebis, Foundation Professor, Dept of
Computer Science and Engineering, University of
Nevada Reno