Counting Sort

easy
Unit 4 · Sorting & Two Pointers0/3 to milestone

When values are small integers, sorting can beat O(n log n). Implement a stable counting_sort(items, key, max_key) where key(item) returns an integer in 0..max_key:

  1. count each key
  2. turn counts into starting positions (prefix sums)
  3. place items into the output in input order

Return a new list.

#sorting#linear-time2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Counting Sort · TechGradify