Q1.Explain radix sort with example?
(W-8)
Q2.Describe the principal of radix sort
with one example.
Q3.Describe radix sort algorithm.
(W-15)
• Radix sort is the generalization of bucket sort.
• To sort decimal numbers, where the radix or base is 10, we
need 10 buckets.
• These buckets are numbered 0,1,2,3,4,5,6,7,8,9.
• Sorting is done in passes.
• Number of passes required to sort is equal to number of digits
in the largest number in the list.
Range Passes
0 to 99 2 passes
0 to 999 3 passes
0 to 9999 4 passes
• In the first pass, numbers are sorted on least significant digit.
Numbers with the same least significant digit are stored in the
same bucket.
• In the 2nd pass, numbers are stored on the second least
significant digit.
• At the end of every pass, numbers in buckets are merged to
produce a common list.
• Number of passes depends on the range of numbers being
sorted.
• Radix sort is a sorting technique that sorts the elements by first
grouping the individual digits of the same place value.
• Radix sort works by sorting each digit from least significant
digit to most significant digit.
• So in base 10 (the decimal system), radix sort would sort by the
digits in the 1's place, then the 10’s place, and so on. To do this,
radix sort uses counting sort as a subroutine to sort the digits in
each place value.
• This means that for a three-digit number in base 10, counting
sort will be called to sort the 1's place, then it will be called to
sort the 10's place, and finally, it will be called to sort the 100's
place, resulting in a completely sorted list.
10 5 99 105 55 100 135 141 137 200 199
135
200 55
100 105 199
10 141 5 137 99
0 1 2 3 4 5 6 7 8 9
Merged List = 10 100 200 141 5 105 55 135 137 99 199
10 100 200 141 5 105 55 135 137 99 199
105
200 137 199
100 10 135 141 55 99
0 1 2 3 4 5 6 7 8 9
Merged List = 100 200 5 105 10 135 137 141 55 99 199
100 200 5 105 10 135 137 141 55 99 199
199
141
99 137
55 135
10 105
5 100 200
0 1 2 3 4 5 6 7 8 9
Merged List = 5 10 55 99 100 105 135 137 141 199 200
Home Work
Ex.1 Q= 348,14,614,5381,47
Ex.2 Q= 361,12,527,143,9,768,348
Ex.3 Q= 12,8,25,4,66,2,98,225
Ex.4 Q= 7,103,15,10,3,25,28,67,304,36,49,84
Ex.5 Q= 87.3 , 2.34,7.29 ,3.59,45.8,3.79,3.20,4.22
Ex.6 Q=18,253,1000,2,80,75,58
7,103,15,10,3,25,28,67,304,36,49,84
3 84 25 67
10 103 304 15 36 7 28 49
0 1 2 3 4 5 6 7 8 9
Merged List = 10 , 103,3,304,84,15,25,36,7,67,28,49
10 , 103,3,304,84,15,25,36,7,67,28,49
7
304
003 15 28
103 10 25 36 49 67 84
0 1 2 3 4 5 6 7 8 9
Merged List = 103,003,304,7,10,15,25,28,36,49,67,84
010,103,003,304,084,015,025,036,007,067,028,049
103,3,304,7,10,15,25,28,36,49,67,84
84
67
49
36
28
25
15
10
7
3 103 304
0 1 2 3 4 5 6 7 8 9
Merged List = 3,7,10,15,25,28,36,49,67,84,103,304
103,003 ,304,007,010,015,025,028,036,049,067,084
348,14,614,5381,47
614
5381 14 47 348
0 1 2 3 4 5 6 7 8 9
Merged List = 5381,14,614,47,348
5381,14,614,47,348
614 348
14 47 5381
0 1 2 3 4 5 6 7 8 9
Merged List = 14,614,47,348,5381
14,614,47,348,5381
47 5381
14 348 614
0 1 2 3 4 5 6 7 8 9
Merged List = 14,47,348,5381,614
14,47,348,5381,614
614
348
47
14 5381
0 1 2 3 4 5 6 7 8 9
Merged List = 14,47,348,617,5381
/* Numbers to be sorted are in array a[] and there are N numbers */
1. large= find the largest number in a[ ]
2. Passes = Numbers of digit in large
3. Div = 1
/* divisor for extracting ith least significant digit */
4. For ( i=1;i<=passes ; i++)
{
1. initialize all buckets b0 to b9
2. for each number x from a[0] to a [ N-1]
{
1. bucket _ no =(x/div)%10
2. insert x in bucket with bucket _ number
}
3. Copy elements of buckets back in array a[ ]
4. DIV = DIV*10
}
5. Exit
1. large= find the largest number in a[ ]
1. large= find the largest number in a[ ]
2. Passes = Numbers of digit in large
2. Passes = Numbers of digit in large
3. Div = 1
3. Div = 1
4. For ( i=1;1<=3 ; i++)
4. For ( i=1;i<=passes ; i++)
{
{
1. initialize all buckets b0 to b9
1. initialize all buckets b0 to b9
2. for each number x from a[0] to a [ N-1]
2. for each number x from a[0] to a [ N-1]
{
{
1. bucket _ no =(x/div)%10
1. bucket _ no =(x/div)%10
(235/1) %10=5
2. insert x in bucket with bucket _ number
2. insert x in bucket with bucket _ number
}
}
3. Copy elements of buckets back in array a[ ]
3. Copy elements of buckets back in array a[ ]
4. DIV = DIV*10
4. DIV = DIV*10 1*10 = 10
}
}
5. Exit 5. Exit
1. large= find the largest number in a[ ]
1. large= find the largest number in a[ ]
2. Passes = Numbers of digit in large
2. Passes = Numbers of digit in large
3. Div = 1
3. Div = 1
4. For ( i=1; 2<=3 ; i++)
4. For ( i=1;i<=passes ; i++)
{
{
1. initialize all buckets b0 to b9
1. initialize all buckets b0 to b9
2. for each number x from a[0] to a [ N-1]
2. for each number x from a[0] to a [ N-1]
{
{
1. bucket _ no =(x/div)%10
1. bucket _ no =(x/div)%10
(235/10) %10=3
2. insert x in bucket with bucket _ number
2. insert x in bucket with bucket _ number
}
}
3. Copy elements of buckets back in array a[ ]
3. Copy elements of buckets back in array a[ ]
4. DIV = DIV*10
4. DIV = DIV*10 10*10 = 100
}
}
5. Exit 5. Exit
1. large= find the largest number in a[ ]
1. large= find the largest number in a[ ]
2. Passes = Numbers of digit in large
2. Passes = Numbers of digit in large
3. Div = 1
3. Div = 1
4. For ( i=1; 3<=3 ; i++)
4. For ( i=1;i<=passes ; i++)
{
{
1. initialize all buckets b0 to b9
1. initialize all buckets b0 to b9
2. for each number x from a[0] to a [ N-1]
2. for each number x from a[0] to a [ N-1]
{
{
1. bucket _ no =(x/div)%10
1. bucket _ no =(x/div)%10
(235/100) %10=2
2. insert x in bucket with bucket _ number
2. insert x in bucket with bucket _ number
}
}
3. Copy elements of buckets back in array a[ ]
3. Copy elements of buckets back in array a[ ]
4. DIV = DIV*10
4. DIV = DIV*10 100*10 = 1000
}
}
5. Exit 5. Exit
1. large= find the largest number in a[ ]
1. large= find the largest number in a[ ]
2. Passes = Numbers of digit in large
2. Passes = Numbers of digit in large
3. Div = 1
3. Div = 1
4. For ( i=1; 4<=3 ; i++)
4. For ( i=1;i<=passes ; i++)
{
{
1. initialize all buckets b0 to b9
1. initialize all buckets b0 to b9
2. for each number x from a[0] to a [ N-1]
2. for each number x from a[0] to a [ N-1]
{
{
1. bucket _ no =(x/div)%10
1. bucket _ no =(x/div)%10
2. insert x in bucket with bucket _ number
2. insert x in bucket with bucket _ number
}
}
3. Copy elements of buckets back in array a[ ]
3. Copy elements of buckets back in array a[ ]
4. DIV = DIV*10 100*10 = 1000
4. DIV = DIV*10
}
}
5. Exit
5. Exit