
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Radix Sort
The radix sort is a non-comparative sorting algorithm that sorts the individual digits of the given numbers in the list. It sorts the numbers digit by digit, starting from the least significant digit to the most significant digit.
The radix sort assumes that all the input elements are k-digit numbers. So, if the elements do not have the same number of digits, find the maximum number of digits in an input element and prepend zeroes to the elements having less number of digits.
In this article, we have an unsorted array of integers and our task is to implement the radix sort algorithm to sort the unsorted array in C++. Here is an example of radix sort:
Input: array = {170, 45, 75, 90, 802, 24, 2, 66} Output: Sorted array: {2, 24, 45, 66, 75, 90, 170, 802}
The explanation of the above example:
First step is prepending 0's to the elements: array = {170, 045, 075, 090, 802, 024, 002, 066} Pass 1: Sorting the elements based on the unit place: array after pass 1 = {170, 090, 802, 002, 024, 045, 075, 066} Pass 2: Sorting the elements based on the tenth place: array after pass 2 = {802, 002, 024, 045, 066, 170, 075, 090} Pass 3: Sorting the elements based on the hundredth place: array after pass 3 = {002, 024, 045, 066, 075, 090, 170, 802} array after sorting = {2, 24, 45, 66, 75, 90, 170, 802}
In the above image, during Pass 1 we have sorted the digits at unit place only. Similarly, in pass 2 and pass 3 the digits at tenth and hundredth place have been sorted.
Steps to Implement Radix Sort
We will use the following steps to implement the radix sort algorithm:
- The radixSort() function sorts the given array. It accepts 3 arguments which are: the first element of the array, the number of elements, and the maximum digit in the largest number.
- The m sets the range of digits we are looking for in the number. It is used with the temp variable so that we can use digits up to which we are sorting.
- The p defines the digit's place in the number and with the index variable it decides the pocket for the digit.
- For each digit place, first, we find the digit at the current place and then place it into the corresponding bucket.
- We have used the for loop with the while loop to put the numbers back in the array in the sorted order.
- The display() function is used to print the sorted array.
C++ Program to Implement Radix Sort
Here is an example using the above steps to implement radix sort in C++:
#include<iostream> #include<list> #include<cmath> using namespace std; // Displays the array void display(int *array, int size) { for(int i = 0; i < size; i++) cout << array[i] << " "; cout << endl; } // Function to implement radix sort void radixSort(int *arr, int n, int max) { int i, j, m, p = 1, index, temp, count = 0; list<int> pocket[10]; // radix = 10 for decimal system for(i = 0; i < max; i++) { m = pow(10, i + 1); p = pow(10, i); for(j = 0; j < n; j++) { temp = arr[j] % m; index = temp / p; // digit at current place pocket[index].push_back(arr[j]); } count = 0; for(j = 0; j < 10; j++) { while(!pocket[j].empty()) { arr[count] = *(pocket[j].begin()); pocket[j].erase(pocket[j].begin()); count++; } } } } int main() { int arr[] = {170, 45, 75, 90, 802, 24, 2, 66}; int n = sizeof(arr) / sizeof(arr[0]); int maxDigits = 3; // max number of digits (802 has 3) cout << "Array before Sorting: "; display(arr, n); radixSort(arr, n, maxDigits); cout << "Array after Sorting: "; display(arr, n); return 0; }
The output of the above code:
Array before Sorting: 170 45 75 90 802 24 2 66 Array after Sorting: 2 24 45 66 75 90 170 802
Complexity of Radix Sort Technique
The time and space complexity of radix sort is mentioned below:
-
Time Complexity: O(nk), where n is the number of elements and k is the number of digits in the largest number.
-
Space Complexity: O(n + k), due to the use of additional buckets for each digit place.