Monday 21 October 2013

Program In C For Insertion Sort In An Array

#include<stdio.h>
#include<conio.h>
#include<limits.h>
void main()
{ int a[10],k,i,n,ptr,temp;
clrscr();
printf("Enter The Size Of Array: ");
scanf("%d",&n);
printf("\nEnter Array: ");
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]); }
a[0]=INT_MIN;
for(k=2;k<=n;k++)
{ temp=a[k];
ptr=k-1;
while(temp<a[ptr])
{ a[ptr+1]=a[ptr];
ptr=ptr-1;
a[ptr+1]=temp; }  }
printf("\nArray After Sorting Is: ");
for(i=1;i<=n;i++)
{printf("%d\t",a[i]); }
getch();
}

No comments:

Post a Comment