write a program for insertion sort with example.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],n,i,j,temp;
printf("enter the size of an array\n");
scanf("%d",&n);
printf("enter the elements in an array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 &&a[j]>temp)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}


Comments

Popular posts from this blog