Fainal Project
Fainal Project
presentation
Firstly,
• It selects the second element (2).
• It checks whether it is smaller than any of the elements before it.
• Since 2 < 6, so it shifts 6 towards right and places 2 before it.
• The resulting list is 2, 6, 11, 7, 5.
Secondly,
• It selects the third element (11).
• It checks whether it is smaller than any of the elements before it.
• Since 11 > (2, 6), so no shifting takes place.
• The resulting list remains the same.
Thirdly,
Fourthly,
2, 5, 6, 7, 11
Insertion Sort Example-
Step-03: For i = 3
Step-04: For i = 4
Loop gets terminated as ‘i’ becomes 5. The state of array after the loops are finished-
Here,
i = variable to traverse the array A
key = variable to store the new number to be inserted into the sorted sub-array
j = variable to traverse the sorted sub-array
Important Notes-
Insertion sort is not a very efficient algorithm when data sets are large.
This is indicated by the average and worst case complexities.
Insertion sort is adaptive and number of comparisons are less if array is partially
sorted.