Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Insertion Sort Algorithm - GATE CSE Notes

Insertion sort is a simplistic sorting method that creates the final sorted array one item at a time.

Table of Content

Working of Insertion Sort Algorithm

We used an unsorted array to demonstrate how the insertion sort algorithm functions.

Let the components of the array are –

Insertion Sort Algorithm 1

At first, we contrasted the first two components of the insertion sort.

Insertion Sort Algorithm 2

Here, 31 is greater than 12. Therefore, since the array’s components are already arranged in ascending order, we don’t need to modify anything. We can state that 11 is currently kept in a sorted sub-array.

Insertion Sort Algorithm 3

It’s time to contrast the next two components.

Insertion Sort Algorithm 4

25 in this array is less than 31. That means this is where we have to swap places. Element 31 is not at the accurate place. We must replace 31 with 25. Now we’ll validate it with every element in the sorted array using the insertion sort technique.

The array now only has one element, namely 12, hence it is empty. Therefore, 25 is more than 11. Therefore, even after swapping, the array will still be sorted.

Insertion Sort Algorithm 5

Currently, two of the organised array’s components are 12 and 25. It’s time to think about the following two components, which are 31 and 8.

Insertion Sort Algorithm 6

31 and 8 are both unorganised components. Therefore, we need to do a swap transaction.

Insertion Sort Algorithm 7

The components 25 and 8 are clearly out of order after the switch.

Insertion Sort Algorithm 8

So, swap them.

Insertion Sort Algorithm 9

Additionally, the components 12 and 8 are not sorted.

Insertion Sort Algorithm 10

Therefore, we must also perform swap operations on them.

Insertion Sort Algorithm 11

The sorted array now contains three components with the values 8, 12, and 25. Go to items 31 and 32 in the following list.

Insertion Sort Algorithm 12

These array elements are already sorted.

Insertion Sort Algorithm 13

We’ll go on to the following two components, which are 32 and 17.

Insertion Sort Algorithm 13

17 is less than 32 in size. In order to work on them, we must exchange them.

Insertion Sort Algorithm 14

Both elements, 31, and 17, are unsorted after the swap. Therefore, we must switch them.

Insertion Sort Algorithm 16

After exchanging, 25 and 17 are no longer sorted. We use swap operations once more.

Insertion Sort Algorithm 15

Now, the array is fully sorted.

Advantages of Insertion Sort Algorithm

The implementation of insertion sort is very easy.

  • The nature of this algorithm is quite adaptive, which means this is good for data sets that are already sorted.

Let’s now examine how the insertion sort algorithm functions.

Insertion Sort Complexity

1. Time Complexity

Case Time Complexity
Best Case O(n)
Average Case O(n2)
Worst Case O(n2)

2. Space Complexity

Space Complexity O(1)

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,