How it works?
Lesson 3 of 9 in Coddy's Counting Sort - DSA Series course.
Counting Sort works in two phases: count, then rebuild.
Step-by-Step Process:
- Count: make a
countarray wherecount[v]is how many times the valuevappears. - Rebuild: walk the values from smallest to largest and write each value
vinto the outputcount[v]times.
Example on [4, 2, 2, 8, 3, 3, 1]:
- Count each value: 1 appears once, 2 twice, 3 twice, 4 once, 8 once.
- Rebuild in order: [1, 2, 2, 3, 3, 4, 8].
No comparisons were made: the value itself decided where each element goes.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Counting Sort - DSA Series
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)