Insertion sort program using c
Insertion sort program
#include<stdio.h>#include<conio.h>
void main()
{
int i,j,k,x,a[5];
clrscr();
printf("Elements to be inserted");
scanf("%d",&x);
i=0;
while(x>0)
{
k=i-1;
while(x<a[k])&&(k>=0)
{
a[k+1]=a[k];
--;
}
a[k+1]=x;
printf("\n %d is inserted:",x);
printf("\n The array is:");
for(j=0;j<=i;j++)
{
printf("\n %d",a[j]);
}
printf("\n Elements to be inserted:");
scanf("%d",&x);
++i;
}
printf("The final sorted array ");
for(j=0;j<i;j++)
printf("\n %d",a[j]);
getch();
}
Output
Elements to be inserted:44 is inserted
The array is :4
Elements to be inserted:8
8 is inserted
The aray is :
4
8
Elements to be inserted:0
The final sorted array:
4
8
No comments: