Friday, 19 July 2013

Program in C to Implement Insertion Sort

#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , n, temp,a[10];
clrscr();
printf(“Enter the size of array\n”);
scanf(“%d”,&n);
printf(“Enter %d elements \n”)
for(i=1;i<=n;i++)
{
scanf(%d”, &a[i]);
}
for(i=1;i<=n;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j]&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(“The sorted array is\n”);
for(i=1;i<=n;i++)
{
printf(“%d”,a[i]);
}
getch();

}

1 comment: