Counting sort is one of the most famous sorting algorithms. It performs some arithmetic calculations in order to get the perfect output sequence. It can be applied to all the values that we can use as an index.
Consider the following algorithm:
The above algorithm is illustrated in the example below:
Let’s implement the above algorithm.
#include<iostream>#include<algorithm>using namespace std;// function for printing an array.void printArr(int *array, int size) {for(int i = 0; i < size; i++)cout << array[i] << " ";cout << endl;}// function for calculating maximum element.int findMax(int array[], int size) {int max = array[0];for(int i = 1; i < size; i++) {if(array[i] > max)max = array[i];}return max;}// function for the count sort algorithmvoid countSort(int *array, int size) {int output[size];int max = findMax(array, size);int count[max+1];for(int i = 0; i <= max; i++)count[i] = 0; //initialize frequencies array to all zerofor(int i = 0; i < size; i++)count[array[i]]++; //fill the array with the count of each numberfor(int i = 1; i <= max; i++)count[i] += count[i-1]; //find cumulative frequencyfor(int i = size - 1 ; i>=0; i--) {output[count[array[i]]-1] = array[i];count[array[i]] -= 1; //decrease count for same numbers}for(int i = 0; i < size; i++) {array[i] = output[i]; //store output array to main array}}int main() {int arr[] = {1, 5, 9, 8, 1, 3};int n = 6;cout << "Array before Sorting: ";printArr(arr, n);countSort(arr, n);cout << "Array after Sorting: ";printArr(arr, n);}
In the above code:
printArr
function to print the array.findMax
function to find the maximum value of the array.max+1
.output
array.output
array in the input
array.We can see one drawback here, that if the minimum value is , the first indexes of the frequency array will not be used and it will increase the space complexity. Let’s do a little trick here.
Now we can handle the negative integer values as well. See the following code:
#include<iostream>#include<algorithm>using namespace std;// function for printing an array.void printArr(int *array, int size) {for(int i = 0; i<size; i++)cout << array[i] << " ";cout << endl;}// function for calculating maximum element.int findMax(int array[], int size) {int max = array[0];for(int i = 1; i < size; i++) {if(array[i] > max)max = array[i];}return max;}// function for calculating minimum element.int findMin(int array[], int size) {int min = array[0];for(int i = 1; i < size; i++) {if(array[i] < min)min = array[i];}return min;}// function for the count sort algorithmvoid countSort(int *array, int size) {int output[size];int min = findMin(array, size);for(int i = 0; i < size; i++)array[i]-=min; // subtracting minimum value from the array to start range from 0int max = findMax(array, size);int count[max+1];for(int i = 0; i<=max; i++)count[i] = 0; //initialize frequencies array to all zerofor(int i = 0; i < size; i++)count[array[i]]++; //fill the array with the count of each numberfor(int i = 1; i<=max; i++)count[i] += count[i-1]; //find cumulative frequencyfor(int i = size - 1 ; i>=0; i--) {output[count[array[i]]-1] = array[i];count[array[i]] -= 1; //decrease count for same numbers}for(int i = 0; i < size; i++) {array[i] = output[i] + min; //store output array to main array with the actual values}}int main() {int arr[] = {1, 5, 9, 8, -1, -3};int n = 6;cout << "Array before Sorting: ";printArr(arr, n);countSort(arr, n);cout << "Array after Sorting: ";printArr(arr, n);}
In the above playground:
findMin
function to find the minimum value.min
value from each element of the array.min
value to the sorted array.We can see two different loops here. One till the number of elements in the array () and the other till the range (). So, This algorithm works in .
We are creating two extra arrays. One to store the sorted array () and the other to store the frequency of each element in the range (). So, the space complexity is .
Free Resources