Time and Space Complexity of Insertion Sort (original) (raw)

Last Updated : 08 Feb, 2024

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.

To sort an array of size N in ascending order iterate over the array and compare the current element (key) to its predecessor, if the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

Insertion Sort Algorithm

**Time Complexity of Insertion Sort Algorithm:

**Best Case: O(N)

**Average Case: O(N 2 )

**Worst Case: O(N 2 )

**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.

This is because the algorithm typically performs in-place sorting, meaning it rearranges the elements within the input array itself without requiring additional data structures or memory allocation proportional to the input size. Therefore, regardless of the size of the input array, the amount of extra space used by the Insertion Sort algorithm remains constant.