/* Insertion Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 23, 15, 29, 11, 1};
int i,j,k,temp;
clrscr();
printf(" Insertion Sort. \n");
printf(" \n Array prior to sorting :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(x[j] > x[i])
{
temp=x[j];
x[j]=x[i];
for(k=i;k>j;k--)
x[k]=x[k-1];
x[k+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
getch();
}
No comments:
Post a Comment