AiTechWorlds
AiTechWorlds
See Counting Sort tally value frequencies and write back in linear O(n+k) time — non-comparison sorting visualized.
def counting_sort(a):
mx = max(a)
count = [0] * (mx + 1)
for x in a: count[x] += 1
idx = 0
for v in range(mx + 1):
while count[v] > 0:
a[idx] = v; idx += 1; count[v] -= 1Counting Sort counts how many times each value occurs, then writes values back in order. It is non-comparison based and runs in linear time when the value range k is small.
| Best | O(n + k) |
| Average | O(n + k) |
| Worst | O(n + k) |
| Space | O(n + k) |
| Stable | Yes |
| In-place | No |
The Counting Sort Visualizer animates how Counting Sort works: it counts how many times each value occurs, then writes the values back in sorted order. It is a non-comparison sort that runs in O(n + k) time, where k is the value range. Counting Sort is stable but needs O(n + k) extra space, so it works best when k is small relative to n.
Generate an array
Click New Array or adjust the size slider for a random dataset of small integers.
Play the animation
Press Play to watch values counted, then written back in order.
Step and scrub
Step forward/back or drag the timeline to follow each write.
Read the synced code
The highlighted pseudocode line tracks the count and output phases; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.