Skip to content
StackDecode
SortingAnimated

Merge Sort

Split, sort halves, merge. Stable and predictable.

Time

O(n log n)

Extra space

O(n)

Use it for

Linked lists, external sorting, stability needed.

How it works

  1. Split the list in half, again and again, until each piece has one item.
  2. Merge two sorted pieces by repeatedly taking the smaller front item.
  3. Merging goes back up until one sorted list remains.
  4. Always O(n log n); uses extra memory for merging.

Watch it run

Loading animation…