There are many sorting algorithms like Merge Sort, Heap Sort, Quick Sort, Insertion Sort, etc., but the lower bound for their time complexity is Ω(nlogn). That means, they cannot perform better than that in the best cases, and can go beyond quadratic time in worst case.

There comes the Radix Sort algorithm, which sorts the numbers using an intermediate algorithm called Counting Sort Algorithm.

Counting sort works in an average time of θ (n + k), where n is the total numbers present with us to sort, and [1, k] is the range for the value of those numbers.

This works in linear time when the value of k is linear with n, but in the case of quadratic value of k, i.e., the k become equivalent to n^{2}, the expression for time complexity becomes θ(n + n^{2}), which on simplifying further gives θ(n^{2}).

In this case, counting sort takes even more time than most of the comparison-based sorting algorithms.

Radix sort algorithm combines the counting sort algorithm to minimize this time complexity.

The basic idea behind radix sort is to sort the numbers present with us according to there least significant digit and most significant digit.

We need to make sure that the numbers we sort according to their digits are sorted in stable manner, also known as stable sort.

*Stable Sorting: This is a sorting methodology which makes sure that the two equal numbers being sorted are at the same position relative to their original position.*

*For example, we have an array [5, 3, 2, 7, 6, 2]*

*An algorithm with stable sorting methodology would sort it as [2, 2, 3, 5, 6, 7], as the 2 in bold appears after the normal 2 in the original array.*

Let us take a simple example to understand radix sort,

Original, unsorted list:

[170, 45, 75, 90, 802, 24, 2, 66]

We can treat this list as,

170, 045, 075, 090, 802, 024, 002, 066

(This would help us in understanding, how radix sort along with counting sort compares the digits according to the digits at different places)

Sorting by least significant digit (1s place) gives:

[*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75. This is because, we need to use an algorithm which includes stable sorting for the radix sort to work]

170, 090, 802, 002, 024, 045, 075, 066

Sorting by next digit (10s place) gives:

[*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

802, 002, 024, 045, 066, 170, 075, 090

Sorting by the most significant digit (100s place) gives:

002, 024, 045, 066, 075, 090, 170, 802

**What is the running time of Radix Sort?**

Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for the decimal system, b is 10. What is the value of d? If k is the maximum possible value, then d would be O(log_{b}(k)). So overall time complexity is O((n+b) * log_{b}(k)). Which looks more than the time complexity of comparison-based sorting algorithms for a large k. Let us first limit k. Let k <= n^{c} where c is a constant. In that case, the complexity becomes O(nLog_{b}(n)). But it still doesn’t beat comparison-based sorting algorithms.

What if we make the value of b larger? What should be the value of b to make the time complexity linear? If we set b as n, we get the time complexity as O(n). In other words, we can sort an array of integers with a range from 1 to n^{c} if the numbers are represented in base n (or every digit takes log_{2}(n) bits).

**Applications of Radix Sort: **

- In a typical computer, which is a sequential random-access machine, where the records are keyed by multiple fields radix sort is used. For e.g., you want to sort on three keys month, day and year. You could compare two records on year, then on a tie-on month and finally on the date. Alternatively, sorting the data three times using Radix sort first on the date, then on month, and finally on year could be used.
- It was used in card sorting machines that had 80 columns, and in each column, the machine could punch a hole only in 12 places. The sorter was then programmed to sort the cards, depending upon which place the card had been punched. This was then used by the operator to collect the cards which had the 1st row punched, followed by the 2nd row, and so on.

Here’s a simple implementation of Radix sort, with the help of counting sort algorithm,

In C++,

```
// C++ implementation of Radix Sort
#include <iostream>
using namespace std;
// A utility function to get maximum value in arr[]
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
// A function to do counting sort of arr[] according to
// the digit represented by exp.
void countSort(int arr[], int n, int exp)
{
int output[n]; // output array
int i, count[10] = { 0 };
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 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[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
// The main function to that sorts arr[] of size n using
// Radix Sort
void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);
// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
// A utility function to print an array
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver Code
int main()
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
radixsort(arr, n);
print(arr, n);
return 0;
}
```

In Java,

```
// Radix sort Java implementation
import java.io.*;
import java.util.*;
class Radix {
// A utility function to get maximum value in arr[]
static int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
// A function to do counting sort of arr[] according to
// the digit represented by exp.
static void countSort(int arr[], int n, int exp)
{
int output[] = new int[n]; // output array
int i;
int count[] = new int[10];
Arrays.fill(count, 0);
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 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[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to current digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
// The main function to that sorts arr[] of size n using
// Radix Sort
static void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);
// Do counting sort for every digit. Note that
// instead of passing digit number, exp is passed.
// exp is 10^i where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
// A utility function to print an array
static void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
/*Driver Code*/
public static void main(String[] args)
{
int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.length;
// Function Call
radixsort(arr, n);
print(arr, n);
}
}
/* This code is contributed by Devesh Agrawal On GeeksForGeeks*/
```

**Output**

2 24 45 66 75 90 170 802

References: