Counting Sort
A non-comparison sorting algorithm that achieves O(n+k) time by counting occurrences. Perfect when the range of values (k) is not significantly larger than n.
Counting Sort
A non-comparison sorting algorithm that achieves O(n+k) time by counting occurrences. Perfect when the range of values (k) is not significantly larger than n.
Initialize count array with zeros
Sort without comparing
Order the steps of a stable counting sort.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
When is Counting Sort most efficient?
The short version
Imagine sorting a deck of cards by suit. Instead of comparing cards, you count how many of each suit you have, then deal them out in order. No comparisons needed!
In more depth
Three Phases
1. Count Phase: Count occurrences of each value
2. Cumulative Phase: Convert counts to positions
3. Output Phase: Place elements in correct positions
Stability
Iterating backwards during output phase ensures stability (equal elements maintain relative order).
Limitations
When to reach for this
- Values are integers in a known, limited range
- Need linear O(n) sorting
- Sorting characters, grades, or small integers
- As a subroutine in Radix Sort
Interview Questions
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required