Insertion Sort
Grow a sorted prefix by shifting larger values right and inserting each key into its final local position.
Core idea
Treat the left prefix as sorted. Remove the next key, shift every larger prefix value one step right, and insert the key into the resulting gap.
Read the visualization
The key marker stays attached to the value being inserted. Comparisons move left through the prefix, and each shift visibly opens the gap that eventually receives the key.
Remove the next key from the unsorted suffix and open a position in the sorted prefix.
Complexity and tradeoffs
Time: Average/worst O(n^2); best O(n). Space: O(1). Stable, adaptive, and effective for small or nearly sorted inputs.
Where it fits
Insertion Sort is a strong base case for hybrid sorts and performs well on small or nearly sorted arrays. Stable insertion also makes it useful inside more specialized algorithms.