# Counting sort Algorithm in C++

## Topics Covered

• Algorithm for counting sort
• C program to implement counting sort
• C++ program to implement counting sort

## Algorithm for counting sort

``````countingsort(array, size)
max <- find largest element in array
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique element and
store the count at jth index in count array
for i <- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1
exit program``````

### C program to implement count sort

``````#include <stdio.h>
#include <conio.h>

void countingSort(int array[], int size) {
int output[10];
// Find the largest element of the array
int maximum = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > maximum)
maximum = array[i];
}
/* The size of count must be at least (max+1) but we cannot declare it as int count(max+1) in C as it does not support dynamic memory allocation.  So, its size is provided statically.*/

int count[10];
// Initialize count array with all zeros.
for (int i = 0; i <= maximum; ++i) {
count[i] = 0;
}
// Store the count of each element
for (int i = 0; i < size; i++) {
count[array[i]]++;
}
// Store the cummulative count of each array
for (int i = 1; i <= maximum; i++) {
count[i] += count[i - 1];
}
/* Find the index of each element of the original array in count array, and place the elements in output array */
for (int i = size - 1; i >= 0; i--) {
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
// Copy the sorted elements into the original array
for (int i = 0; i < size; i++) {
array[i] = output[i];
}
}
// Function to print the array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d  ", array[i]);
}
printf("\n");
}
// Driver code
int main() {
int array[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(array) / sizeof(array[0]);
countingSort(array, n);
printArray(array, n);
}``````

### C++ program to implement count sort

``````#include<iostream>
#include<iostream>
using namespace std;

// Applying function to implement counting sort.
void CountingSort(int a[], int n, int r, int lower)
{
int i, j = 0, count[r] = {0};
// Counting the number of occurrence of each element in the array.
for(i=0; i<n; i++)
count[a[i]-lower]++;

i=0;
// placing the elements back into the array.
while(i < r)
{
flag:
a[j] = lower+i;
j++;
count[i]--;

// place the same element until its counter is equal to zero.
if(count[i] > 0)
goto flag;

i++;
}
}

int main()
{
int n, i, range, upperlimit, lowerlimit;
cout<<"\nEnter the size of the array to be sorted: ";
cin>>n;

cout<<"\nEnter the lower and upper limit of the data to be entered: ";
cin>>lowerlimit>>upperlimit;

// Range of the input data.
range = upperlimit-lowerlimit+1;

// Apply loop for taking the elements in the array
int array[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>array[i];
}

CountingSort(array, n, range, lowerlimit);

// Printing the final sorted array.
cout<<"\nThe final sorted array is :  "<< endl;
for (i = 0; i < n; i++)
cout<<" "<<array[i];

return 0;
}``````

We will be happy to hear your thoughts
Hello!๐