Write a Program in C to Accept 5 Numbers from the User and Sort Using Insertion Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,swap,num[10];
clrscr();
printf("Enter 5 numbers\n\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]);
}
printf("\n\nOriginal Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]);
}
printf("\n\n");
for(i=1;i<=4;i++)
{
printf("Round %d \n",i);
for(j=0;j<i;j++)
{
printf("Comapring (%d) and (%d)",i+1,j+1);
if(num[j]>num[i])
{
swap=num[j] ;
num[j]=num[i] ;
for(k=i;k>j;k--)
{
num[k]=num[k-1];
}
num[k+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 Insertion Sort Method\n");
for(i=0;i<=4;i++ )
{
printf("%d\n",num[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,swap,num[10];
clrscr();
printf("Enter 5 numbers\n\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num[i]);
}
printf("\n\nOriginal Nos. \t\t\t");
for(k=0;k<=4;k++)
{
printf("%d\t",num[k]);
}
printf("\n\n");
for(i=1;i<=4;i++)
{
printf("Round %d \n",i);
for(j=0;j<i;j++)
{
printf("Comapring (%d) and (%d)",i+1,j+1);
if(num[j]>num[i])
{
swap=num[j] ;
num[j]=num[i] ;
for(k=i;k>j;k--)
{
num[k]=num[k-1];
}
num[k+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 Insertion Sort Method\n");
for(i=0;i<=4;i++ )
{
printf("%d\n",num[i]);
}
getch();
}
No comments:
Post a Comment