# Insertion sort and let us consider an array of size 10 for Data Structure in 'C'

## Insertion sort:

In this technique of sorting, the fact of inserting the pivotal element  at its proper position from n (size of the array) number of elements is used. The pivotal element to be inserted is picked from the array itself. The selected pivotal element is inserted at the position from the left part of the array that is treated as sorted.
Initially the first element of the array is treated as left part of the array and being one element that is treated as sorted part of the array. The first pivotal element that is selected for insertion is the second element of the array. the pivotal element is compared with the first element and if the first element is greater than the pivotal element, then it is moved to the second position. Now we remain with no elements compared to be compared in the left part, the position where the pivotal element is found as the first position. The pivotal element is inserted at the first position to over the pass. If the first element is not greater than the pivotal element then the position of the pivotal element is right and remains at the same place. After the first pass the left part of the array containing two elements is in sorted order.
In the further passes the element of the unsorted part of the array is selected as pivotal element and a position to insert it in the sorted part (left array) of the array is found by shifting the sorted elements to the right if required. So after every pass the pivotal element is inserted at its proper position in the array. As the insertion operation is being performed to insert the pivotal element, this technique of sorting the array is called as ‘’insertion sort’’.

### Let us consider an array of size 10 with the following elements:

91 18 22 43 34 10 88 11 33 77

We can observe the elements and see that the elements are not in any order. So to arrange the elements in ascending order we can apply the insertion sort.
In the first pass, we select the element 18 is pivotal element. The left part of the of the array before the pivotal element contains only one element and it is the sorted part of the array. 18 is compared with 91,91 is greater than 18. So, 91 is shifted to the right by one position. Now there are no elements to be compared in the sorted part of the array with the pivotal element. Now the pivotal element is inserted at first position of the array. After this pass the array looks as follows: [sorted elements-BOLD, pivotal element – underlined].
18 91 22 43 34 10 88 11 33 77
In the second pass, 22 is selected as pivotal element. 22 is compared with 91, as 91 is greater then 22, 91 is shifted towards its right by one position. Again 22 is compared with 18,18 is not greater then the pivotal element 22. So, the comparison is stopped because the position where the pivotal element 22 is to be inserted is found. The pivotal element 22 is inserted at second position. After this pass the array looks as follows:
18 22 91 43 34 10 88 11 33 77
In the third pass, 43 is selected as pivotal element from the unsorted part of the array. 43 is compared with 91 and  91 is shifted towards its right by one position. 43 is again compared with 22. 22 is not greater then 43 and comparison is stops. 43 is inserted at third element of array.  After this pass the array looks as follows:
18 22 43 91 34 10 88 11 33 77
In the fourth pass, 34 is selected as pivotal element from the unsorted part of the array. 34 is compared with 91 and 91 is shifted towards its right by one position. 34 is again compared with 43. 43 is also right shifted by one position. 34 is now comparison with 22. 22 is not greater then 34 and comparison stops. 34 is inserted at third element of array.  After this pass the array looks as follows:
18 22 34 43 91 10 88 11 33 77
In the fifth pass, 10 is selected as pivotal element from the unsorted part of the array. As 10 smaller then all the sorted elements of the left part of the array, each element is shifted to right by one position respectively. 10 is inserted as the first element of array.  After this pass the array looks as follows:
10 18 22 34 43 91 88 11 33 77
In the sixth pass, 88 is selected as pivotal element from the unsorted part of the array. 88 is compared with 91 and  91 is shifted towards its right by one position because it is greater then 88. 88 is now compared with 43. and comparison is stops. 88 is inserted at sixth element of array.  After this pass the array looks as follows:
10 18 22 34 43 88 91 11 33 77
In the seventh pass, 11 is selected as pivotal element from the unsorted part of the array. 11 is compared with all the element up to 18 and they are  shifted to right by one position respectively. The comparison is stops when 11 is compared with 10. So, 11 is inserted at second element of the array.  After this pass the array looks as follows:
10 11 18 22 34 43 88 91 33 77
In the eighth pass, 33 is selected as pivotal element from the unsorted part of the array. 33 is compared with all the element up to 34 and they are  shifted to right by one position respectively. The comparison is stops when 33 is compared with 22. So, 33 is inserted at fifth element of the array.  After this pass the array looks as follows:
10 11 18 22 33 34 43 88 91 77
In the ninth and final pass, 77 is selected as pivotal element from the unsorted part of the array. 77 is compared with 91 and 88 and they are shifted to right by one position respectively. The comparison is stops when 77 is compared with 43. So, 77 is inserted at eighth element of the array.  After this pass the array looks as follows:
10 11 18 22 33 34 43 77 88 91

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google