Professional Documents
Culture Documents
1
Group Members
P.D. Bulathsinhala
W.N.S. Fernando
K.K.H. Rangana
Rajeswaran Indravarman
M.K.H. Gunasekara
2
Introduction
4
Simulation
173 256 548 326 753 478 222 144 721 875
875
721
144
222
478
753
326
548
256
173
0 1 2 3 4 5 6 7 8 59
Simulation
0 1 2 3 4 5 6 7 8 9
875
721
144
222
478
753
326
548
256
173
326 478
0 1 2 3 4 5 6 7 8 69
Simulation
326 478
0 1 2 3 4 5 6 7 8 9
875
721
144
222
478
753
326
548
256
173
144 173 222 256 326 478 548 721 753 875
8
Bucket Sort
ALGORITHM
1.Keep an array count of size m(Maximum range of output)
2.Repeat following steps till input is exhausted
a) Read input number as x.
b) Increment count [x] by 1
3.Repeat the following for i=0 to m
a) Assign j to zero
b) While count [ i ] is not equal to zero do
i) a[ j ] = I and increment j
ii) Decrement count[ i ]
4.Return the sorted elements stored in array a.
9
Pseudo code for radix sort
ALGORITHM
1.Create an array a[ 0…..n-1] elements.
2.Call bucket sort repeatedly on least to most significant digit of each
element as the key.
3.Return the sorted array.
10
C code for radix sort
void radixsort(int *a, int n) #if def SHOWPASS
{ printf("\nPASS : ");
int i, b[MAX], m = a[0], exp = 1; print(a, n);
for (i = 1; i < n; i++) #endif
{ }
if (a[i] > m) }
m = a[i];
}
ALGORITHM
12
Example
23 69 72 85 90 35 58 95 48
0 => 90
1 =>
2 => 72
3 => 23
4 =>
5 => 85 35 95
6 =>
7 =>
8 => 58 48
9 => 69
13
Example
90
90 72
72 23
23 85
85 35
35 95
95 58
58 48
48 68
0 => 90
1 =>
2 => 72
3 => 23
4 =>
5 => 85 35 95
6 =>
7 =>
8 => 58 48
9 => 69
14
Example
90 72 23 85 35 95 58 48 69
0 =>
1 =>
2 => 23
3 => 35
4 => 48
5 => 58
6 => 69
7 => 72
8 => 85
9 => 90 95
15
Recursive Version - Inefficient
• Each bucket is dynamically
allocated and resized as needed
• This runs the risk of serious
memory fragmentation
• Degrade performances.
16
String variation
• The keys are strings of d
characters each
• We represent each key by a d-
tuple of integers, where is the
ASCII (8-bit integer) or Unicode
(16-bit integer) representation of
the i-th character and apply radix
sort.
17
Analysis
Merge sort
Radix sort
sorted
the overall relation between input size and execution time (in seconds). 19
Applications
• Mostly used in parallel computing
20
21
22