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:
- Sorted portion: Left side grows as elements are inserted
- Current element: Highlighted element being processed
- Shifting: Elements moving right to make room
- Insertion: Element placed in correct position
- 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.
Progress 57%
Page 4 of 7
← Previous
→ Next