← back to algorithms

dedicated visualizer

Merge Sort

Recursively splits the array and merges sorted halves into a fully ordered result. This page keeps the runner, chart, and controls focused on a single algorithm so the walkthrough feels calmer than the overview page.

sortingintermediatebest O(n log n)worst O(n log n)space O(n)
divide and conquermergingstablerecursion

session controls

Compare this algorithm against a related one, turn on quiz mode, or keep the current state in a shareable URL.

current shareable URL

Copy the URL to preserve this exact dataset, target, compare mode, and quiz state.

browse more

Want a different problem or visual mode? Jump back to the catalog and open another dedicated page.

open catalog

scenario presets

Load a focused input that reveals a specific behavior quickly instead of hand-editing every value first.

dataset controls

Use your own array, randomize a fresh one, or restore defaults. The same dataset is shared by both panels in compare mode.

Enter up to 12 integers. Values are normalized to the range 1–99 for clean visualization.

step 1 / 570% complete

current action

split range

8
8
idx 0
3
3
idx 1
5
5
idx 2
1
1
idx 3
9
9
idx 4
6
6
idx 5
2
2
idx 6
7
7
idx 7

run summary

Finished in 57 steps. Final order: 1, 2, 3, 5, 6, 7, 8, 9.

comparisons

17

writes

24

steps

57

current explanation

Merge sort repeatedly splits the array into smaller ranges before merging them back together.

simple explanation

Break the array apart, sort the tiny pieces, then merge them.

pseudocode

1split the range into two halves
2sort both halves recursively
3compare the front values of each half
4write the smaller value into the merged output
5copy any leftovers into the output

complexity card

best

O(n log n)

average

O(n log n)

worst

O(n log n)

space

O(n)

algorithm notes

intuition

Small sorted pieces are easy to combine, so merge sort focuses on creating those pieces first.

tradeoffs

  • Predictable O(n log n) time.
  • Needs extra memory for merging.
  • Stable, which helps when equal keys matter.

when to use it

Use when stable ordering matters and extra memory is acceptable for consistent performance.

interview tips

  • Explain the divide-and-conquer recurrence and why it leads to O(n log n).
  • Mention linked-list merge sort as a common variant.

what I learned building this

typed definitions

One algorithm schema now drives the catalog, counters, pseudocode, notes, and visual modes, which keeps the UI consistent as the lab grows.

replay over mutation

Precomputed steps made it much easier to synchronize explanations, metrics, quiz prompts, and scrubber playback without hidden state drifting out of sync.

portfolio framing

Shareable URL state, compare mode, and responsive layouts mattered as much as the algorithm logic because this page needs to teach clearly and still feel polished as a product.

more in this lane

Want a different take on the same problem family? These stay in the same category but change the strategy.