Insertion Sort Stepper

Insertion sort builds a sorted region one element at a time. At each step it removes the next element, shifts larger values to the right, and inserts the saved element into the correct gap. Use the controls to follow the algorithm line-by-line and watch the array update in real time.

Pseudocode

  1. for i ← 1 to length(A) - 1
  2.   key ← A[i]
  3.   j ← i - 1
  4.   while j ≥ 0 and A[j] > key
  5.     A[j + 1] ← A[j]
  6.     j ← j - 1
  7.   A[j + 1] ← key

Array Animation

Press play to begin the demonstration.