Saturday, 5 November 2016

insertion sort

Tags

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, a[100] , i , k, j, temp , flag=0;
clrscr();
printf("enter no of elements");//taking input from user
scanf("%d",&n);
printf(enter %d integers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
for(j=0;j<=i-1;j++)
{
if(a[i]<a[j])
{
for(k=i-1;k>=j;k--)
a[k+1]=a[k];
a[j]=temp;
f=1;
}
if(f==1)
break;
}
}
printf("\n after sorting");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}