Value-range-based stable sorting walkthrough using buckets, prefix sums, and output placement.
Easy Explanation
Counting Sort counts how many times each value appears, turns those counts into positions, then places values in order.
Renderer Mode
`Simple` uses abstract, short-form-friendly visuals. `Advanced` keeps the current detailed view.
Input
0 values
Range
0 buckets
Placements
0
Writes
0
Press Play or Step to start execution.
Input
Count Buckets
Output
Write Back
function countingSort(values):
minValue <- min(values)
maxValue <- max(values)
counts <- array(maxValue - minValue + 1, fill 0)
output <- array(length(values))
for value in values:
counts[value - minValue] <- counts[value - minValue] + 1
for i from 1 to length(counts) - 1:
counts[i] <- counts[i] + counts[i - 1]
for i from length(values) - 1 down to 0:
value <- values[i]
bucket <- value - minValue
counts[bucket] <- counts[bucket] - 1
output[counts[bucket]] <- value
return output