INSERTION SORT
Program
#include"stdio.h"
#include"conio.h"
int i,j,a[20],n;
void main()
{
clrscr();
printf("\n INSERTION SORT \n");
printf("Enter The Number Of Elements :");
scanf("%d",&n);
for(i=0;i < n;i++)
scanf("%d",&a[i]);
insert(a,n);
printf("The Sorted List Is :\n");
for(i=0;i < n;i++)
printf("%d\n",a[i]);
getch();
}
insert(a,n)
int a[],n;
{
int i,k,y;
for(k=1;k < n;k++)
{
y=a[k];
for(i=k-1;i > =0 &&y < a[i];i--)
a[i+1]=a[i];
a[i+1]=y;
}
return;
}
INSERTION SORT
Enter The Number Of Elements :5
4
2
8
5
3
The Sorted List Is :
2
3
4
5
8
No comments:
Post a Comment