Reading n items and initializing k counters is necessary: Θ(n+k).
Sorting algorithms · 5/7
Counting sort
Replaces comparisons with counts of integer keys.
How it works
Counts become cumulative positions; scanning input right to left creates stable output.
Step-by-step visualization
Each bar represents one element. Colors identify compared elements, the pivot or key, the final region and the active range.
Use 2 to 16 values from 0 to 999, spanning a range no wider than 80.
- Comparisons
- 0
- Writes
- 0
- Swaps
- 0
- Pass / level
- 0
Pseudocode linked to the visualization
C[0 … k] ← 0for value in A: C[value]++for i ← 1 to k: C[i] ← C[i] + C[i − 1]for i ← n − 1 downto 0B[C[A[i]] − 1] ← A[i]C[A[i]]--copy B into A
The highlighted line corresponds to the operation described by the visualizer. Index-management details are intentionally simplified.
Complexity analysis
Input order does not change the passes: Θ(n+k).
Worst case performs the same linear passes.
Θ(k) counters and Θ(n) stable output: Θ(n+k), not in place.
Properties at a glance
Stability concerns the relative order of elements with equal keys.
This classification refers to the version shown on this page.
When to use it and what to avoid
A good choice when…
Excellent for integers whose range k is not much larger than n.
Common mistake
A huge max−min range wastes time and memory. This visualizer accepts nonnegative integers.