Code on a screen, representing counting sort.

Sorting by counting how many of each value.

What counting sort is

Counting sort is a sorting algorithm that orders items by counting how many times each possible value appears, then using those counts to place every item in its correct position. Unlike many sorting methods, it does not work by comparing items to each other. Instead, it relies on tallying occurrences, which lets it sort very quickly when the range of possible values is limited.

Counting occurrences

The first step of counting sort is to go through the items and count how many times each distinct value occurs. These counts are recorded, giving a tally for every possible value. This counting pass gathers all the information the algorithm needs about how the values are distributed, without any direct comparisons between the items themselves.

Placing items in order

Using the counts, counting sort can determine exactly where each value should go in the sorted output. Because it knows how many items have each value, it can figure out the correct positions and place the items accordingly. The result is a fully sorted arrangement produced from the counts rather than from comparing pairs of items.

Fast for small ranges

Counting sort is very fast when the number of distinct possible values is small relative to the number of items, because the counting and placing steps are efficient. However, if the range of possible values is very large, the approach needs more space for the counts and becomes less practical. It works best when values fall into a limited range.

Why counting sort matters

Counting sort matters because it shows that sorting does not always require comparing items, and it can be extremely fast in the right conditions. It is useful when values come from a small, known range. Understanding counting sort clarifies how tallying occurrences and using those counts can order data quickly without comparisons.

Why it matters

Counting sort is a notable non-comparison sorting algorithm. Understanding what it is - counting each value and using the counts to place items - clarifies an efficient alternative to comparison-based sorting. For anyone interested in computer science, it is a valuable concept to understand.

See also: What is a sorting algorithm? Explained, What is radix sort? Explained.

Sources

Published date reflects the original event date (2026-10-19). This article is original Skillo editorial written from the sources above; facts were verified in September 2026.

Skillo Staff

Skillo Staff

All stories by: Skillo Staff

Leave a Reply