Intermediate 25 min

Interactive Visualization

Watch insertion sort work in real-time. The visualizer shows how elements are picked, compared, shifted, and inserted.

Insertion Sort Visualization

1x

What to Observe

As you watch the visualization, notice:

  1. Sorted portion: Left side grows as elements are inserted
  2. Current element: Highlighted element being processed
  3. Shifting: Elements moving right to make room
  4. Insertion: Element placed in correct position
  5. Final result: Array is completely sorted

Understanding the Colors

  • Green: Sorted portion (left side)
  • Blue/Cyan: Current element being inserted
  • Orange/Yellow: Elements being shifted
  • Red: Elements being compared
  • Gray: Unsorted portion (right side)

Try Different Arrays

The visualizer shows how insertion sort:

  • Builds sorted array incrementally
  • Shifts elements efficiently
  • Handles nearly sorted arrays well
  • Works in-place without extra space

Best Case Scenario

For a nearly sorted array like [1, 2, 3, 5, 4]:

  • Only one element (4) needs shifting
  • Inner loop runs few times
  • Very efficient - close to O(n)

Worst Case Scenario

For a reverse sorted array like [5, 4, 3, 2, 1]:

  • Every element needs maximum shifting
  • Inner loop runs many times
  • O(n²) comparisons and shifts

What’s Next?

Now that you’ve seen it work, let’s implement insertion sort in code.