Time Complexity of Insertion Sort Algorithm

Best Case: O(N)

  • The best-case time complexity of Insertion Sort occurs when the input array is already sorted.
  • In this scenario, each element is compared with its preceding elements until no swaps are needed, resulting in a linear time complexity.
  • Therefore, the best-case time complexity is O(N), where n is the number of elements in the array.

Average Case: O(N2)

  • The average-case time complexity of Insertion Sort is also O(N2).
  • This complexity arises from the nature of the algorithm, which involves pairwise comparisons and swaps to sort the elements.
  • Although the exact number of comparisons and swaps may vary depending on the input, the average-case time complexity remains quadratic.

Worst Case: O(N2)

  • The worst-case time complexity of Insertion Sort occurs when the input array is in reverse sorted order.
  • In this scenario, each element needs to be compared and possibly swapped with every preceding element, resulting in a quadratic time complexity.
  • Therefore, the worst-case time complexity is O(N2), where n is the number of elements in the array.

Time and Space Complexity of Insertion Sort

Similar Reads

What is Insertion Sort?

Insertion sort is a simple sorting algorithm that works similarly to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed in the correct position in the sorted part....

Time Complexity of Insertion Sort Algorithm:

Best Case: O(N)...

Auxiliary Space Complexity of Insertion Sort Algorithm:

The auxiliary space complexity of Insertion Sort is O(1), indicating it uses constant extra space regardless of the input size....