Merge Sort Animation
Merge sort, step by step: halves splitting level by level, then two sorted runs merging through one buffer
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
Merge sort is easy to say and hard to see: cut the array in half until pieces are one value, then merge sorted pieces back up. This player shows both halves of that. The array splits level by level while a recursion tree grows under it, and every merge lifts its two halves onto a buffer shelf, then drops the smaller front value back into the array while the pointers i, j and k walk along. Finished stretches turn green, and the call stack beside it shows it never goes deeper than ⌈log₂ n⌉.
- Exactly the listing on the Merge Sort page, and its code lights up line by line as it runs
- Captions in plain words ("buffer[i] = 3 <= buffer[j] = 3? Yes, they are equal, and the left one goes first") for every step
- Random, sorted, reversed, nearly sorted or many-duplicates input from a seed, or your own numbers
- Equal values carry a letter, so you can watch the sort stay stable
- Play, step, scrub and change speed with the mouse or the keyboard; it loops for a slide or a video
- Every colour, radius and timing is a CSS custom property; light and dark follow your page
- Nothing to install: one CSS file and one small script, or one React component
Features · 11
- Follows the algorithm page's listing exactly: top-down halves, one buffer made once, a tie going to the left half
- The array splits into halves level by level, with a recursion tree under it that fills in as the calls are made and turns green as they return
- A merge lifts both halves onto a buffer shelf, then drops the smaller front into place with the i, j and k pointers walking along
- The listing's own code with the running line lit, and a call stack with a depth gauge against ⌈log₂ n⌉
- A caption in plain words for every step, plus comparisons, writes and stack depth
- Random, sorted, reversed, nearly sorted or many-duplicates presets from a seed you can replay, or type your own numbers
- Equal values carry a letter (3a, 3b), so you can watch the sort stay stable
- Play, pause, step, scrub and 0.5×–4× speed; a small celebration when the array is sorted
- Dark aurora look that also works light; fits a phone, a blog post or a 16:9 slide
- Plain HTML/CSS/JS and React (Next.js ready) builds
- Zero dependencies, about 19 KB gzipped (script and styles together)
How to use it
Link the stylesheet and the script once, then mark an empty element with
data-merge-sort-animation. It builds the whole player inside it:
<link rel="stylesheet" href="merge-sort-animation.css" />
<script src="merge-sort-animation.js" defer></script>
<div data-merge-sort-animation data-merge-sort-size="12" data-merge-sort-loop></div>
<!-- your own values, without the extra panels: good for a slide -->
<div
data-merge-sort-animation
data-merge-sort-values="8, 3, 5, 3, 9, 1"
data-merge-sort-inputs="false"
data-merge-sort-panels="false"
></div>Options as data attributes (a boolean is on when present, off when it says "false"):
| Attribute | Default | What it does |
|---|---|---|
data-merge-sort-values |
— | Your numbers, separated by commas or spaces (48 at most); replaces the generated array |
data-merge-sort-size |
12 |
Bars in a generated array, 2 to 48 |
data-merge-sort-scenario |
random |
random, sorted, reversed, nearly or duplicates |
data-merge-sort-seed |
7 |
Which generated array: the same seed always draws the same one |
data-merge-sort-speed |
1 |
0.25 to 4 |
data-merge-sort-autoplay |
on | Starts playing on load (never with prefers-reduced-motion) |
data-merge-sort-loop |
off | Starts over after the last step |
data-merge-sort-inputs |
on | The size and preset pickers, "New input" and the field for your own numbers |
data-merge-sort-panels |
on | The code and call-stack panels |
Players added after the page loads: MergeSortAnimation.init(container) — it returns a function
that undoes it. To drive one from your own code (a slide clicker, say), build it yourself:
const player = MergeSortAnimation.create(element, { values: [5, 1, 4, 2, 3], autoplay: false });
player.stepForward();
player.seek(20);
player.setValues([9, 8, 7]);
player.destroy(); // the element is given back as it wasThe handle also has play, pause, stepBack, reset and frameCount.
With focus anywhere in the player, Space plays or pauses, ← / → step and Home goes back to the start. In the numbers field the keys stay the field's own; Enter sorts what you typed.
Copy MergeSortAnimation.tsx, merge-sort-animation.css and the core folder into your project:
import { MergeSortAnimation } from "./MergeSortAnimation";
<MergeSortAnimation size={16} scenario="duplicates" loop />
<MergeSortAnimation values={[8, 3, 5, 3, 9, 1]} inputs={false} panels={false} />The props are the options above: values, size, scenario, seed, speed, autoplay, loop,
inputs, panels, plus any <div> prop (className, id, aria-label…). A new array with the
same numbers does not restart the player. React 18 or 19.
The component starts with "use client", so use it from any page or server component. With the
App Router nothing else is needed. With the Pages Router, move the CSS import into pages/_app.
Set these on .merge-sort-animation (or a class of your own):
| Property | What it changes |
|---|---|
--mergesort-surface, --mergesort-panel, --mergesort-stage, --mergesort-border |
The glass, the panels, the stage and their edges |
--mergesort-aurora-1, --mergesort-aurora-2, --mergesort-aurora-3 |
The coloured glows behind it |
--mergesort-text, --mergesort-muted |
Text and secondary text |
--mergesort-bar, --mergesort-bar-2 |
A bar's gradient, top to bottom, before it is merged |
--mergesort-left, --mergesort-left-2 |
The left half of a merge and its pointer i |
--mergesort-right, --mergesort-right-2 |
The right half of a merge and its pointer j |
--mergesort-write, --mergesort-write-2 |
The pointer k, the value just written and the buffer's merge window |
--mergesort-sorted, --mergesort-sorted-2 |
Bars in a finished sorted run, and calls that have returned |
--mergesort-range, --mergesort-on-mark |
The range bracket, and text on coloured tags |
--mergesort-accent, --mergesort-accent-2, --mergesort-ring |
Play button, scrubber, active code line, focus ring |
--mergesort-radius, --mergesort-bar-radius |
Corners |
--mergesort-font, --mergesort-mono |
Text and code fonts |
--mergesort-array-h |
How tall the array's lane is (the buffer shelf is two thirds of it) |
--mergesort-duration, --mergesort-ease |
How long a bar takes to move at 1× (it scales with the speed) |
Colours use light-dark(): they follow color-scheme on your page.
Ask about this product
Not sure it fits your project? Ask — the answer comes from this page.
Changelog
1.0.0First releaseLatest
Plain HTML/CSS/JS and React builds of a step-by-step merge sort player (top-down halves, one shared buffer, ties going left so the sort stays stable) with a recursion tree, the listing's code, call stack, counters, seeded presets, your own numbers, keyboard and reduced-motion support.
- Fixes and new features land behind the same download, which always serves the latest version.
Reviews
No reviews yet
Used it? Be the first to say how it went.
Write a review
How did it work for you? Reviews appear once we have read them.
You might also like
- AnimationSorting
Insertion Sort Animation
A card lifted out of the row, bigger bars sliding right underneath, a gap that travels left: insertion sort as shifting, never swapping
Free - AnimationSorting
Heap Sort Animation
The array and the binary tree it stands for, side by side: every sink moves both at once, the heap shrinks and the sorted tail locks in
Free - AnimationSorting
Bubble Sort Animation
The interactive version of the short video: bars that cross when they swap, a sorted tail that grows, and the early exit
Free





