Sorting

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Count each key, then rebuild output in key order.

Step 1 / 5
Problem shape
Current lesson

Counting Sort Lab

Sorting Intermediate

Counting Sort

Welcome to the Counting Sort algorithm lab. Count each key, then rebuild output in key order. Use it when: Use when keys are integers in a small known range. The time complexity is O(n + k), and the space complexity is O(k). The pseudocode is: count every value for value from min to max repeat count[value] times output value As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n + k)

Space

O(k)

Pseudocode

count every value
for value from min to max
  repeat count[value] times
    output value
Open deeper structure lesson
1.0x