Suppose the elements in the array are A[]={2, 13.5, 18, 14, 20} Does this array can be represent in INSERTION SORTING? Justify your answer with all the three steps.


Q.) Suppose the elements in the array are A[]={2, 13.5, 18, 14, 20} Does this array can be represent in INSERTION SORTING? Justify your answer with all the three steps.

Subject: Data Structures - II

Yes, the given array can be represented in Insertion Sort. Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted region. Values from the unsorted region are picked and placed at the correct position in the sorted region.

Here are the steps to sort the array A[]={2, 13.5, 18, 14, 20} using insertion sort:

Step 1: Initialization

We start by making the leftmost element of the array as a sorted segment. So, initially, the sorted segment is A[0] = 2.

Step 2: Insertion Operation

Now, we move towards the right of the array, expanding our sorted segment and inserting elements at their correct position within the sorted segment.

Step 3: Iteration

We repeat the insertion operation until the entire array is sorted.

Let's illustrate these steps with the given array:

Iteration Array Comment
0 2, 13.5, 18, 14, 20 No change as 2 is already in its correct position
1 2, 13.5, 18, 14, 20 No change as 13.5 is already in its correct position
2 2, 13.5, 18, 14, 20 No change as 18 is already in its correct position
3 2, 13.5, 14, 18, 20 14 is moved to its correct position
4 2, 13.5, 14, 18, 20 No change as 20 is already in its correct position

So, the sorted array is {2, 13.5, 14, 18, 20}.

In each iteration, the insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. The choice of which element to remove from the input is arbitrary and can be made using almost any choice algorithm.