Insertion Sort Algorithm

A simple comparison-based sorting algorithm that builds the final sorted array one item at a time.

Understanding Insertion Sort

Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It's much like sorting playing cards in your hands.

How it Works:

  1. Start with First Element: Assume that the first element is already sorted.
  2. Select Next Element: Pick the next unsorted element and store it as a "key".
  3. Insert in Correct Position: Compare the key with all elements in the sorted part of the array, moving from right to left. Move elements greater than the key to the right to make space for the key.
  4. Place Key in Correct Position: Once you find an element smaller than the key or reach the beginning of the array, insert the key after that element.
  5. Repeat: Continue this process until all elements are sorted.

Complexity:

  • Time Complexity:
    • Best Case: O(n) - when the array is already sorted
    • Average Case: O(n²)
    • Worst Case: O(n²) - when the array is sorted in reverse order
  • Space Complexity: O(1) - sorting is done in-place

Key Properties:

  • Stable: Maintains the relative order of equal elements
  • Adaptive: Becomes faster when the array is partially sorted
  • In-place: Only requires a constant amount O(1) of additional memory space
  • Online: Can sort a list as it receives it

When to Use Insertion Sort:

  • When the array is small (typically < 20 elements)
  • When the array is already mostly sorted
  • When you need a simple implementation with low overhead
  • As a subroutine in more complex algorithms like Timsort

Real-world Applications:

While insertion sort isn't typically used for large data sets due to its O(n²) average complexity, it has several practical applications:

  • Used in hybrid sorting algorithms like Timsort (used in Python) and Introsort (used in C++ STL) for small subarrays
  • Preferred for sorting small files or arrays
  • Used when only a few elements need to be inserted into an already sorted list
  • Applied in situations where input arrives continuously and needs to be processed online