DSA Lab

Counting sort visualizer

Count how many times each value appears, then write each value out as many times as it was counted. It never compares two items. Whole numbers in a small range k (exam marks, ages, digits). Not for large ranges or for decimals.

Counting sort: its cost, and how to use this tool

Time: best O(n + k), average O(n + k), worst O(n + k). Extra memory: O(k). Stable, not in place.

Sort a ready-made input or your own numbers. The bars are the array; the code beside them (in Python, C, C++ or Java) marks the line that just ran, and every step is explained and worked out below the bars.

Lessons that use it

  • Heap sort and counting sort (Algorithms)Heap sort: O(n log n) in place. Counting sort: O(n + k) without comparing, and why comparison sorts cannot beat n log n.

See also