Radix Sort in C++
Radix Sort Algorithm in C++ (Complete Implementation).
1#include <iostream>2#include <algorithm>3using namespace std;45int getMax(int arr[], int n) {6 int max = arr[0];7 for (int i = 1; i < n; i++) {8 if (arr[i] > max)9 max = arr[i];10 }11 return max;12}1314void countSort(int arr[], int n, int exp) {15 int output[n];16 int count[10] = {0};1718 // Store count of occurrences19 for (int i = 0; i < n; i++)20 count[(arr[i] / exp) % 10]++;2122 // Change count to position23 for (int i = 1; i < 10; i++)24 count[i] += count[i - 1];2526 // Build output array27 for (int i = n - 1; i >= 0; i--) {28 output[count[(arr[i] / exp) % 10] - 1] = arr[i];29 count[(arr[i] / exp) % 10]--;30 }3132 // Copy output to original array33 for (int i = 0; i < n; i++)34 arr[i] = output[i];35}3637void radixSort(int arr[], int n) {38 int max = getMax(arr, n);3940 // Do counting sort for every digit41 for (int exp = 1; max / exp > 0; exp *= 10)42 countSort(arr, n, exp);43}4445void printArray(int arr[], int n) {46 for (int i = 0; i < n; i++) {47 cout << arr[i] << " ";48 }49 cout << endl;50}5152int main() {53 int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};54 int n = sizeof(arr) / sizeof(arr[0]);5556 cout << "Original array: ";57 printArray(arr, n);5859 radixSort(arr, n);6061 cout << "Sorted array: ";62 printArray(arr, n);6364 return 0;65}
Output
Original array: 170 45 75 90 802 24 2 66 Sorted array: 2 24 45 66 75 90 170 802
What's going on
The program sorts an array of integers using the Radix Sort algorithm. For example:
Radix Sort processes digits from right to left (least significant to most significant), using Counting Sort for each digit position.
Header Files Used
Understanding Radix Sort
Algorithm Concept
:
Visual Example
:
Array: [170, 45, 75, 90, 802, 24, 2, 66]
Pass 1 (ones place): [170, 90, 802, 2, 24, 45, 75, 66]
Pass 2 (tens place): [802, 2, 24, 45, 66, 170, 75, 90]
Pass 3 (hundreds place): [2, 24, 45, 66, 75, 90, 170, 802]
Function: getMax()
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
}
1return max;
How it works
:
Function: countSort()
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
// Change count to position
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build output array
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy output to original array
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
1// Store count of occurrences
How it works
:
Example
(exp = 1, ones place):
Function: radixSort()
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
How it works
:
Example
(max = 802):
Time and Space Complexity
Time Complexity
: O(d * (n + k))
Space Complexity
: O(n + k)
Stability
: Stable
When to Use Radix Sort
Best For
:
Not Recommended For
:
Important Considerations
Digit Extraction
:
Stability
:
Number of Passes
:
return 0;
This ends the program successfully.