Most sorting algorithms try to sort a set of items without being given an arbitrary set of values. In contrast, Counting Sort does this differently by only giving you a finite set of values to sort. This is extremely helpful when it comes to run time because more sorting algorithms can only get O(N log N) runtime while Counting Sort can get O(N) runtime.

This is due to the fact that you can sort the finite set on its own and thus just count each instance of a value. This is were Counting Sort derives its name.

Here is an implementation of this where the set of values can only be from 0 to 9:

countSort = (arr) => {

let hash = {}

for(let i of arr) {

if(!hash[i]) {

hash[i] = 1

} else {

hash[i] = hash[i] + 1

}

} let spot = 0;

let count = 0

while(count < 10) {

if(hash[count]) {

arr[spot] = count

hash[count] = hash[count] - 1

} else {

count++

}

}

return arr

}

This application can be used in more ways than just with numbers. As long as the set is finite you can use Counting Sort. This will speed up your programs and make things run smoother.