← Divide & Conquer

Micro-Drill #75 — Counting sort

Divide & Conquer Target: 15s

Counting sort is O(n+k) for bounded integers. Optimal when the range is small.

count = [0] * (max(a) + 1)
for x in a:
    count[x] += 1
result = []
for val, cnt in enumerate(count):
    result.extend([val] * cnt)

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #74 Micro #76 →