Insertion Sort (Algo) :

Insertion sort is a very simple sorting algorithm,in which the sorted array is built one element at a time.Insertion sort is very less efficient when compared with other more advanced algorithms such as quick sort, heap sort, and merge sort.

INSERTION_SORT (A, N) :
Here A is an unsorted array having N elements.

Step I :         Repeat Steps 2 to 5 for K = 1 to N
Step II :                   Set Temp = A [ k ]
Step III :                  Set J = k – 1
Step IV :                   Repeat while Temp <= A [ j ]
                                               Set A [ j + 1 ] = A [ j ]
                                               Set J = j – 1
 [ End of Inner loop ]
Step V :                   Set A [ j + 1 ]  = Temp
 [ End of for  Loop ]
Step VI :      Exit.

Sorting Steps :
IS
Advantages :-
1. Easy to implement
2. Efficient to use on small sets of data.
3. Requires less memory space.
4. It is twice as fast as the bubble sort, almost 40 % faster than the selection sort.

Leave a comment