The counting sort

The counting sort is the first distribution sort we will learn about in this book. Distribution sort algorithms use auxiliary data structures (known as buckets) that are organized and then merged, resulting in the sorted array. The counting sort uses a temporary array that will store how many times each element appears in the original array. After all the elements are counted, the temporary array is sorted and it can be iterated to construct the resultant sorted array.

It is a good algorithm to sort integers (it is an integer sorting algorithm) with complexity O(n + k), where k is the size of the temporary counting array; however, it does require more memory for the temporary array.

The following code represents the counting ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.