Hello, good evening. :)
I got some problems with the "Bubble Sort" and "Selection Sort". My "Insertion Sort" is already ok. I just can't figure it out with this problem. Hope you can help me with this and explain it to me my mistakes.
Here's the whole code:
#include<stdio.h>
#include<conio.h>
void bubble(void);
void insert(void);
void select(void);
int main()
{
int cho, i;
do{
printf("\n\n\n[1] Bubble Sorting\n");
printf("[2] Insertion Sorting\n");
printf("[3] Selection Sorting\n");
printf("[4] Exit\n\n");
printf("Enter your choice:");
scanf("%d",&cho);
i = getchar();
if(cho==1)
{
bubble();
}
if(cho==2)
{
insert();
}
if(cho==3)
{
select();
}
}while(cho != 4);
printf("\n\n\t\t\t press enter when ready");
i = getchar();
++i;
clrscr();
return 0;
}
void bubble(void)
{
int a[100],n,i,j,t,k;
clrscr();
printf("Enter integer value for total no.s of elements to be sorted:");
scanf("%3d",&n);
printf("\n\n");
for(i=0;i<=n-1;i++)
{
printf(" Enter integer value for element no.%d : ",i+1);
scanf("%3d",&a[i]);
}
printf("\n\n Finally sorted array is :");
for(i=0;i<=n-1;i++)
{
printf("%3d",a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
a[j]=t;
printf("\n The array after pass no.%d:",i);
for(k=0;k<=n-1;k++)
{
printf("%3d",a[k]);
}
}
}
}
void insert(void)
{
int a[100],n,i,j,t,k;
clrscr();
printf("Enter an integer value for total no.s of elements to be sorted: ");
scanf("%3d",&n);
printf("\n\n");
for(i=0;i<=n-1;i++)
{
printf(" Enter an integer value for element no.%d: ",i+1);
scanf("%3d",&a[i]);
}
printf("\n\n Finally sorted array is : ");
for(i=0;i<=n-1;i++)
{
printf("%3d",a[i]);
}
for(i=1;i<=n-1;i++)
{
j=i;
t=a[i];
while(a[j-1]>t && j>0)
{
a[j]=a[j-1];
j=j-1;
}
a[j]=t;
printf("\n The array after pass no.%d: ",i);
for(k=0;k<=n-1;k++)
{
printf("%3d",a[k]);
}
}
}
void select(void)
{
int a[100],n,i,j,k,t;
clrscr();
printf("Enter an integer value for total no.s of elements to be sorted: ");
scanf("%3d",&n);
printf("\n\n");
for(i=0;i<=n-1;i++)
{
printf(" Enter an integer value for element no.%d: ",i+1);
scanf("%3d",&a[i]);
}
printf("\n\n Finally sorted array is : ");
for(i=0;i<=n-1;i++)
{
printf("%3d",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i-1;j>n;j++)
{
if(a[i]<t)
{
t=a[i];
a[i]=a[j];
a[i]=t;
}
}
a[i]=t;
printf("\n The array after pass no.%d: ",i);
for(k=0;k<=n-1;k++)
{
printf("%3d",a[k]);
}
}
}