Open In App

Algorithms | InsertionSort | Question 1

Like Article
Like
Save
Share
Report

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


Answer: (A)

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
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads