Counting sort

medium

Tally the values, turn the tallies into positions with a prefix sum, then place each element directly. The comparison counter stays at zero for the whole run - this is not bound by the Ω(n log n) lower bound.

Best O(n + k)Average O(n + k)Worst O(n + k)Space O(n + k)StableNeeds extra memory
Loading the counting sort engine…

Where to go next