# Radix Sort Program In C? # How To Best Implement Radix Sort Program In C?

• Count Sort Function

So let us get started then,

In basic words, arranging implies orchestrating the given components in an efficient request. Arranging is done in the greater part of the calculations since it makes search simpler which in the long run makes the calculation effective. In this blog we will comprehend one of the normally utilized soring calculations for example Radix sort.

Radix sort is a non-similar whole number arranging calculation. It does digit by digit arranging beginning from least huge digit(i.e. digit present at the option) to most huge digit(i.e. digit present at the left). Radix sort utilizes considering sort a subroutine to sort.

The lower headed for Comparison based arranging calculation, (for example, Heap sort, Quick Sort, Merge Sort) is Ω(nLogn), and they it can’t be improved past nLogn. In the event that we talk about tallying sort, it is a direct time arranging calculation with O(n+k) time multifaceted nature, where the reach lies between 1 to k. Presently, the issue with tallying sort is, it takes O(n2) when the components territory from 1 to n2.

In this way, to sort a cluster with components that range from 1 to n2 in direct time, we need radix sort. Radix sort sorts the exhibit digit by digit beginning from least huge digit to most critical digit. Radix sort utilizes considering sort a subroutine to sort.

Perform the following steps for all the digits starting from the least significant digit present in right, moving towards most significant digit present in left.

Sort the elements using counting sort according to the current digit.
Example:

Original Array:
140, 65, 85, 110, 612, 54, 12, 86

Sorting the least significant digit i.e. at one’s place, gives

140, 110, 612, 12, 54, 65, 85, 86

NOTE: As 612 appears before 12, and the sorting is done only for one’s digit, thus 612 appears before 12 after this iteration.

Sorting by next digit, i.e. at 10s place, gives:

110, 612, 12, 140, 54, 65, 85, 86

Sorting by most significant digit, i.e. present at the 100s place, gives:

012, 054, 065, 085, 086, 110, 140, 612

## Radix Sort Program In C

First look at Radix sort function

 12345678 `void radixsort(int array[], int n) {``//Get the largest number to know the maximum number of digits``int m = getMax(array, n);``int dig;``//Counting sort is performed for every digit``for (dig = 1; m / dig > 0; dig *= 10)``countSort(array, n, dig);``}`

## Count Sort Function:

 1234567891011121314151617 `void countSort(int array[], int n, int dig) {``int output[n]; ``int i, count = { 0 };``// Store count of occurrences in count[]``for (i = 0; i < n; i++)``count[(array[i] / dig) % 10]++;``// Change count[i] so that count[i] now contains actual ``//  position of this digit in output[] ``for (i = 1; i < 10; i++) count[i] += count[i - 1]; // Build the output array for (i = n - 1; i >= 0; i--) {``output[count[(array[i] / dig) % 10] - 1] = array[i];``count[(array[i] / dig) % 10]--;``}``// Copy the output array to arr[], so that arr[] now ``// contains sorted numbers according to current digit``for (i = 0; i < n; i++)``array[i] = output[i];``}`

 12345678910111213141516171819202122232425262728293031323334353637383940 `#include``//Function to find the Largest Number``int getMax(int array[], int n) {``int max = array;``int i;``for (i = 1; i < n; i++) if (array[i] > max)``max = array[i];``return max;``} ``//Function for Count sort``void countSort(int array[], int n, int dig) {``int output[n]; ``int i, count = { 0 };``// Store count of occurrences in count[]``for (i = 0; i < n; i++)``count[(array[i] / dig) % 10]++; ``// Change count[i] so that count[i] now contains actual ``//  position of this digit in output[] ``for (i = 1; i < 10; i++) count[i] += count[i - 1]; // Build the output array for (i = n - 1; i >= 0; i--) {``output[count[(array[i] / dig) % 10] - 1] = array[i];``count[(array[i] / dig) % 10]--;``} ``// Copy the output array to arr[], so that arr[] now ``// contains sorted numbers according to current digit``for (i = 0; i < n; i++) array[i] = output[i]; } // The main function to that sorts arr[] of size n using Radix Sort void radixsort(int array[], int n) { //Get the largest number to know the maximum number of digits int m = getMax(array, n); int dig; //Counting sort is performed for every digit for (dig = 1; m / dig > 0; dig *= 10)``countSort(array, n, dig);``}``//Function to Print Array``void print(int arr[], int n) {``int i;``for (i = 0; i < n; i++)``printf("%d ", arr[i]);``} ``int main() {``int arr[] = { 140, 65, 85, 110, 612, 54, 12, 86 };``int n = sizeof(arr) / sizeof(arr);``radixsort(arr, n);``print(arr, n);``return 0;``}` 