Overview
Insertion Sort grows a sorted prefix one item at a time. For each new item, it shifts larger earlier items to the right and places the item into the gap.
Sorts a list by inserting each item into the correct position within the already-sorted prefix.
Compact metadata for implementation and discovery.
Insertion Sort grows a sorted prefix one item at a time. For each new item, it shifts larger earlier items to the right and places the item into the gap.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n)
Average
O(n^2)
Worst
O(n^2)
Space
O(1)
Show a sorted prefix growing as each item slides into place.
Insertion sort is stable when equal keys are not moved past one another.
What each reference supports for this entry.
Textbook support for Insertion Sort invariants, recurrences, relaxation rules, or asymptotic analysis.
Open sourceGood next jumps from the relationship graph, ranked for useful discovery.
Sorting · Extended
Library sort is a gapped insertion-sort variant that leaves empty slots for faster insertions.
Compare with thisSorting · Core
Bubble sort and insertion sort are simple stable quadratic sorts with different local-movement behavior.
Compare with thisSorting · Core
Both are simple quadratic sorts, but insertion sort adapts better to nearly sorted data.
Compare with thisSorting · Extended
Patience sorting builds piles instead of inserting directly into one sorted array.
Compare with thisBoth are simple quadratic sorts, but insertion sort adapts better to nearly sorted data.
Tim Sort uses insertion sort on small runs.