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
- Treat the first item as a sorted prefix.
- Take the next item and slide it left past every larger item until it fits.
- The sorted prefix grows by one each time.
- Very fast on nearly-sorted data.
Watch it run
Loading animation…