Counting Sort in C

Checking sort calculation is a sorting calculation which doesn’t include the correlation between components of a cluster.

In this instructional exercise, I am sharing including sort program in C. Steps that I am doing to sort the components are given beneath.

As a matter of first importance, I am perusing n components in the cluster a[].

While perusing the cluster components I have likewise determined the greatest component.

Presently the genuine sorting is done in counting_sort() work.

Here I am checking the event of every component in the cluster a[] and afterwards putting away it

at the file equivalent to the estimation of that component. For instance, the event of component 5 will be put away at record 5,

the event of component 9 will be put away at list 9, etc.

As the incentive at each file in count[] exhibit is the event of that record or component,

so the components are imprinted in the rising request by printing each file number of times equivalent to its comparing esteem.

I have likewise included a video instructional exercise beneath that will assist you with understanding the tallying sort calculation effectively.

On the off chance that you are confronting any issue, at that point ask it in the remark segment.

Program for Counting Sort in C

#include<stdio.h>
 
void counting_sort(int a[],int n,int max)
{
     int count[50]={0},i,j;
     
     for(i=0;i<n;++i)
      count[a[i]]=count[a[i]]+1;
      
     printf("\nSorted elements are:");
     
     for(i=0;i<=max;++i)
      for(j=1;j<=count[i];++j)
       printf("%d ",i);
}
 
int main()
{
    int a[50],n,i,max=0;
    printf("Enter number of elements:");
    scanf("%d",&n);
    printf("\nEnter elements:");
                  
    for(i=0;i<n;++i)
    {
     scanf("%d",&a[i]);
     if(a[i]>max)
      max=a[i];
    }
     
    counting_sort(a,n,max);
    return 0;
}

Output

On the off chance that you found any error or anything missing in above instructional exercise for including sort in C at

that point please notice it by remarking underneath.

Leave a Comment

error: Alert: Content is protected!!