Pseudo Code
Lesson 4 of 9 in Coddy's Counting Sort - DSA Series course.
countingSort(array):
max = largest value in array
count = array of (max + 1) zeros
for each x in array: # count phase
count[x] += 1
result = [] # rebuild phase
for v from 0 to max:
repeat count[v] times:
append v to result
return result- count[v] uses the value
vas an index, so the value range decides the size of the count array. - The count phase tallies every value in one pass.
- The rebuild phase emits each value the right number of times, in increasing order, producing a sorted array.
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)