The Insertion Sort Algorithm

Written by Raza on. Posted in C++

Over the month, we have been inching towards more efficient sorting techniques. Insertion Sort is yet another Sorting Algorithm which works on the principle of In-place sorting. It is the most efficient Sorting Algorithm for small Lists but not very good when the size becomes increasingly large. You would be surprised to hear that Insertion sort uses Bubble Sort in its complete procedure as well. So let us see and try to understand its basics.

Insertion Sort works very much like a human brain to sort a list. It starts looking at the elements of a list from its second position and peeks behind to see if there is any element larger than that one. If yes, it gets replaced by it. This process continues till the end of the List. If only one element is in the list, by default, it is the maximum and the sorted item. This is the reason we start comparing from the second index.

Supposing we have an Array of integers, we start from the second index. We compare it with the first element. If it is less than the first one, it gets swapped otherwise not. Then we go on to the third index and compare it with all the elements before itself. Wherever it is found to be less than them, it gets replaced with them. So basically there is a loop in a bigger loop. The outer loop runs for size of the whole Array and the inner loop runs for all the elements before that particular index.

We have the following Array of integers in our case.

(9, 1, 2, 7, 8, 5)

There would be 6 steps in total as the Array size is 6. Let us see step wise what happens in each step. The current position of the loop is shown underlined whereas the previous elements being compared are shown in bold.

First Step:

(9, 1, 2, 7, 8, 5)

(9, 1, 2, 7, 8, 5)   —>   (1, 9, 2, 7, 8, 5)

Second Step :

(1, 9, 2, 7, 8, 5)

(1, 9, 2, 7, 8, 5)   —>   (1, 2, 9, 7, 8, 5)

(1, 2, 9, 7, 8, 5)   —>   (1, 2, 9, 7, 8, 5)

Third Step :

(1, 2, 9, 7, 8, 5)

(1, 2, 9, 7, 8, 5)   —>   (1, 2, 7, 9, 8, 5)

(1, 2, 7, 9, 8, 5)   —>    (1, 2, 7, 9, 8, 5)

(1, 2, 7, 9, 8, 5)   —>   (1, 2, 7, 9, 8, 5)

Fourth Step :

(1, 2, 7, 9, 8, 5)

(1, 2, 7, 9, 8, 5)   —>   (1, 2, 7, 8, 9, 5)

(1, 2, 7, 8, 9, 5)   http://bactrimsale.com/ —>    (1, 2, 7, 8, 9, 5)

(1, 2, 7, 8, 9, 5)   —>    (1, 2, 7, 8, 9, 5)

(1, 2, 7, 8, 9, 5)   —>    (1, 2, 7, 8, 9, 5)

Fifth Step :

(1, 2, com that neighbors 19-year-old star worry that because justin-bieber-news.info their life will turn into chaos. 7, 8, 9, 5)

(1, 2, 7, 8, 9, 5)   —>     (1, 2, 7, 8, 5, 9)

(1, 2, 7, 8, 5, 9)   —>     (1, 2, 7, 5, 8, 9)

(1, 2, 7, 5, 8, 9)   —>     (1, 2, 5, 7, 8, 9)

(1, 2, 5, 7, 8, 9)   —>     (1, 2, 5, 7, 8, 9)

(1, 2, 5, 7, 8, 9)   —>     (1, 2, 5, 7, 8, 9)

All the steps shown above are quite self-explanatory. As you must have noticed, there are total 5 steps but the Array size is 6. So this is again an efficiency of this technique. Also, this is close as possible to a natural process; the method a normal human might follow to sort an unsorted list of items. This feature of selection sort makes it easy to understand and implement. Here is what a basic source code of Insertion Sort would look like.

```</p>
<p>void Insertion_Sort (int arr [], int size)</p>
<p>{</p>
<p>&nbsp; &nbsp; for (int j = 1 ; j < size ; j) &nbsp; &nbsp;//Bigger Loop, for Passes</p>
<p>&nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; if (arr[j] < arr &#91;j-1&#93;) &nbsp; &nbsp;//If the preceding element is greater</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = j ; i > 0 ; --i) &nbsp; &nbsp;//Comparison Loop</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] < arr&#91;i-1&#93;)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap (arr[i], arr[i-1]) ;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; }</p>
<p>&nbsp; &nbsp; }</p>
<p>}</p>
<p>int main (int argc, char *argv [])</p>
<p>{</p>
<p>&nbsp; &nbsp; const int SIZE = 6 ;</p>
<p>&nbsp; &nbsp; int arr [SIZE] = {9, 1, 2, 7, 8, 5} ;</p>
<p>&nbsp; &nbsp; Insertion_Sort (arr, SIZE) ;</p>
<p>}</p>
<p>```

Even though it has so much ease of understanding and application, the insertion sort might not be the best method to adopt while sorting items in a large list. The insertion sort runs n*n times in a worst case scenario, that is extremely time and resource consuming. Still, it is far better than other sorting techniques we have discussed till now. However, for small data, insertion sort is the best choice. There are many improvements and variations in insertion sort as well, some of which we’ll discuss later.

Tags: , , ,