Radix Sort
Radix Sort Algorithm in C++ (Complete Implementation)
C++ Radix Sort Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <algorithm>
using namespace std;
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
// Store count of occurrences
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];
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
// Do counting sort for every digit
for (int exp = 1; max / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
radixSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}Original array: 170 45 75 90 802 24 2 66 Sorted array: 2 24 45 66 75 90 170 802
Understanding Radix Sort
This program teaches you how to implement the Radix Sort algorithm in C++. Radix Sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits from least significant to most significant. It uses Counting Sort as a subroutine for each digit position. Radix Sort is efficient for integers with a limited number of digits.
---
1. What This Program Does
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.
---
2. Header Files Used
---
3. Understanding Radix Sort
Algorithm Concept
:
Visual Example
:
---
4. 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];
}
}
return max;How it works
:
---
5. 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];
}
// Store count of occurrencesHow it works
:
Example
(exp = 1, ones place):
---
6. 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):
---
7. Time and Space Complexity
Time Complexity
: O(d * (n + k))
Space Complexity
: O(n + k)
Stability
: Stable
---
8. When to Use Radix Sort
Best For
:
Not Recommended For
:
---
9. Important Considerations
Digit Extraction
:
Stability
:
Number of Passes
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning non-comparative sorting algorithms, understanding digit-based sorting, and preparing for advanced sorting techniques in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Radix Sort
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.