How to write a C Program to Insertion Sort in C Programming Language ?
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.See More : Insertion sort
Solution For C Program :
//INSERTION SORT
#include<stdio.h>
main()
{
int a[20],i,n;
clrscr();
printf("enter how many elements do u want to enter: ");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ins_sort(a,n);
getch();
}
ins_sort(int a[],int n)
{
int i,j,k;
for(j=1;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0&&k<a[i];i--)
{
a[i+1]=a[i];
}
a[i+1]=k;
printf("element %d inserted in appropriate location",k);
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
printf("final sorted array is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}