Insertion Sort

Builds the sorted array one element at a time by inserting each element into its correct position. Very efficient for small or nearly sorted data.

Best O(n) Avg O(n²) Worst O(n²) Space O(1) Stable Yes In-place Yes Comparison-based

How it works

Insertion Sort builds the sorted portion one element at a time. It takes the next unsorted element and shifts larger sorted elements one position right until the correct slot opens up, then drops the element in.

Because it only moves elements that are actually out of place, it is genuinely adaptive: nearly-sorted input runs in close to O(n).

Implementation

function insertionSort(arr) {
  for (let i = 1; i < arr.length; i++) {
    let key = arr[i], j = i - 1;
    while (j >= 0 && arr[j] > key) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
}

Advantages

  • Excellent on small or nearly-sorted arrays — often the fastest choice for tiny inputs.
  • Stable and in-place with O(1) extra memory.
  • Online: can sort a stream as elements arrive.

Disadvantages

  • O(n^2) on average and in the worst case for random data.
  • Many element shifts when the array is in reverse order.

When to use it

Use it for small subarrays (it is the base case inside Quick Sort and Tim Sort) and whenever the data is already close to sorted.