RK DSA Studio

Insertion sort

Grow a sorted prefix by moving each new item left past larger neighbors.

Remember: Sorted prefix → insert next.

Example: [3,1,2] → [1,3,2] → [1,2,3].

Time: O(n²) worst; O(n) best. Auxiliary space: O(1).

Formula: Sorted prefix + adjacent swaps → a larger sorted prefix

Up to 15 integers. Stable: only strictly inverted neighbors swap. # labels show original positions. Counters count comparisons, swaps and array writes.

Complexities describe the underlying algorithm; tutorial snapshots and rendering can use additional resources.

The interactive workspace loads here when JavaScript is available. You can still browse the learning notes and links above without it.