C++ Program - Radix Sort
Radix sort is based on the idea that the sorting of the input data is done digit by digit from least significant digit to most significant digit and it uses counting sort as a subroutine to perform sorting. Counting sort is a linear sorting algorithm with overall time complexity Θ(N+K) in all cases, where N is the number of elements in the unsorted array and K is the range of input data. The idea of radix sort is to extend the counting sort algorithm to get a better time complexity when K goes up.
Example:
To understand the radix sort, lets consider an unsorted array A = [101, 1, 20, 50, 9, 98, 27, 153, 35, 899] and discuss each step taken to sort the array in ascending order.
Step 1: According to the algorithms, the input data is first sorted based on least significant digit. Therefore, array A[ ] is sorted based on one's digit. After sorting it on one's digit it will become [20, 50, 101, 1, 153, 35, 27, 98, 9, 899].
Step 2: In this step, the array A[ ] array is sorted based on ten's digit and after this step it will become [101, 1, 9, 20, 27, 35, 50, 153, 98, 899].
Step 3: Finally, the array A[ ] is sorted based on hundred's digit (most significant digit) and the array will be sorted after this step and the final array will be [1, 9, 20, 27, 35, 50, 98, 101, 153, 899].
Implementation of Radix Sort
#include <iostream> using namespace std; static void radixsort(int Array[], int n); static void countingsort(int Array[], int n, int place); static void PrintArray(int Array[], int n); // function for radix sort void radixsort(int Array[], int n) { int max = Array[0]; //find largest element in the Array for (int i=1; i<n; i++) { if(max < Array[i]) max = Array[i]; } //Counting sort is performed based on place. //like ones place, tens place and so on. for (int place = 1; max/place > 0; place *= 10) countingsort(Array, n, place); } static void countingsort(int Array[], int n, int place) { int output[n]; //range of the number is 0-9 for each place considered. int freq[10] = {0}; //count number of occurrences in freq array for(int i = 0; i < n; i++) freq[(Array[i]/place)%10]++; //Change count[i] so that count[i] now contains actual //position of this digit in output[] for (int i = 1; i < 10; i++) freq[i] += freq[i - 1]; //Build the output array for (int i = n - 1; i >= 0; i--) { output[freq[(Array[i]/place)%10] - 1] = Array[i]; freq[(Array[i]/place)%10]--; } //Copy the output array to the input Array, Now the Array will //contain sorted array based on digit at specified place for (int i = 0; i < n; i++) Array[i] = output[i]; } // function to print array static void PrintArray(int Array[], int n) { for (int i=0; i<n; i++) cout<<Array[i]<<" "; cout<<"\n"; } // test the code int main (){ int MyArray[] = {101, 1, 20, 50, 9, 98, 27, 153, 35, 899}; int n = sizeof(MyArray) / sizeof(MyArray[0]); cout<<"Original Array\n"; PrintArray(MyArray, n); radixsort(MyArray, n); cout<<"\nSorted Array\n"; PrintArray(MyArray, n); return 0; }
The above code will give the following output:
Original Array 101 1 20 50 9 98 27 153 35 899 Sorted Array 1 9 20 27 35 50 98 101 153 899
Time Complexity:
The time complexity of radix sort is Θ((N+b)*logb(max)) in all cases, where:
- N is the number of elements in unsorted array.
- b is the base of input array, for example, for decimal system, b is 10.
- max is the maximum element of the input array.
Recommended Pages
- C++ Program - To Check Prime Number
- C++ Program - Bubble Sort
- C++ Program - Selection Sort
- C++ Program - Maximum Subarray Sum
- C++ Program - Reverse digits of a given Integer
- C++ - Swap two numbers
- C++ Program - Fibonacci Sequence
- C++ Program - Insertion Sort
- C++ Program - Find Factorial of a Number
- C++ Program - Find HCF of Two Numbers
- C++ Program - Merge Sort
- C++ Program - Shell Sort
- Stack in C++
- Queue in C++
- C++ Program - Find LCM of Two Numbers
- C++ Program - To Check Whether a Number is Palindrome or Not
- C++ Program - To Check Whether a String is Palindrome or Not
- C++ Program - Heap Sort
- C++ Program - Quick Sort
- C++ - Swap Two Numbers without using Temporary Variable