Insertion sort works in a similar way we sort the playing cards in our hands. Insertion sort is a simple and efficient comparison sort.
In this algorithm we first partition the array into two parts and consider that the left sub array is sorted and right sub array is unsorted. In every iteration an element is removed from the input data and is inserted into the correct position in the list being sorted.
This algorithm falls under Decrease & Conquer Technique which is based on exploiting the relationship between a solution to a given instance of a problem and a solution to its smaller instance. Let's look at the working of this algorithm.
Do refer the code available the end of this section to understand the following theory.
PS: We give random inputs because the no. of input will generally be 100 or 1000 to find the time complexity and manually giving so many inputs is cumbersome work.
There exists two recursive calls in which the elements gets partitioned in each call. The basic operation in this algorithm is the comparison between at step 5 in the above working procedure. So increment the count variable above this if condition to get the count of no. of times the basic operation has been performed.
Time Complexity: O(n2)
Please open this in your pc or with a compatible app in your mobile.
C++ Implementation for INSERTION SORTThat's it from this blog post. If you liked it then do share this blog with your friends or people who wanna get into programming world. Thank You!