Radix sort is a non-comparative sorting algorithm. While comparison-based sorts like QuickSort or MergeSort are limited to $O(n \log n)$, Radix Sort can achieve $O(n \cdot k)$ performance (where $k$ is the number of bits/digits), making it one of the fastest ways to sort large datasets in games.
Radix sort avoids comparisons by grouping keys by individual digits that share the same position and value. It typically uses Counting Sort as a stable subroutine to process each “digit” or “byte.”
Array: [170, 45, 75, 90, 802, 24, 2, 66]
[170, 90, 802, 2, 24, 45, 75, 66][802, 2, 24, 45, 66, 170, 75, 90][2, 24, 45, 66, 75, 90, 170, 802]void CountingSort(std::vector<int>& arr, int exp) {
int n = arr.size();
std::vector<int> output(n);
int count[10] = {0};
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
arr = output;
}
void RadixSort(std::vector<int>& arr) {
int m = *std::max_element(arr.begin(), arr.end());
for (int exp = 1; m / exp > 0; exp *= 10)
CountingSort(arr, exp);
}