sandbox/Antoonvh/radix.h
Radix sort
Modified from Geeks for Geeks:
https://www.geeksforgeeks.org/radix-sort/
Sorting positive integers, keeping track of their original index.
A linear helper algorithm:
void countSort (int n, int arr[n], int ind[n], int exp) {
int output[n], out_ind[n];
int i, count[10] = {0};
for (i = 0; i < n; i++)
[(arr[i] / exp) % 10]++;
countfor (i = 1; i < 10; i++)
[i] += count[i - 1];
countfor (i = n - 1; i >= 0; i--) {
[count[(arr[i] / exp) % 10] - 1] = ind[i];
out_ind[count[(arr[i] / exp) % 10] - 1] = arr[i];
output[(arr[i] / exp) % 10]--;
count}
for (i = 0; i < n; i++) {
[i] = out_ind[i];
ind[i] = output[i];
arr}
}
User interface
Input:
n , number of elements in array
arr , the array to be sorted (input and output)
Ind , index translation array (output)
void radixsort (int n, int arr[n], int ind[n]) {
int m = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > m)
= arr[i];
m [i] = i;
ind}
for (int exp = 1; m/exp > 0; exp *= 10)
countSort(n, arr, ind, exp);
}