Radix Sort is an algorithm that sorts a sequence of integers in non-decreasing order in time and space.
Tip
This problem can also be solved by Selection Sort in time and space.
Tip
This problem can also be solved by Insertion Sort in time and space.
Tip
This problem can also be solved by Bubble Sort in time and space.
Tip
This problem can also be solved by Merge Sort in time and space.
Tip
This problem can also be solved by Heapsort in time and space.
Tip
This problem can also be solved by Quicksort in time and space.
Tip
This problem can also be solved by Counting Sort in time and space.
Algorithm
Lemma
- Sort based on recursively.
- Apply Counting Sort to stably sort based on .
This algorithm solves the problem in time and space.
void radix_sort(int n, int m, int d, std::vector<int> &a) {
for (int i = 0, k = 1; i < d; i++, k *= m) {
std::vector cnt(m, 0);
for (int j = 0; j < n; j++) {
cnt[a[j] / k % m]++;
}
std::exclusive_scan(cnt.begin(), cnt.end(), cnt.begin(), 0);
std::vector<int> b(n);
for (int j = 0; j < n; j++) {
b[cnt[a[j] / k % m]++] = a[j];
}
std::swap(a, b);
}
}