University of Science & Technology, Chittagong
Faculty of Science, Engineering & Technology (FSET)
Course Title: Data Structures
Course Code: CSE 212
Submitted to: Submitted by:
Name: Rittika Dev
Name: Sukanta Paul Roll No: 17010131
Batch No: 29th
Radix Sort
• Radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which
share the same significant position and value.
• That is the idea of radix sort is to do digit by digit sort starting from
least significant digit to most significant digit.
MSD (Most significant digit)
Two classifications
LSD(Least significant digit)
• LSD starts sorting from the least digit moving towards the MSD.
• Whereas, MSD works the other way around.
• It is a method used by a card sorter, which contains 13 receiving pockets
as below:
9,8,7,6,5,4,3,2,1,0,11,12,R(Reject)
• If we have to sort a list of names, in that case radix=26. And there will be
26 classes. Where the first class consists names that starts with “A”,
second class “B” and so on. During the second pass ,each class is
alphabetized to the second letter of the name.
• When it comes to decimal numbers the radix is 10. The sorter uses a
reverse digit sort on numbers.
• That is if a card sorter is given a collection of cards where each card
contains a 3-digit number then the cards will sort according to ,
units digit ten digits hundred digits
• For example, if we take 4 – digit number,
1256, 3597, 2498, 3542, 1235, 4532, 7968, 3539, 2239
• As all the numbers contains 4 – digits, there will be four passes
1st Pass
Input 0 1 2 3 4 5 6 7 8 9
1256 1256
3597 3597
2498 2498
3542 3542
1235 1235
4532 4532
7968 7968
3539 3539
2239 2239
2nd Pass
Input 0 1 2 3 4 5 6 7 8 9
3542 3542
4532 4532
1235 1235
1256 1256
3597 3597
2498 2498
7968 7968
3539 3539
2239 2239
3rd Pass
Input 0 1 2 3 4 5 6 7 8 9
4532 4532
1235 1235
3539 3539
2239 2239
3542
c 3542
1256 1256
7968 7968
3597 3597
2498 2498
4th Pass
Input 0 1 2 3 4 5 6 7 8 9
1235 1235
2239 2239
1256 1256
2498 2498
c4532 4532
3539 3539
3542 3542
3597 3597
7968 7968
Sorted list : 1235, 1256, 2239, 2498, 3539, 3542, 3597, 4532, 7968
#include <stdio.h>
int getMax(int arr[],int n) {
int mx = arr[0];
int i;
for(i = 1; i < n; i++)
if(arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n]; // output array
int i, count[10] = {0};
for(i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for(i = 1; i < 10; i++)
count[i] += count [i-1];
for(i = n-1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for(i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[],int n){
int m = getMax(arr,n);
int exp;
for(exp =1; m/exp>0; exp *=10)
countSort(arr,n,exp);
}
void print(int arr[],int n){
int i;
for(i = 0; i < n; i++)
printf("%d",arr[i]);
}
int main(){
int arr[] = {1256,3597,2498,3542,1235,4532,7968,3539,2239};
int n = sizeof(arr) / sizeof(arr[0]);
radixsort(arr, n);
print(arr, n);
return 0;
}