Counting sort is a non-comparative sorting algorithm. It sorts an array by counting occurrences of each unique element. It partially hashes the count of unique elements and then performs calculations to find the index of each element in the final, sorted array. It is quite a fast algorithm but unsuitable for large datasets. It is used as a subroutine inside radix sort.
++ cCopy#include<iostream>
using namespace std;constint N =10;intmaxm(int arr[],int n){int max = arr[0];for(int i =1; i < n; i++){if(arr[i]> max){
max = arr[i];}}return max;}intminm(int arr[],int n){int min = arr[0];for(int i =1; i < n; i++){if(arr[i]< min){
min = arr[i];}}return min;}voidcountingSort(int arr[],int n){int min =minm(arr, n);int max =maxm(arr, n);int range = max - min +1;int count[range]={};int output[n];for(int i =0; i < n; i++) count[arr[i]- min]++;for(int i =1; i < range; i++) count[i]+= count[i -1];for(int i = n -1; i >=0; i--){
output[count[arr[i]- min]-1]= arr[i];
count[arr[i]- min]--;}for(int i =0; i < n; i++) arr[i]= output[i];}intmain(){int n =7;int arr[7]={3,5,1,1,4,2,1};
cout <<"Input arr: ";for(int i =0; i < n; i++){
cout << arr[i]<<" ";}
cout <<"\n";countingSort(arr, n);// Sort elements in ascending order
cout <<"Output arr: ";for(int i =0; i < n; i++){
cout << arr[i]<<" ";}
cout <<"\n";}
This implementation also works for negative numbers.
Counting Sort iterates through all the n items twice and iterates through the count array once. So, it has a time complexity of O(n + b) where b is the range of input. Since b is often small, the counting sort’s time complexity is said to be of the order of [Big Theta]: O(n).
Worst Case
The worst-case time complexity is [Big O]: O(n).
Best Case
The best-case time complexity is [Big Omega]: O(n). It is the same as the worst-case time complexity.
Space Complexity for the counting sort algorithm is O(n+b), where b is the range of input. It comes from count &output arrays. Sometimes b can be larger than n, but if b is small, the time complexity is said to be of O(n).
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Harshit Jindal has done his Bachelors in Computer Science Engineering(2021) from DTU. He has always been a problem solver and now turned that into his profession. Currently working at M365 Cloud Security team(Torus) on Cloud Security Services and Datacenter Buildout Automation.