Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bucketsort.c #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions bucket sort.c
Original file line number Diff line number Diff line change
@@ -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]);
}
}
}
}
88 changes: 88 additions & 0 deletions bucketsort.c
Original file line number Diff line number Diff line change
@@ -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]);
}
}


}

}
85 changes: 85 additions & 0 deletions radixsort.c
Original file line number Diff line number Diff line change
@@ -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]);
}
}