Algorithms and Data Structures: Counting Sort and Radix Sort
Algorithms and Data Structures: Counting Sort and Radix Sort
Algorithms and Data Structures: Counting Sort and Radix Sort
The loops in lines 35, 912, and 1314 all require time (n).
(After sorting, 2 items with the same key have their initial relative
order).
ADS: lect 8 slide 6 15th October, 2013
Analysis of Counting Sort
The loops in lines 35, 912, and 1314 all require time (n).
(After sorting, 2 items with the same key have their initial relative
order).
ADS: lect 8 slide 6 15th October, 2013
Analysis of Counting Sort
The loops in lines 35, 912, and 1314 all require time (n).
(After sorting, 2 items with the same key have their initial relative
order).
ADS: lect 8 slide 6 15th October, 2013
Radix Sort
Basic Assumption
Keys are sequences of digits in a xed range 0, . . . , R 1,
all of equal length d.
Examples of such keys
If keys and elements are exactly the same thing (in our setting, an
element is a structure containing the key as a sub-element) then we
have a much easier (non-stable) version of Counting-Sort.
(How? ... CLASS?).
ADS: lect 8 slide 8 15th October, 2013
Stable Sorting Algorithms
Denition 1
A sorting algorithm is stable if it always leaves elements with equal keys
in their original order.
Examples
If keys and elements are exactly the same thing (in our setting, an
element is a structure containing the key as a sub-element) then we
have a much easier (non-stable) version of Counting-Sort.
(How? ... CLASS?).
ADS: lect 8 slide 8 15th October, 2013
Radix Sort (contd)
Idea
Sort the keys digit by digit, starting with the least
signicant digit.
Example
now
for
tip
ilk
dim
tag
jot
sob
nob
sky
hut
ace
bet
sob
for
nob
ace
ilk
tag
dim
tip
jot
hut
bet
now
sky
tag
ace
bet
hut
dim
ilk
sky
tip
now
jot
for
sob
nob
ace
bet
dim
for
tip
tag
sob
sky
now
nob
jot
ilk
hut
Each of the three sorts is carried out with respect to the digits in that
column. Stability (and having previously sorted digits/suxes to the
right), means this achieves a sorting of the suxes starting at the current
column.
ADS: lect 8 slide 9 15th October, 2013
Radix Sort (contd)
Algorithm Radix-Sort(A, d)
1. for i 0 to d do
2. use stable sort to sort array A using digit i as key
Most commonly, Counting Sort is used in line 2 - this means that once
a set of digits is already in sorted order, then (by stability) performing
Counting Sort on the next-most signicant digits preserves that order,
within the blocks constructed by the new iteration.
Then each execution of line 2 requires time (n + R).
Thus the overall time required by Radix-Sort is
(d(n + R))
ADS: lect 8 slide 10 15th October, 2013
Sorting Integers with Radix-Sort
Theorem 2
An array of length n whose keys are b-bit numbers can be sorted in time
(nb/ lg n)
using a suitable version of Radix-Sort.
Proof: Let the digits be blocks of lg n bits. Then R = 2
lg n
= (n)
and d = b/lg n. Using the implementation of Radix-Sort based on
Counting Sort the integers can be sorted in time
(d(n + R)) = (nb/ lg n).
Note: If all numbers are at most n
k
, then b = k lg n . . . Radix Sort
is (n) (assuming k is some constant, eg 3, 10).
ADS: lect 8 slide 11 15th October, 2013
Reading Assignment
[CLRS] Sections 8.2, 8.3
Problems
1. Think about the qn. on slide 7 - how do we get a very easy
(non-stable) version of Counting-Sort if there are no items
attached to the keys?
2. Can you come up with another way of achieving counting sorts
O(m + n)-time bound and stability (you will need a dierent data
structure from an array).
3. Exercise 8.3-4 of [CLRS].
ADS: lect 8 slide 12 15th October, 2013