Description: Radix sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. It groups numbers by each digit, starting from the least significant digit (LSD) to the most significant digit (MSD), or vice versa. Radix sort is particularly effective for sorting large sets of integers or strings.
Here's a C++ implementation of radix sort using counting sort as a subroutine:
#include <iostream>
#include <vector>
using namespace std;
// A function to do counting sort based on the digit represented by exp
void countingSort(vector<int>& arr, int n, int exp) {
vector<int> output(n); // output array
int count[10] = {0}; // count array for digits (0-9)
// Count occurrences of each digit in exp
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
// Update count[i] so that count[i] now contains the actual position of this digit in output[]
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the 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 the output array to arr[], so that arr[] now contains sorted numbers according to the current digit
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
// The main function to implement radix sort
void radixSort(vector<int>& arr) {
int n = arr.size();
// Find the maximum number to know the number of digits
int maxVal = *max_element(arr.begin(), arr.end());
// Apply counting sort for each digit
for (int exp = 1; maxVal / exp > 0; exp *= 10) {
countingSort(arr, n, exp);
}
}
int main() {
vector<int> arr = {170, 45, 75, 90, 802, 24, 2, 66};
radixSort(arr);
cout << "Sorted array: ";
for (int i : arr) {
cout << i << " ";
}
cout << endl;
return 0;
}
Function Definitions:
countingSort: A helper function that sorts the array based on the digit represented by exp.radixSort: The main function that sorts the array using radix sort.Counting Sort Process:
countingSort function counts how many times each digit appears and calculates their positions in the output array.Radix Sort Logic:
radixSort function first finds the maximum value in the array to determine how many digits need sorting.Output: After sorting, the program prints the sorted array.
Radix sort is an efficient sorting algorithm for large datasets of integers or strings with a fixed number of digits. Its linear time complexity for certain datasets makes it faster than comparison-based algorithms for sorting numbers. Radix sort's stability also makes it suitable for applications where the relative order of equal elements must be preserved. However, it may require additional memory for counting and output arrays.
Open this section to load past papers