Tuesday, 17 September 2013

Bucket Sort

HTML Online Editor Sample

 

#include<stdio.h>

#include<stdlib.h>
#include<unistd.h>
int buccket[100];
void buccketsort(int *a ,int n)
{
int i;
for(i=0;i<n;i++)
buccket[a[i]]++;
}
int main()
{
int *a,n,i;
printf("\n Enter The size of the Data To Sort\n");
scanf("%d",&n);
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
a[i]=rand()%(100);
struct timeval tv_start,tv_end;
gettimeofday(&tv_start,NULL);
buccketsort(a,n);
gettimeofday(&tv_end,NULL);
 
 
for(i=0;i<100;i++)
{
while(buccket[i]--)
printf("%d ",i);
 
}
//printf("%d ",a[i]);
printf("\n---------------------\n Time Taken is %g \n",((tv_end.tv_sec-tv_start.tv_sec)+(tv_end.tv_usec-tv_start.tv_usec)*0.000001));
return 0;
}

No comments:

Post a Comment