Skip to content
StackDecode
SortingAnimated

Insertion Sort

Insert each item into the already-sorted prefix.

Time

O(n²), O(n) if sorted

Extra space

O(1)

Use it for

Small or nearly-sorted data.

How it works

  1. Treat the first item as a sorted prefix.
  2. Take the next item and slide it left past every larger item until it fits.
  3. The sorted prefix grows by one each time.
  4. Very fast on nearly-sorted data.

Watch it run

Loading animation…