Insertion sorting cprogram
Aim : To write Insertion Sorting Program in C program
Program:
#include<stdio.h>
void main()
{
int a[50],n,i;
clrscr();
printf("\n Enter no of elements to be entered");
scanf("%d",&n);
printf(" Enter elements");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
insort(a,n);
printf(" Insertion sorting");
for(i=1;i<=n;i++)
printf(" %d",a[i]);
getch();
}
insort(int a[],int n)
{
int k,ele,loc;
for(k=2;k<=n;k++)
{
ele=a[k];
loc=k-1;
while(loc>=1&&ele<a[loc])
{
a[loc+1]=a[loc];
loc=loc-1;
}
a[loc+1]=ele;
}
}
Output :
No comments:
Post a Comment