Radix Sort
Radix Sort
1st Iteration:pass1:
0 1 2 3 4 5 6 7 8 9
count 2 2 1 0 0 1 0 1 2 1
Count[i] 2 4 5 5 5 6 6 7 9 10
1st 1 3 4 5 6 8 9
decrement
2nd 0 2 7
decrement
B[i] 530 090 231 011 432 045 677 008 088 199
2nd iteration:pass2:
after the pass 1,the temporary array value b[i] are
530 090 231 011 432 045 677 008 088 199
Form pass 1 ,According to the numbers at the 2nd least significant bit position
are
We will arrange them from lowest to highest using count sort.
0 1 2 3 4 5 6 7 8 9
count 1 1 0 3 1 0 0 1 1 2
Count[i] 1 2 2 5 6 6 6 7 8 10
1st 0 1 4 5 6 7 9
decrement
2nd 3 8
decrement
B[i] 008 011 530 231 432 045 677 088 090 199
2nd Iteration:pass3:
after the pass 2,the temporary array value b[i] are
008 011 530 231 432 045 677 088 090 199
Form pass 1 ,According to the numbers at the 3rd least significant bit position
are 0,0,5,2,4,0,6,0,0,1
We will arrange them from lowest to highest using count sort.
0 1 2 3 4 5 6 7 8 9
count 5 1 1 0 1 1 1 0 0 0
Count[i] 5 6 7 7 8 9 10 10 10 10
1st 4 5 6 7 8 9
decrement
2nd 3
decrement
3rd 2
decrement
4th 1
decrement
5th 0
decrement
B[i] 008 011 045 088 090 199 231 432 530 677
We have reached the end of the iteration and we have sorted our array as well.
After sorting, we have the following array
008 011 045 088 090 199 231 432 530 677
//c program for radix sort.
#include <stdio.h>
int main() {
int size;
printf("Enter the number of elements: ");
scanf("%d", &size);
int array[size];
printf("Enter the elements:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
radixsort(array, size);