Saturday, 20 March 2021

Bucket Sort Program

 //write a program to sort the number to bucket sort

#include<stdio.h>

#include<conio.h>

void main()

{

int a[100],i,j=0,k,n,max=1,ten=1,low=0,temp,skp,t;

clrscr();

printf("enter no:");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

printf("\nenter n:");

scanf("%d",&a[i]);

}

for(i=1;i<=n;i++)

{

while((a[i]/ten)!=0)

{

j++;

ten=ten*10;

}

if(j>max)

{

max=j;

}

j=0;

ten=1;

}

for(i=1;i<=max;i++)

{

low=0;

k=1;

while(low<10)

{

for(j=k;j<=n;j++)

{

if((a[j]/ten)%10==low)

{

temp=a[j];

for(skp=j;skp>k;skp--)

{

a[skp]=a[skp-1];

}

a[skp]=temp;

k++;

}

}

low++;

}

ten=ten*10;

printf("\nafter itretion%d:",i);

for(t=1;t<=n;t++)

{

printf("%d\t",a[t]);

}

}

printf("\nsorted data\n");

for(i=1;i<=n;i++)

{

printf("%d\t",a[i]);

}

getch();

}

1 comment: