Insertion Sort
It sorts the array proceeding towards the end by shifting the numbers until we find an element that is less than equal to the item we have selected presently, worst time O(n^2) and best time O(nlog n).
#include<stdio.h>
#include<conio.h>
void Insertion_sort(int *arr, int len)
{
int i,j,value;
for(i=1;i<len;i++)
{
j=i-1;
value = arr[i];
while(arr[j]>value&&j>=0)
{ arr[j+1]=arr[j];
j--;
}
arr[j+1]=value;
}
}
int main()
{
int arr[8]={44,22,33,34,25,42,897,34};
int i;
Insertion_sort(arr, 8);
for(i=0;i<8;i++)
printf(" %d ",arr[i]);
getch();
return 0;
}
Download Program


