Professional Documents
Culture Documents
Iitk DS CS210 2012
Iitk DS CS210 2012
(CS210/ESO207/ESO211)
Lecture 37
Integer Sorting (continued from last lecture)
0A
6.1
7.9
2A
3
could
used
Why
did have
we scan
We
Count 2
1
Place
2
Count of
array
to
elements
A inonly
reverse
output
the elements
order (from
index to of
)
A
in sorted
order.
Why
while
placing
them
in
did
compute
Place
thewe
final
sorted array
and
B ?B ?
CountSort(A[...], )
For =0 to do Count[] 0;
For =0 to do Count[A[]] Count[A[]] +1;
For =0 to do Place[] Count[];
For =1 to do Place[] Place[] + Count[];
For = to do
{
??
] A[];
B[
Place[A[]]-1
Place[A[]] Place[A[]]-1;
}
return B;
in word RAM).
It is a stable sorting algorithm.
Radix Sort
Radix Sort
2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258
=4
=12
=10
2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258
5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299
2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258
5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299
2012
7098
6239
92588
299
2373
1385
3465
5501
9624
5810
4961
2012
1385
4961
5810
2373
6239
9624
8299
3465
7098
5501
9258
5501
5810
2012
9624
6239
9258
4961
3465
2373
1385
7098
8299
2012
7098
6239
92588
299
2373
1385
3465
5501
9624
5810
4961
1385
2012
2373
3465
4961
5501
5810
6239
7098
8299
92589
624
Radix Sort
RadixSort(A[...], )
{ For =1 to do
Execute CountSort(A,) with th digit as the key;
return A;
}
Correctness:
A number stored in A
Inductive assertion:
At the end of th iteration, array A is sorted according to the last digits.
During the induction step, you will have to use
the fact that Countsort is a stable sorting
algorithm.
Radix Sort
RadixSort(A[...], )
{ For =1 to do
Execute CountSort(A,) with th digit as the key;
return A;
}
Time complexity:
A single execution of CountSort(A,) runs in O() time and O() space.
Since , a single execution of CountSort(A,) runs in O() time.
Time complexity of radix sort = O().
Extra space used = O().
Question: How to sort A storing integers in range [..] in O() time and
O() space ?
Answer:
RadixSort(A[...], )
Problem Description
: {} called universe
,
,
0 1 2
0 0 1 A0 0
0 1 0
0 0 1
Next class
Hashing (15 minutes will be devoted to this topic)