What is radix sort?

Radix sort, unlike merge-sort or quick-sort, is a non-comparative sorting algorithm. The idea behind it is to sort a list of integers based on their individual digits. In other words, sorting is performed from the least significant digit to the most significant digit.

Radix sort uses the same idea behind count sort/bucket sort.

Algorithm

The following illustration underlines the working of the Radix sort algorithm:

1 of 5

Implementation

The Radix sort algorithm in C++ is implemented below:

#include <iostream>
using namespace std;
// utility function for printing the array.
void print(int arr[], int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void RadixSort(int a[], int arraySize)
{
// Initialize a bucket array and the starting LSD (digitPosition).
int i, bucket[arraySize], maxVal = 0, digitPosition = 1;
// Find the maximum value in the array.
// The number of passes in the algorithm will be
// the number of digits the largest number has (maxVal).
for(i = 0; i < arraySize; i++)
{
if(a[i] > maxVal)
maxVal = a[i];
}
int pass = 1; // used to show the progress
/* This is the Count Sort algorothm: */
while(maxVal/digitPosition > 0) {
// digitCount[i] array will be counting the number
// of array values having that 'i' digit at their
// digitPosition-th place.
int digitCount[10] = {0};
// Count the number of times each digit occurred at (digitPosition)th place in every input.
for(i = 0; i < arraySize; i++)
digitCount[a[i]/digitPosition%10]++;
// Find cumulative count, so it now stores actual position
// of the digit in the output array (bucket).
for(i = 1; i < 10; i++)
digitCount[i] += digitCount[i-1];
// To keep the order, start from back side.
for(i = arraySize - 1; i >= 0; i--)
bucket[--digitCount[a[i]/digitPosition%10]] = a[i];
// Rearrange the original array using elements in the bucket
for(i = 0; i < arraySize; i++)
a[i] = bucket[i];
/* End of Count Sort Algo */
// At this point, the array is sorted by digitPosition-th digit
cout << "Pass #" << pass++ << ": ";
print(a, arraySize);
// Move to the next digit position, or next least significant digit.
digitPosition *= 10;
}
}
// Driver code.
int main()
{
int arr[] = {82, 901, 100, 12, 150, 77, 55, 23};
int size = sizeof(arr)/sizeof(arr[0]);
cout << "The unsorted array is: " << endl;
print(arr,size);
cout << endl;
cout << "Starting Radix Sort: " << endl;
cout << "Pass #0: ";
print(arr,size);
RadixSort(arr,size);
return 0;
}

Explanation

  • The maximum value of the input array, calculated from line 2121 to line 2525, controls the while loop condition on line 3030; the number of digits in the maximum value is the number of passes that need to be made by the algorithm.
  • Line 3030-5252 is the count/bucket sort algorithm. It sorts the array based on the digit at the digitPosition-th place.
  • Line 6060ā€‹ advances the least significant digit to the next place. (e.g., 10ā€™s to 100ā€™s and so on).
Copyright Ā©2024 Educative, Inc. All rights reserved