My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.
Showing posts with label Sort Using Insertion Sort. Show all posts
Showing posts with label Sort Using Insertion Sort. Show all posts

Monday, 15 August 2011

Sort Using Insertion Sort

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();
}

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls