In: Computer Science
Suppose you have the following list of integers to sort:
[1, 5, 4, 2, 18, 19]
which list represents the partially sorted list after three complete passes of insertion sort?
1 2 4 5 18 19 |
||
1 4 5 2 18 19 |
||
1 4 2 5 18 19 |
||
None of the above |
The answer is 1 2 4 5 18 19
Explanation:
The steps in the insertion sort is as follows
1.In an array a[] of size n, we have elements stored from a[0] to a[n].
Iterate from a[1] to a[n]
2.compare the current element called as key with the predecessor elements.
3.If key is smaller than the predecessor then compare with the elements before.
Move the greatee element to one position up to make space for swapped element.
Refer to the image for the steps of insertion sort for the given array. Thank you