Sorting is one of the most basic things that every programmer must learn and understand to get a job. In this article, we will study the Counting Sort, its Algorithm, and its implementation in Python. So, let’s get started!
What is Counting Sort?
First, we all know what sorting is. It is the process of ranking elements either in ascending or descending order from the collection of elements. There are many Sorting Algorithms available to sort the data in an array or list and some of them are Bubble sort, Selection sort, Insertion sort, Merge sort, etc.
Counting sort is a sorting algorithm that sorts the elements with the technique of counting the number of occurrences of each unique element in an array or list. It is basically a hashing technique with the keys between a specific range and then counting the number of objects having distinct key values.
We will get the output sequence by doing some arithmetic calculations for positioning each object using its key values. Counting sort uses the partial hashing technique to count the occurrence of the element in O(1). Let us study an example of counting sort for a clear understanding.
Counting Sort Example
Consider a given array to be sorted. Firstly, we will find the largest element from the array and initialize it as max.
Now we will initialize the new array named count array with length “max+1” with all the elements 0 to store the sorted data.
Later, we will store elements of the given array with the corresponding index in the count array as shown in the figure.
Now, we will modify the count array by adding the previous counts and creating the cumulative sum of an array as shown below:
Since we have 7 inputs in the original array, we will create another empty array with 7 places to store the sorted data and place the elements in their correct positions and decrease the count by one.
Therefore, the sorted array is:
Algorithm for Counting Sort
Here is the complete algorithm to implement Counting Sort:
Python Code for Counting Sort
Now that you know the algorithm, you can easily do its implementation in Python. Below is the Python code for Counting Sort:
Counting Sort Time Complexity
In the above algorithm, we have 4 loops in total. The first loop is to find the largest element in the given array, the second is to store the count of each element. Third loop stores the cumulative sum of each element in the count array and the fourth loop stores the sorted array in the new array using the indexing method of the count array.
The time complexity of the Counting Sort algorithm is O(m+n) where m is the number of elements in the input array and n is the range of input. In all the cases whether it be the best case, worst case, or average case the time complexity of the algorithm is the same because the time complexity of the counting sort algorithm is not dependent on how many elements we store in the array.
Why is Counting Sort not used?
This sorting method has some drawbacks that make it not so useful despite the fact that it is an efficient technique for sorting a restricted range of integer values. The following are some of the main reasons:
- Memory specifications: In order to store the counting array, that has a size proportional to the variety of the input values, the counting sort needs additional RAM. Due to its propensity to quickly run out of memory, the algorithm is inappropriate for large datasets containing a diverse range of input values.
- Little Application: Counting Only data that fits within a finite range can be sorted. For instance, irrespective of the actual number of elements in the array, if the information is supplied has a maximum value of 100, the counting array is going to have a size of 101. This means that datasets having a range that is too broad or poorly specified cannot be used with the technique.
- Performance: Counting Sort has an O(m+n) time complexity, where m is the input value range and n is the total quantity of elements in the array that is input. Despite having a linear time complexity, the algorithm's performance can suffer with a wide range of input values.
Is Counting Sort space efficient?
The algorithm Counting Sort is not regarded as being space-efficient. The counting array, whose size is proportionate according to the range of input values, requires more memory to be stored. This indicates that the approach is inadequate for huge datasets because its memory consumption grows as the size of the input data does.
The sorting of huge datasets is better suited for algorithms with reduced memory requirements, like Quick Sort and Heap Sort. The number of items in the input array, n, and the range of input values, k, can be used to calculate the space complexity of the counting sort as O(n+k).
Despite having a linear complexity of space, the counting array that must be stored by the algorithm demands additional memory, which may quickly turn into a bottleneck when the input value range is very wide.
Is a Counting Sort stable?
Yes, Counting Sort is a stable algorithm. This indicates that it keeps the input array's equal items' relative positions in the same order. For instance, counting sort will guarantee that x appears before y within the output array if the given input array contains two components x and y that are equal and appear in that order in the input array.
The Counting Sort is useful when we have a finite and limited domain of variables. It is also commonly used when the range of input elements is known in advance. This is because it has linear complexity is required.
By comparing the execution time of Counting Sort with other sorting algorithms, one can assess the efficiency and practicality of those algorithms for particular data distributions and ranges.
So far, we have learned Counting Sort in Python with its algorithm an example, and a program. Besides the linear time complexity, it is very useful for finding the smaller integer with multiple counts. Happy Learning :)