Counting Sort Algorithm Explained with Examples

Counting Sort Algorithm Explained with Examples

We have seen sorting algorithms in the previous article. In this article, we are going to discuss the Counting Sort Algorithm.

When comparing elements, no sorting algorithm can sort n elements in less than O(n log n) time. Fortunately, if we know some information about the elements ahead of time, we can sort them in other ways. Assume we are asked to sort n elements but are told that each element is in the range 0-k, where k is much smaller than n. We can use the situation to create a linear O(n) sorting algorithm. That’s the Counting type.

What is a Counting Sort Algorithm?

A counting sort (ultra sort or math sort) is a sorting algorithm that sorts an array’s elements by calculating the number of occurrences of each distinct array element. The count is stored in an auxiliary array, and sorting is accomplished by mapping the count to an index in the auxiliary array.

Counting sort uses the range (k) of the numbers in the array arr to be sorted. It makes an array of this length using this range. Each index i in array Bucket is then used to count how many elements in arr have the value i the counts stored in _Bucket _can then be used to place the elements in arr in their proper order in the resulting sorted array. Harold H. Seward invented the algorithm in 1954.

How does it work?

There are three phases in this algorithm.

  1. Counting the Elements
  2. Aggregating the Histogram
  3. Writing Back Sorted Objects

Counting the Elements

The array below must be sorted.

Find the largest element (let it be max) in the given array. Create an array of length max+1 with all elements set to 0 (When we initialize an array, all of its elements will take 0 by default). This array is used to keep track of the array’s element count. Here, max=9, therefore the array size created is 10. The array index is shown below the line in the diagram.

sorting-algorithms computer-science arrays

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Selection Sort Algorithm Visually Explained

The Selection Sort algorithm sorts an array by looking for the smallest item and moving it to the front of the list. That’s really all you…

Bubble Sort Algorithm Demystified

As [Bubble Sort]( "Bubble Sort") represents the basic foundations of sorting, we've tried to demystify this sorting algorithm.

Heap Sort Algorithm Visually Explained

Heap Sort Algorithm Visually Explained - Now that we know how to create a max-heap, we’ll move into sorting an array of values using heap sort. To sort the array using heap sort…

How to deal with sorting algorithms: Merge Sort

Speaking about sorting algorithms, one should always remember: there is more than one approach. Last time we talked about Insertion Sort and worked with one unsorted array. This time we’re going to try a different approach and more complicated pattern — Merge Sort. Our starter pack is two sorted arrays, our task is to combine them into one sorted array.

Topological Sorting Visually Explained

Topological Sorting Visually Explained - Topological sorting is the result you get when you sort the tree in order of decreasing finishing times. What this means is that you run…