Radix Sort in C
Here you will learn the program code of Radix Sort in C programming language.
Define Radix Sort
Radix Sort is a powerful algorithm used for sorting large amounts of data efficiently and quickly. Its unique feature is the ability to work on any size of data with excellent performance at low storage.
Radix Sort in C program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | //radix sort program in c #include <stdio.h> #include <stdlib.h> int max (int[],int); void counting_Sort(int[],int,int); void radix_Sort(int[], int); void Display(int[], int); int main() { int num; printf("Input number of elements in array : "); scanf("%d", &num); int array[num]; printf("\nInput %d values :\n",num); for(int i = 0; i < num; i++) { scanf("%d", &array[i]); } radix_Sort(array, num); Display(array, num); return 0; } void counting_Sort(int array[], int num, int ex) { int output[num]; int i, count[10] = {0}; for (i = 0; i < num; i++) count[ (array[i]/ex)%10 ]++; for (i = 1; i < 10; i++) count[i] += count[i - 1]; for (i = num - 1; i >= 0; i--) { output[count[ (array[i]/ex)%10 ] - 1] = array[i]; count[ (array[i]/ex)%10 ]--; } for (i = 0; i < num; i++) array[i] = output[i]; } int Max(int array[], int num) { int mx = array[0]; for (int i = 1; i < num; i++) if (array[i] > mx) mx = array[i]; return mx; } void radix_Sort(int array[], int num) { int m = Max(array, num); for (int ex = 1; m/ex > 0; ex *= 10) counting_Sort(array, num, ex); } void Display(int array[], int num) { printf("\n***Array after radix Sort***\n"); for (int i = 0; i < num; i++) printf("%d ", array[i]); } |
Output
Input number of elements in array : 5
Input 5 values :
3
7
5
2
6
***Array after radix Sort***
2 3 5 6 7
Read Also