Back In the print window choose "Save as PDF" as the destination
NOTE.CS

Counting Sort

1 topic · 28 Nov 2026
1

Counting Sort

Algorithm › Sort

Non-comparison sort for small integer ranges: count each value, then write them back in order. O(n + k) where k is the value range.

C++
void countingSort(vector<int> &a, int maxVal) {
    vector<int> cnt(maxVal + 1, 0);
    for (int x : a) cnt[x]++;
    int idx = 0;
    for (int v = 0; v <= maxVal; v++)
        while (cnt[v]--) a[idx++] = v;
}