===================================================
BUCKET SORT:
========================================
#include <stdio.h>
#include <iostream.h>
#define m 20
void bucketsort(int *a, int n)
{
int buckets[m];
for(int j=0;j<m;++j)
buckets[j]=0;
for(int i=0;i<n;++i)
++buckets[a[i]];
for( i=0,j=0;j<m;++j)
for(int k=buckets[j];k>0;--k)
a[i++]=j;
for(i=0;i<m;++i)
printf("[%d]",buckets[i]);
}
int main()
{
int n,a[m];
printf("Please enter the number of elements to be sorted:");
scanf("%d",&n);
printf("The elements must be lower than %d:",m);
for(int i=0;i<n;i++)
{
printf("input %d element:",i);
scanf("%d",&a[i]);
}
printf("unsorted list");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
bucketsort(a,n);
printf("sorted list");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
return 0;
}
BUCKET SORT:
========================================
#include <stdio.h>
#include <iostream.h>
#define m 20
void bucketsort(int *a, int n)
{
int buckets[m];
for(int j=0;j<m;++j)
buckets[j]=0;
for(int i=0;i<n;++i)
++buckets[a[i]];
for( i=0,j=0;j<m;++j)
for(int k=buckets[j];k>0;--k)
a[i++]=j;
for(i=0;i<m;++i)
printf("[%d]",buckets[i]);
}
int main()
{
int n,a[m];
printf("Please enter the number of elements to be sorted:");
scanf("%d",&n);
printf("The elements must be lower than %d:",m);
for(int i=0;i<n;i++)
{
printf("input %d element:",i);
scanf("%d",&a[i]);
}
printf("unsorted list");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
bucketsort(a,n);
printf("sorted list");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
return 0;
}
No comments:
Post a Comment