Radix Sort in C. Radix sort is a non as non-comparative sorting algorithm that processes integer numbers by grouping them based on the individual digits that share the same position and value. Unlike traditional compression-based sorting algorithms, such as Quick sort and Merge sort, Radix sort operates on the premise of digit-by-digit sorts starting from the least significant digit to the most significant digit. This unique approach enables Radix sort to efficiently handle large data sets, Making it particularly useful in applications where the range of data is significantly large.

## Table of Contents

## How Radix Sort Works

The fundamental operation of Radix sort involves two main phases the distribution phase and the collection phase. These phases are repeated for each significant digit, Starting from the least and moving toward the most significant digit. step-by-step breakdown of the process is given below.

**Distribution Phase:**In this phase, the Radix sort algorithm distributes elements into buckets based on the current digit being considered. For decimal numbers, and buckets (0-9) are used, corresponding to each possible digit value. If the numbers are binary then only 2 buckets (0 and 1) are needed.**Collection Phase:**Once the numbers are distributed into buckets, the algorithm then collects them back in order, starting from the lowest number bucket and moving to the highest. The order within each bucket is preserved as per the previous digit sort, Ensuring stability.**Repeat for Each Digit:**These two phases are repeated for each digit, moving from the least significant digit to the most significant digit. after the final collection phase, the numbers emerge sorted.

## C Program for Radix Sort

To implement radix sort in C, we need to pay attention to a few critical functions: A function to find the maximum number (To know the number of digits), an Afunction for counting sort(To sort based on each digit), and the main radix sort function. Let’s write the C program to perform the Radix sort.

```
void main()
{
int array[] = {6, 7, 8, 4, 2, 67, 23, 56};
int n = sizeof(array) / sizeof(array[0]);
printf("Array before sorting\n");
for (int i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
quickSort(array, 0, n - 1);
printf("\nArray after sorting\n");
for (int i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
}
```

### Output

## Time Complexity of Radix Sort

The time complexity of Radix sort can be represented as `O(nk)`

, where-

- n is the number of elements in the array.
- k is the number of digits in the maximum number(in the case of an integer).

Radix sort offers a unique and efficient method for sorting, especially when dealing with large data sets. Radix Sort’s ability to sort data without direct comparison between the elements makes it stand out among sorting algorithms.

**Happy Coding & Learning**

## 2 thoughts on “Radix Sort in C”