Insertion Sort VisualizerO(N²) Online & Stable

LeetCode #147
Comparisons0
Shifts / Swaps0
Accesses0
Step 1 / 0
45
[0]
12
[1]
85
[2]
32
[3]
89
[4]
39
[5]
67
[6]
19
[7]
Code Execution

šŸ’” Insertion Sort in Layman's Terms

Insertion Sort works like sorting playing cards in your hand! It treats the 1st card on the far left as already sorted, then picks up each remaining card one-by-one from left to right and inserts it into its exact spot.

How it Works Step-by-Step:
  • 1Pick the Next Card (Left to Right): Consider the 1st card on the far left as a 1-card sorted hand. Move to the 2nd card and pick it up as your target card to insert.
  • 2Compare Backwards & Make Room: Look left into your sorted cards. If a card in your hand is bigger than your target card, slide that bigger card one step to the right to open up a gap.
  • 3Insert Into Open Gap: As soon as you hit a smaller card (or reach the start of the row), drop your target card right into the open gap!
šŸƒ Real-World Analogy: Organizing Playing Cards in Your Hand

Imagine holding cards [3, 5, 8] already sorted in your left hand. You pick up the next card 6 from the table. You compare 6 with 8 — since 8 is bigger, you slide 8 right to make a gap. Next, you compare 6 with 5 — since 5 is smaller, you stop and insert 6 right into the gap between 5 and 8!

State Variables
i0
j0
key0
n8
Time: O(N²)Space: O(1)
LeetCode #147