Menu
Coddy logo textTech

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 v as 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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Counting Sort - DSA Series