Insertion Sort – Data Structure and Algorithm

Insertion sort functions as a straightforward sorting algorithm, resembling the manner in which you arrange playing cards in your hands. It virtually divides the array into two segments: one sorted and the other unsorted. From the unsorted portion, it selects values and positions them accurately within the sorted segment. This process keeps continuing with each iteration until the array becomes fully sorted. Additionally, this method’s efficiency improves when implemented with small datasets, making it an effective option for sorting tasks.

Insertion Sort Algorithm

For ascending order sorting of an array with a size of N, initially, begin by iterating through the array. Compare the current element (referred to as the key) with the element that comes before it. If the key element happens to be smaller than its predecessor, subsequently proceed to compare it with the elements that precede it. In such cases, shift the larger elements one position upward so as to generate room for the swapped element. This series of comparisons and element shifts ensures that the array becomes sorted in ascending order.

Working of the algorithm

Consider the following example:

Example Array: arr[] = {25, 18, 5, 32, 14, 7}

PassArrayExplanation
1st25    18    5    32    14    7The initial array
18    25    5    32    14    725 and 18 are compared; swap them
18    25    5    32    14    7Inserted 18 into the sorted sub-array
2nd18    25    5    32    14    7No changes; 25 and 5 are in correct order
18    5    25    32    14    725 and 5 are compared; swap them
5    18    25    32    14    7Inserted 5 into the sorted sub-array
3rd5    18    25    32    14    7No changes; 25 and 32 are already sorted
5    18    25    32    14    7Inserted 32 into the sorted sub-array
4th5    18    25    32    14    7No changes; 25 and 14 are not in the right order
5    18    25    14    32    732 and 14 are compared; swap them
5    18    25    14    32    725 and 14 are compared; swap them
5    18    14    25    32    725 and 14 are compared; swap them
5    14    18    25    32    718 and 14 are compared; swap them
5    14    18    25    32    7Inserted 14 into the sorted sub-array
5th5    14    18    25    32    7No changes; 25 and 7 are not in the right order
5    14    18    25    7    3232 and 7 are compared; swap them
5    14    18    7    25    3225 and 7 are compared; swap them
5    14    7    18    25    3218 and 7 are compared; swap them
5    7    14    18    25    3214 and 7 are compared; swap them
5    7    14    18    25    32Inserted 7 into the sorted sub-array
6th5    7    14    18    25    32The array is completely sorted now

This example illustrates the step-by-step demonstration of the insertion sort algorithm, showcasing how each pass rearranges the array into ascending order.

Complexity Analysis

Time Complexity of Insertion Sort

  • Worst-case time complexity of the Insertion sort is O(N^2)
  • Average case time complexity of the Insertion sort is O(N^2)
  • Best case time complexity of the Insertion sort is O(N).

Space Complexity of Insertion Sort

The auxiliary space complexity of Insertion Sort is O(1)

In conclusion, the insertion sort algorithm stands as a valuable sorting technique. By systematically comparing and shifting elements, it ensures the arrangement of data in ascending order. This method’s efficiency is evident through its straightforward implementation and effectiveness, especially with smaller datasets. Consequently, insertion sort serves as a foundational sorting approach, simplifying the organization of data for a wide range of applications.

Selection sort Read More

1 thought on “Insertion Sort – Data Structure and Algorithm”

Leave a Comment