Skip to interview prep
Apexed

35. How does counting sort achieve linear time?

MediumSorting

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

For integers in a small range [0, k], count how many times each value occurs, then rebuild the array in order. It runs in O(n + k) without comparisons. Radix sort applies a stable counting sort digit by digit to handle larger numbers.

Example program

How does counting sort achieve linear time — Data structures & algorithms interview question — Apexed