SortingAnimated
Counting Sort
Count occurrences of each small integer, then rebuild the list.
Time
O(n + k)
Extra space
O(k)
Use it for
Integers in a small known range.
How it works
- No comparisons at all. Works for small whole numbers.
- Count how many times each value appears.
- Turn the counts into running totals: the total for value v tells how many items are ≤ v, i.e. where the last v goes.
- Walk the input backwards, put each item at its position and decrement the total. Walking backwards keeps equal items in order (stable).
Watch it run
Loading animation…