INTERMEDIATE · 10 MIN · FREE LESSON
Insertion sort
Explore insertion sort through a visual, step-by-step model.
Loading interactive lesson…
INTERMEDIATE · 10 MIN · FREE LESSON
Explore insertion sort through a visual, step-by-step model.
Loading interactive lesson…
Start with the selected input.
A sorted input needs one comparison per inserted value; reversed inputs shift every previous value.
Measured reads + comparisons, seed 42. Plotted sample sizes respect each lesson’s explicit input limits.
Keyboard: Space play/pause · ←/→ step · R restart · 1–5 speed. Reduced motion disables transitions.
Insert a playing card into your already ordered hand by sliding larger cards aside.
Make room for the next value. Run best, average, worst and seeded random inputs; the animation is a reversible execution trace.
Pause at each decision and predict the operation before revealing its result. Input arrays are limited to 64 bounded integers.
A sorted prefix needs one comparison per insertion. Reverse order can shift all earlier keys, giving quadratic work.
Choose a language in the code panel. The highlighted stage follows the trace; counters record the visualized reads, writes, comparisons and swaps.
Use it for small arrays, nearly sorted inputs and short runs inside hybrid sorts.
Shift only strictly larger keys to preserve stability; do not lose the held key.
Test the invariant and complexity below, then compare another algorithm on the same input.
MAKE IT STICK
Progress saves on this device and merges with your account after sign-in.
0 / 5000 · Saved on this device; synced when signed in.