Translate

Labels

Thursday, 28 February 2013

INSERTION SORT



# include <stdio.h>
int a[100];
main()
{
int n,i,j,t,Z;
clrscr();
printf("\t\t\t\t INSERTION SORT \n");
printf("\nEnter the no. of elements:\n");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nIterations:\n");
for(j=2;j<=n;j++)
{
      for(Z=1;Z<=n;Z++)
printf("%d  ",a[Z]);
printf("\n");
t=a[j];
insert(t,j-1);
}
for(Z=1;Z<=n;Z++)
printf("%d  ",a[Z]);
printf("\n\nThe sorted elements are...\n");
for(Z=1;Z<=n;Z++)
printf("%d  ",a[Z]);
getch();
}
insert(int r,int i)
{
int J,k;
J=i;
k=r;
while(k<a[J])
{
a[J+1]=a[J];
J--;
if(J==0)
break;
}
a[J+1]=r;
}


No comments: