Related Articles
Algorithms | InsertionSort | Question 1
• Difficulty Level : Basic
• Last Updated : 24 Jun, 2016

Consider an array of elements arr[5]= {5,4,3,2,1} , what are the steps of insertions done while doing insertion sort in the array.

(A) 4 5 3 2 1
3 4 5 2 1
2 3 4 5 1
1 2 3 4 5

(B) 5 4 3 1 2
5 4 1 2 3
5 1 2 3 4
1 2 3 4 5

(C) 4 3 2 1 5
3 2 1 5 4
2 1 5 4 3
1 5 4 3 2

(D) 4 5 3 2 1
2 3 4 5 1
3 4 5 2 1
1 2 3 4 5

Explanation: In the insertion sort , just imagine that the first element is already sorted and all the right side Elements are unsorted, we need to insert all elements one by one from left to right in the sorted Array.

Sorted : 5                                unsorted : 4 3 2 1

Insert all elements less than 5 on the left (Considering 5 as the key )

Now key value is 4 and array will look like this

Sorted : 4 5                             unsorted : 3 2 1

Similarly for all the cases the key will always be the newly inserted value and all the values will be compared to that key and inserted in to proper position.

Quiz of this Question

My Personal Notes arrow_drop_up