Radix Sort in C++

What is Radix Sort in C++?

Radix Sort is a Sorting algorithm that can be only applied to numeric values. This sorting technique that sorts the elements into a logical order by  grouping the individual digits of the same place value. This sorting of individual digits of same place value is done with the help of counting sort.

In this article we will learn more about radix sort in C++. You can learn more about counting sort by clicking the button below.

Radix sort algorithm in C++

What is Radix Sort in C++?

Radix Sort is a Sorting algorithm that can be only applied to numeric values. This sorting technique that sorts the elements into a logical order by  grouping the individual digits of the same place value. This sorting of individual digits of same place value is done with the help of counting sort. In this article we will learn more about radix sort in C++. You can learn more about counting sort by clicking the button below.

Working of radix sort in C++

Following steps are followed to sort any numeric data in a logical order using radix sort :-

  • Take unit place digit of each element.
  • Sort the element according to their unit place using counting sort.
  • Now look upon the ten’s place digit of each element.
  • Sort the element according to their ten’s place using counting sort.
  • Repeat the step for digit of each place value.

Algorithm for radix sort in C++

Radix-Sort (array A, int n, int d)

  1. for i ← 1 to d
  2. do sort A to sort array A on digit i

Program for radix sort in C++

Radix Sort in C++ is an efficient non-comparative sorting algorithm that sorts numbers by processing individual digits. The program typically uses counting sort as a subroutine to sort digits from least significant to most significant, resulting in a fully sorted array. This approach is especially useful for large datasets with integer keys.

Run

#include<iostream> 
using namespace std;

//Function to get the largest element from an array
int getMax(int array[], int n)
{
  int max = array[0];
  for (int i = 1; i < n; i++) if (array[i] > max)
      max = array[i];
  return max;
}

//Using counting sort to sort the elements in the basis of significant places
void countSort(int array[], int size, int place) 
{
  const int max = 10;
  int output[size];
  int count[max];

  for (int i = 0; i < max; ++i)
    count[i] = 0;

  //Calculate count of elements
  for (int i = 0; i < size; i++)
    count[(array[i] / place) % 10]++;

  //Calculating cumulative count
  for (int i = 1; i < max; i++)
    count[i] += count[i - 1];

  //Placing the elements in sorted order
  for (int i = size - 1; i >= 0; i--) 
  {
    output[count[(array[i] / place) % 10] - 1] = array[i];
    count[(array[i] / place) % 10]--;
  }

  for (int i = 0; i < size; i++)
    array[i] = output[i];
}

//Main function to implement radix sort
void radixsort(int array[], int size) 
{
  //Getting maximum element
  int max = getMax(array, size);

  //Applying counting sort to sort elements based on place value.
  for (int place = 1; max / place > 0; place *= 10)
    countSort(array, size, place);
}

//Printing an array
void display(int array[], int size) 
{
  int i;
  for (i = 0; i < size; i++)
    cout << array[i] << "\t";
  cout << endl;
}

int main() 
{
  int array[] = {112, 400, 543, 441, 678, 675, 9, 777};
  int n = sizeof(array) / sizeof(array[0]);
  cout<<"Before sorting \n";
  display(array, n);
  radixsort(array, n);
  cout<<"After sorting \n";
  display(array, n);
}

Output:

Before sorting 
112	400	543	441	678	675	9	777	
After sorting 
9	112	400	441	543	675	678	777	

Explanation: 

  • The largest element in the array is found using getMax to determine the number of digits for sorting.
  • countSort arranges elements according to a specific digit place while preserving their relative order.
  • radixsort sorts the array completely by applying countSort from the least significant digit to the most significant digit.
  • The display function prints the array before and after sorting.
  • In main, the array is initialized, displayed, sorted with Radix Sort, and displayed again to show the sorted result.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Time and Space Complexity for Radix Sort in C++

Time Complexity of Radix Sort

Radix Sort works by sorting numbers digit by digit. It uses a stable sorting method (like Counting Sort) for each digit.

Let:

  • n = number of elements
  • k = number of digits in the largest number (for example, 802 has 3 digits)

Best Case: O(n * k)
It goes through each digit of every number, even if the data is already sorted.

Average Case: O(n * k)
It still needs to process all digits of all numbers.

Worst Case: O(n * k)
Even when the numbers are in the worst possible order, it processes all digits the same way.

So, in all cases, the time complexity is O(n * k), where k depends on the number of digits in the largest number.

Space Complexity of Radix Sort

Radix Sort needs some extra space to work:

  • An output array of size n to store results during sorting.
  • A count array of fixed size (usually 10 for base 10 numbers) to count digits.

Space Complexity: O(n + b)

Where:

  • n is the number of elements
  • b is the base (for decimal numbers, b = 10)

To Wrap It Up:

Radix Sort in C++ provides an efficient way to sort numeric data by processing digits place by place, using Counting Sort as a subroutine. This method ensures a stable sorting process and works well for numbers with multiple digits.

Its time complexity remains O(n × k) across best, average, and worst cases, making it predictable for performance analysis. The algorithm requires extra space for output and count arrays, resulting in a space complexity of O(n + b), where b is the number base.

FAQs

Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits from least significant to most significant using a stable sorting method like counting sort. It is mainly used for integer sorting.

It works by sorting the array digit by digit, starting from the least significant digit to the most significant, ensuring that the relative order of numbers with the same digit is preserved.

The time complexity is O(d*(n + k)), where n is the number of elements, d is the number of digits in the largest number, and k is the range of digits (usually 0–9).

Radix Sort is ideal for sorting large lists of integers or strings with a fixed length, especially when comparison-based sorting algorithms like quicksort are less efficient.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription