Insertion Sort

Insertion Sort is a simple, comparison-based sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms like QuickSort or MergeSort, but it excels at sorting small datasets or partially sorted arrays.


1. The Core Mechanism: The "Hand" Analogy

  1. Pick: Take the next element from the unsorted section.
  2. Compare: Compare it with the elements in the sorted section (from right to left).
  3. Shift: Shift the sorted elements one position to the right until you find the correct "insertion" point.
  4. Insert: Place the element in its correct spot.

2. Complexity Analysis

Scenario Time Complexity Space Complexity
Best Case (Sorted) O(N) O(1)
Average Case O(N²) O(1)
Worst Case (Reverse) O(N²) O(1)

3. Implementation

function insertionSort(arr) {
  for (let i = 1; i < arr.length; i++) {
    let key = arr[i];
    let j = j = i - 1;

    // Shift elements of arr[0..i-1] that are greater than key
    // one position ahead of their current position
    while (j >= 0 && arr[j] > key) {
      arr[j + 1] = arr[j];
      j = j - 1;
    }
    arr[j + 1] = key;
  }
  return arr;
}

4. Interview Pro-Tips: Why use it in production?


Technical Summary

  1. Adaptive: Performance improves with the degree of sortedness.
  2. O(1) Space: In-place sorting.
  3. Stable: Ideal for multi-criteria sorts.
  4. Online: Can process elements in a stream.