Beware of bugs in the above code; I have only proved it correct, not tried it. Donald E. Knuth
Insertion sort is a simple sorting algorithm that builds sorted array one item at a time. It is efficient for the small data set.
Insertion sort is best suitable for nearly sorted arrays and the small set of elements. It is similar to selection sort. The selection sort must scan all the elements to find the smallest element. But the insertion sort scans only the remaining elements in the array. The insertion sort can combine with divide and conquer based algorithm for better optimization.
Write a program to implement the insertion sort.
Unsorted array before sorting: [12, 34, 56, 23, 45, 78, 46] Sorted array After Insertion Sort sorting: [12, 23, 34, 45, 46, 56, 78]