Loading

C Program for Counting Sort

C - Program for Counting Sort


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

void main()
{
   int a[20],b[20],c[1000],n,i,j,k=1000;
   clrscr();
   printf("\nHow many Numbers Do u want to sort :: ");
   scanf("%d",&n);

   printf("\nEnter the Numbers :: ");
   for(i=1;i<=n;i++)
   {
    scanf("%d",&a[i]);
   }
  
 /* counting sort starts here */
   for(i=0;i<k;i++)
    c[i]=0;
   for(j=1;j<=n;j++)
    c[a[j]]=c[a[j]]+1;
   for(i=1;i<k;i++)
    c[i]=c[i]+c[i-1];
   for(j=n;j>=1;j--)
   {
    b[c[a[j]]]=a[j];
    c[a[j]]=c[a[j]]-1;
   }

   printf("\nThe Sorted Numbers :: ");
   for(i=1;i<=n;i++)
    printf(" %d ",b[i]);
   getch();
}

This entry was posted in . Bookmark the permalink.

Leave a reply