• Now Online : 87
  • admin@codemyne.net

Introduction

Insertion sort is a simple sorting algorithm. In insertion sort each element is compared with before element(s) and inserted before its higher element. So it is called as 'Insortion sort'.This will be repeated until the sorting is finished.

Suppose an array with n elements a[1], a[2],....a[n] is in memory.The insertion sort algorithm scans a from a[1] to a[n], inserting each element a[k] into its proper position in the previously sorted subarray a[1], a[2], ..., a[k-1].

Example for Insertion sort is shown below:

include <stdio.h>
void main()
{
    int a[7]={23,34,2,57,67,8,33};
    int i,j,val;
    for(i=1;i<7;i++)
    {
        val=a[i];
        for(j=i;j>0 && a[j-1]>val;j--)
        {
            a[j]=a[j-1];
        }
        a[j]=val;
    }
    for(i=0;i<7;i++)
    {
        printf("%d \n",a[i]);
    }
}

Comments Post a Comment

srinivas 3/19/2012 (IST) / Reply

hi i want all computer science subjects

Webmaster 3/19/2012 (IST) / Reply

Hi Srinivas, We are covering almost all computer subjects. Please let us know which subjects you want to learn.