Counting Sort AlgorithmIn this article, we will discuss the counting sort Algorithm. Counting sort is a sorting technique that is based on the keys between specific ranges. In coding or technical interviews for software engineers, sorting algorithms are widely asked. So, it is important to discuss the topic. This sorting technique doesn't perform sorting by comparing elements. It performs sorting by counting objects having distinct key values like hashing. After that, it performs some arithmetic operations to calculate each object's index position in the output sequence. Counting sort is not used as a general-purpose sorting algorithm. Counting sort is effective when range is not greater than number of objects to be sorted. It can be used to sort the negative input values. Now, let's see the algorithm of counting sort. AlgorithmWorking of counting sort AlgorithmNow, let's see the working of the counting sort Algorithm. To understand the working of the counting sort algorithm, let's take an unsorted array. It will be easier to understand the counting sort via an example. Let the elements of array are - 1. Find the maximum element from the given array. Let max be the maximum element. 2. Now, initialize array of length max + 1 having all 0 elements. This array will be used to store the count of the elements in the given array. 3. Now, we have to store the count of each array element at their corresponding index in the count array. The count of an element will be stored as - Suppose array element '4' is appeared two times, so the count of element 4 is 2. Hence, 2 is stored at the 4th position of the count array. If any element is not present in the array, place 0, i.e. suppose element '3' is not present in the array, so, 0 will be stored at 3rd position. Now, store the cumulative sum of count array elements. It will help to place the elements at the correct index of the sorted array. Similarly, the cumulative count of the count array is - 4. Now, find the index of each element of the original array After placing element at its place, decrease its count by one. Before placing element 2, its count was 2, but after placing it at its correct position, the new count for element 2 is 1. Similarly, after sorting, the array elements are - Now, the array is completely sorted. Counting sort complexityNow, let's see the time complexity of counting sort in best case, average case, and in worst case. We will also see the space complexity of the counting sort. 1. Time Complexity
In all above cases, the time complexity of counting sort is same. This is because the algorithm goes through n+k times, regardless of how the elements are placed in the array. Counting sort is better than the comparison-based sorting techniques because there is no comparison between elements in counting sort. But, when the integers are very large the counting sort is bad because arrays of that size have to be created. 2. Space Complexity
Implementation of counting sortNow, let's see the programs of counting sort in different programming languages. Program: Write a program to implement counting sort in C language. Output After the execution of above code, the output will be - Program: Write a program to implement counting sort in C++. Output After the execution of above code, the output will be - Program: Write a program to implement counting sort in C#. Output After the execution of above code, the output will be - Program: Write a program to implement counting sort in Java. Output Program: Write a program to implement counting sort in PHP. Output So, that's all about the article. Hope the article will be helpful and informative to you. This article was not only limited to the algorithm. We have also discussed counting sort complexity, working, and implementation in different programming languages. Next TopicHeap Sort |