Insertion sort

Time complexity: O(n²), O(n) if nearly sorted.

Result

Examples

How it works

Insertion sort takes each item in turn and slides it left into its place among the items already sorted, like sorting a hand of cards.

When to use it

Small or nearly sorted lists. Fast sorts often switch to it for short runs.

Other algorithms