Write a Program in C to Accept 5 Numbers from the User and Sort Using Bubble Sort
#include<stdio.h>#include<conio.h>
void main()
{
int swap,i,j,num[10],k;
clrscr();
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]); //accepting 5 numbers
}
printf("\n\n Original Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n\n");
for(i=0;i<=3;i++)
{
printf("Round %d \n",i+1);
for(j=0;j<=3-i;j++)
{
printf("Comapring (%d) and (%d)",j+1,j+2);
if(num[j]>num[j+1])
{
swap=num[j];
num[j]=num[j+1]; //swapping if is greater
num[j+1]=swap;
}
printf("\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]); //printing the numbers
}
printf("\n");
getch();
}
printf("\n");
}
printf("\n\nNumbers after using Bubble Sort Method\n");
for(i=0;i<=4;i++)
{
printf("%d\n",num[i]);
}
getch();
}
No comments:
Post a Comment