#include<stdio.h>
#include<conio.h>
void insertion(int a[10],int n);
void main()
{
int a[10],n,choice,i;
clrscr();
printf("\nenter 1. for insertion sort");
printf("\nenter 2. for exit");
printf("\nenter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nenter the no. of elements to be sorted by insertion sort technique");
scanf("%d",&n);
printf("\nenter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertion(a,n);
printf("\nthe sorted elements are =");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
break;
case 2:
exit(0);
default:
printf("\nentered a wrong choice");
}
getch();
}
void insertion(int a[10],int n)
{
int i,j,x;
for(i=1;i<n;i++)
{
x=a[i];
j=i-1;
while(j>=0)
{
if(a[j]>x)
{
a[j+1]=a[j];
}
else
{
a[j+1]=x;
break;
}
j--;
}
if(j==-1)
a[0]=x;
}
}