June 17, 2024
c-program c ++

C or C++ Program for Counting Sort

This article describes about the program for counting sort in C/C++. Let us discuss about the counting sort.

What is Counting Sort?
Counting sort is a sorting technique based on keys between specific ranges. It works by counting the number of objects having distinct key values. It performs some arithmetic operation and calculates the position of each object in the output sequence.
It works by counting the occurrences of each data value. It imagines that there are n data items in the range of 1…m (m is an integer). For each input element the algorithm can then determine, the amount of elements less than it. For example if there are 8 elements less than element x, then x belongs in the 9th data position.

Counting Sort Program


find_k(int a[],int n) {
     int hight=a[1];
     for(int i=2;i<=n;i++)
     return hight;

void counting_sort(int a[],int b[],int n) {
     int c[300];
     int k=find_k(a,n);
     for(int i=0;i<=k;i++)
     for(int j=1;j<=n;j++)
     for(i=1;i<=k;i++) 	c[i]=c[i]+c[i-1];      for(j=n;j>=1;j--) {

void main() {
int a[34],b[23],n;

     cout<<"     \" This will arrange data in ascending order (using counting sort) .\""<<endl<<endl;
     cout<<"How many number ?. give a integer number."<<endl;      cin>>n;
     cout<<"What are the elements ?. Give the numbers."<<endl;

     for(int i=1;i<=n;i++) 	cin>>a[i];

     cout<<"You have given the following numbers :"<<endl;
	cout<<a[i]<<"  ";

     cout<<"After sorting the values are.......\n";
	cout<<b[i]<<"  ";


Rashedul Alam

I am a software engineer/architect, technology enthusiast, technology coach, blogger, travel photographer. I like to share my knowledge and technical stuff with others.

View all posts by Rashedul Alam →

One thought on “C or C++ Program for Counting Sort

Leave a Reply

Your email address will not be published. Required fields are marked *