diff --git a/bucket sort.c b/bucket sort.c new file mode 100644 index 0000000..635bfa0 --- /dev/null +++ b/bucket sort.c @@ -0,0 +1,59 @@ +#include<stdio.h> +#include<stdlib.h> + +int main() +{ + int a[10][20],count[10]={0}; + int i,n,ele,x,y; + printf("enter the elements between 0 and 99\n"); + printf("enter the number of elements\n"); + scanf("%d",&n); + printf("enter %d elements\n",n); + for(i=0;i<n;i++) + { + scanf("%d",&ele); + int tplace=ele/10; + a[tplace][count[tplace]]=ele; + count[tplace]++; + } + for(i=0;i<10;i++) + { + if(count[i]==0) + { + continue; + } + else + { + int noe=count[i]; + for(x=0;x<noe-1;x++) + { + for(y=0;y<noe-x-1;y++) + { + if(a[i][y]>a[i][y+1]) + { + int z; + z=a[i][y]; + a[i][y]=a[i][y+1]; + a[i][y+1]=z; + } + } + } + } + } + printf("elements afetr sorting\n"); + for(i=0;i<10;i++) + { + if(count[i]==0) + { + continue; + } + else + { + int w; + for(w=0;w<count[i];w++) + { + printf("%d ",a[i][w]); + } + } + } +} diff --git a/bucketsort.c b/bucketsort.c new file mode 100644 index 0000000..90d0a09 --- /dev/null +++ b/bucketsort.c @@ -0,0 +1,88 @@ +#include<stdio.h> +#include<math.h> +void main() +{ + int a[100],b[10][10],c[10]={0},n,i,count=1,rem,k,u=0,h=0,e=0,j=0,p,t,w,m; + printf("enter the no.of elements"); + scanf("%d",&n); + printf("enter the elements"); + for(i=0;i<n;i++) + { + scanf("%d",&a[i]); + } + for(i=0;i<n;i++) + { + rem=a[i]/10; + while(rem>0) + { + count++; + rem=rem/10; + + } + if(count==1) + { + + b[0][h]=a[i]; + h++; + c[0]++; + } + if(count==2) + { + k=a[i]/10; + if(k==m) + { + e++; + } + else{e=0;} + b[k][e]=a[i]; + + c[k]++; + m=k; + } + count=1; + } +for(i=0;i<10;i++) +{if(c[i]==0) +{ + continue; +} +else +{ +p=i; + for(w=0;w<c[i];w++) + { + for(j=w+1;j<c[i];j++) + { + + if(b[p][w]>b[p][j]) + { + t=b[p][w]; + b[p][w]=b[p][j]; + b[p][j]=t; + + } + } + + + } +} + +} + for(j=0;j<10;j++) + { + if(c[j]==0) + { + continue; + } + else{ + for(p=0;p<c[j];p++) + { + + printf("%d ",b[j][p]); + } + } + + + } + +} diff --git a/radixsort.c b/radixsort.c new file mode 100644 index 0000000..8410956 --- /dev/null +++ b/radixsort.c @@ -0,0 +1,85 @@ +#include<stdio.h> +#include<math.h> +int a[100],rem,larg,count=1,b[10],num,c[100],g,u=1,k,y,e1,f1,v,ym; +int n,i,e,f,t; +void main() +{ +printf("enter the no.of elements"); +scanf("%d",&n); +printf("enter the elements"); +for(i=0;i<n;i++) +{ + scanf("%d",&a[i]); +} +larg=a[0]; +for(i=1;i<n;i++) +{ + if(a[i]>larg) + { + larg=a[i]; + } +} +rem=larg/10; +while(rem>0) +{ + rem=rem/10; + count++; +} +while(u<=count) +{ +for(i=0;i<10;i++) +{ + b[i]=0; +} +for(i=0;i<n;i++) +{ + if(u==1) + { + t=pow(10,u); + num=a[i]%(t); + b[num]++; +} +else{ +e=pow(10,u); +f=pow(10,u-1); +y=a[i]%(e); +num=y/(f); +b[num]++; +} +} +for(i=1;i<10;i++) +{ + b[i]=b[i]+b[i-1]; +} +for(i=n-1;i>=0;i--) +{ + v=pow(10,u); + if(u==1) + { + g=a[i]%v; + k=b[g]; + c[k-1]=a[i]; + b[g]--; +} +else +{ + e1=pow(10,u); +f1=pow(10,u-1); +ym=a[i]%(e1); +g=ym/(f1); + k=b[g]; + c[k-1]=a[i]; + b[g]--; +} +} +for(i=0;i<n;i++) +{ + a[i]=c[i]; +} +u++; +} +for(i=0;i<n;i++) +{ + printf("%d ",a[i]); +} +}