Insertion Sort visualization
Grow a sorted prefix by inserting each new value into its correct slot.
Like sorting playing cards in your hand: take the next card and slide it left past every larger card.
Very fast on nearly-sorted input (each value moves only a little), which is why hybrid sorts like Timsort use it for small runs.
Complexity
Time: best O(n), average O(n²), worst O(n²). Space: O(1). Difficulty: Beginner.
Pseudocode
procedure insertionSort(A):
for i ← 1 to n−1:
key ← A[i]; j ← i − 1
while j ≥ 0 and A[j] > key:
A[j+1] ← A[j]; j ← j − 1
A[j+1] ← key
return AMore sorting algorithms
- Bubble Sort — Repeatedly swap adjacent out-of-order pairs; the largest value bubbles to the end each pass.
- Selection Sort — Find the minimum of the unsorted part and swap it to the front.
- Cocktail Shaker Sort — Bubble sort that alternates direction, moving large values right and small values left.
- Shell Sort — Insertion sort over shrinking gaps, so values travel long distances early.
- Merge Sort — Divide in half, sort each half recursively, then merge the two sorted halves.
- Quick Sort — Partition around a pivot so smaller values go left and larger go right, then recurse.
- Heap Sort — Build a max-heap, then repeatedly move the root (maximum) to the end and re-heapify.
- Counting Sort — Count occurrences of each value, then rebuild the array from the counts.
- Radix Sort (LSD) — Sort by the ones digit, then tens, then hundreds — each pass is a stable bucket sort.