#include<iostream>#include<vector>#include<algorithm>// Function to get the digit at the dth positionunsignedgetDigit(unsigned number, int d, int radix){
return (number / static_cast<unsigned>(pow(radix, d))) % radix;
}
// Counting sort for a specific digitvoidcountingSortByDigit(unsigned* a, int n, int digit, int radix){
std::vector<int> count(radix, 0);
std::vector<unsigned> output(n);
// Count occurrences of each digitfor (int i = 0; i < n; i++) {
count[getDigit(a[i], digit, radix)]++;
}
// Cumulative countfor (int i = 1; i < radix; i++) {
count[i] += count[i - 1];
}
// Build the output arrayfor (int i = n - 1; i >= 0; i--) {
output[count[getDigit(a[i], digit, radix)] - 1] = a[i];
count[getDigit(a[i], digit, radix)]--;
}
// Copy the output array to the original arrayfor (int i = 0; i < n; i++) {
a[i] = output[i];
}
}
// Radix sort functionvoidsort(unsigned* a, int n){
constint radix = 256; // Using 8 bits per digitconstint numDigits = 4; // 32 bits / 8 bits per digit// Sort for each digitfor (int digit = 0; digit < numDigits; digit++) {
countingSortByDigit(a, n, digit, radix);
}
}