Counting Sort Visualizer

Learn Counting Sort by watching frequency counts and output placement evolve with interactive controls and step-by-step explanations.

Current status: Reading the next input value and locating its count bucket.

Comma-separated numbers, up to 20 values.

Balanced

Live operation

current

Step 1 / 34
5
[0]
3
[1]
8
[2]
3
[3]
9
[4]
1
[5]
0
[6]
4
[7]
Input valueCount cellOutput placementSorted

Count array

Stable output

Comparisons

0

Moves / writes

0

Confirmed sorted

0 / 8

Algorithm notes

What to watch for

Counting Sort avoids value-to-value comparisons. It is efficient when the range of integer keys is reasonably small, trading extra count-array memory for linear-style passes.

Concept guide

Review the mental model, tradeoffs, and practical use cases after you experiment.

Counting Sort Complete Info Card

Non-ComparisonStable

Counting Sort is a non-comparison based sorting algorithm that works by counting occurrences of each unique element, then calculating positions to reconstruct the sorted array. Ideal for integer data with limited range.

Algorithm Characteristics

Time Complexity (Best)

Linear time performance

O(n + k)

Time Complexity (Average)

Consistent performance

O(n + k)

Time Complexity (Worst)

Depends on input range

O(n + k)

Space Complexity

Auxiliary count array needed

O(n + k)

Stable

Maintains relative order

Yes

In-Place

Requires extra memory

No

Sorting Process Steps

1

Find maximum element in array

2

Create count array of size k

3

Store element counts in count array

4

Convert count array to cumulative sums

5

Build output array using count positions

Key Features & Considerations

FeatureAdvantagesLimitations
Non-Comparison BasedBeats O(n log n) limitOnly works with integers
Stable SortPreserves original orderRequires careful implementation
Range DependentExcellent for small rangesInefficient for large k
Integer KeysPerfect for counting dataNot for floating points
Linear TimePredictable performanceMemory intensive for large k

Optimal Use Cases

  • Sorting integers with limited range
  • When stability is required
  • As subroutine for Radix Sort

When to Avoid

  • Sorting non-integer data
  • Large value ranges (k >> n)
  • Memory-constrained environments
Pro Tip: Counting Sort shines in specialized scenarios and is often used as a building block for Radix Sort. Use it when sorting numbers with a range not much bigger than the number of elements.
Linear timeStable sortInteger sorting