# Insertion Sort Algorithm

Insertion sort is also very simple sorting algorithm. In this sorting we break the list in two parts. We pick up a particular value and insert it at the appropriate place in the sorted sub list. That means during the kth iteration the element a[k] is inserted in its proper place in the sorted sub array.

Algorithm

Following are the simple steps to sort array using insert sort:

Pass 1-  a is inserted either before or after a so that a and a are sorted.

Pass 2-  a is inserted either before a or between a and a or after a so that the elements a , a , a are sorted.

Pass k – a[k] is inserted in proper place in the sorted sub array a,a,a……,a[k-1] so that after insertion , the elements a, a,a,….a[k-1],a[k] are sorted.

Pass n-1 – a[n-1] is inserted in proper place in sorted sub array a,a,a,….a[n-2],so that after insertion, the elements a,a,a,……a[n-1] are sorted

Working of Insertion Sort

To understand the working of insertion sort lets take an unsorted array named a having 7 elements pass 1: Since a < a , insert element a before a , this give the following table pass 2: Since a < a , no action is performed. pass 3: Since a < a , no action is performed. pass 4: Since a is less than a, a, a and a , therefor insert a before a , giving the following array. pass 5: Since a is less than a, a,a and a , therefor insert a before a and after a , giving the following array. pass 6: Since a is less than a,a and a , therefor insert a before a and after a , giving the following array. Complexity: 