All topics
39

Sorting whole numbers by counting them

Tier 1Locked

Where it runs

1. The problem

The sorts you have seen so far all compare pairs. They ask whether one number is bigger than another, and the answer decides whether to swap them or move on. Every comparison sort is built on that question, and because of it no comparison sort can do better than about n log n work.

Now a different situation. You have a lot of numbers, but they are all whole numbers from a small range. Ages from 0 to about 120. Star ratings from 1 to 5. Test scores from 0 to 100. When the values lie in a range that small, you can sort without comparing anything.

You have 100,000 survey responses, each with a rating from 1 to 5 stars. You want them ordered from 1 star up to 5 stars. Comparing the responses with each other is wasted work: there are only 5 different values.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
Counting sort: tally the keys, then read them out
countingSort([3, 1, 4, 1, 0, 3, 2], 4)
arr
3
1
4
1
0
3
2
counts (index = key, cell = how many)
0
0
0
0
0
out
empty

Every key is between 0 and 4, so make 5 counters, one per possible key.

Speed race
Counting sort and merge sort on the same 40 numbers from 1 to 8

One operation is one comparison, one read or one write. Every frame, each lane does the same number of operations, so the slow one falls behind.

Counting sort O(n + k)
0 operations
Merge sort O(n log n)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 1,000
Counting sort2K · under 1 ms
Merge sort19.9K · under 1 ms
n = 100,000
Counting sort200K · 2 ms
Merge sort3.3M · 33 ms
n = 10,000,000
Counting sort20M · 200 ms
Merge sort465.1M · 4.7 s

Recognise it

Non-comparison sort (counting / radix)

Say these words and it is this pattern

  • keys are integers in a small known range
  • sort ages, grades, or bounded counts
  • need O(n) and comparisons are the bottleneck
  • sort by digit or fixed-width key
  • n large but the value range is much smaller than n

Looks similar, isn't

n values with keys under k gives O(n + k). Radix sort costs O(d times n) across d digits.

On LeetCode

Problems where the values are whole numbers from a small range, like heights up to 100 or citations capped at n. Count each value, then write the values out in order, with no comparisons.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy