Algorithm LogbookAlgorithm Logbook

Insertion Sort

Sorting

๐Ÿ“‹ Problem Statement

Given an unsorted array of integers, sort it in ascending order using the Insertion Sort algorithm. Modify the array in-place (O(1)O(1) extra space). Input: [12, 11, 13, 5, 6] โ†’ Output: [5, 6, 11, 12, 13]. Constraint: You must strictly use the Insertion Sort logic. Do not use built-in sort functions.

๐Ÿ’ก Intuition

Think of sorting a hand of playing cards. You keep the sorted cards in your left hand and pick up one unsorted card at a time. You scan your sorted hand from right to left, shift the larger cards over to make room, and insert the new card into its correct position. The left side of the array is progressively built up as a fully sorted section.

๐Ÿ”ง Approach

  1. Iterate from index i=1i = 1 to Nโˆ’1N-1 (assume the first element at index 0 is already sorted).
  2. Store the current element in a variable called item (this is the card you just picked up).
  3. Use an inner loop pointer jj starting at iโˆ’1i - 1 to scan the sorted left portion backwards.
  4. While jโ‰ฅ0j \ge 0 and the element arr[j] is strictly greater than item, shift the element one position to the right (arr[j + 1] = arr[j]).
  5. Once the loop stops, insert the item into the vacated spot (arr[j + 1] = item).

โšก Complexity Analysis

Time
  • Worst & Average Case: O(N2)O(N^2) โ€” Occurs when the array is reverse sorted, requiring every element to be shifted to the very beginning.
  • Best Case: O(N)O(N) โ€” Occurs when the array is already sorted. The inner while loop immediately fails, so no shifting happens.
Space

O(1)O(1) โ€” Insertion Sort is an in-place algorithm requiring only a few variables (item, i, j).

โš ๏ธ Common Mistakes

  • Forgetting the boundary check j >= 0 in the while loop, which will cause an IndexError when scanning past the front of the array.
  • Overwriting values instead of shifting them (forgetting to store arr[i] in the temporary item variable first).
  • Using arr[j] >= item instead of arr[j] > item. Using >= causes the algorithm to swap identical elements, which breaks the "stability" of the sort.

๐ŸŽฏ Final Thoughts

Insertion Sort is highly efficient for small datasets or arrays that are already partially sorted (it is an "adaptive" algorithm). It is also a "stable" sort, meaning duplicate elements retain their original relative order. While it shares the same O(N2)O(N^2) worst-case time complexity as Bubble and Selection sort, its O(N)O(N) best-case makes it the fastest of the three in real-world, nearly-sorted scenarios.