Archive for the ‘Code Collection’ Category

C program for counting sort

Posted: February 9, 2012 in Code Collection
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *input,i,j,n,*arr,max;
printf("Enter the no. of elements\n");
scanf("%d",&n);
arr=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",(arr+i));
}
max=*(arr+0);
for(i=0;i<n;i++)
{
if(*(arr+i)>max)
max=*(arr+i);
}
input=(int*)calloc(sizeof(int),(max+1));
for(i=0;i<n;i++)
(*(input+*(arr+i)))++;


for(i=0;i<=max;i++)
for(j=0;j<*(input+i);j++)
printf("%d\n",i);

return 0;
}