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
.zip · HTML / CSS / JS · v1.0.0
Free — no account, no email. Always the newest version.
Live preview
About
This is the interactive version of heap sort, the animation behind
the Heap Sort algorithm page. The code in the panel is that page's
heapSort, siftDown and biggerChild, line for line, so what you watch is what the page teaches,
and the default input is the page's worked example, 7 2 9 4 3 8 1.
The trick of heap sort is that an array is a tree: the children of index i sit at 2i + 1 and
2i + 2. So the stage shows the same values twice, as bars and as the complete binary tree they
stand for, and keeps them in sync. When a value sinks, the two swapped nodes cross in the tree and
the two bars cross in the array at the same moment.
Phase 1, Build heap: from the last parent back to the root, each value is sunk with siftDown.
You see the parent (the value that sinks), its two children, the comparison that picks the bigger
one, and the path lighting up edge by edge. Phase 2, Sort: the root, the biggest value left, is
swapped with the last one and locks in on the right in green; the heap meter shrinks, and the new
root sinks inside the smaller heap.
- Counters for comparisons, swaps and sift-downs, and a row per
siftDowncall showing how much work it did - Presets for the walkthrough, random, sorted, reversed (already a heap, so the build does nothing) and duplicates (ties go left, an equal parent stays), or type your own numbers
- A full player with a scrubber and 0.5×–4× speed, built to look good on a slide or in a recording
- Every colour, radius, font and timing is a CSS custom property; light and dark follow your page
- Nothing to install: one CSS file and one script, or one React component
Features · 10
- Animates heapSort, siftDown and the biggerChild helper exactly as the free algorithm page writes them, line for line in the code panel
- The same values twice, kept in sync: bars in an array and nodes in the complete binary tree (children of i at 2i + 1 and 2i + 2, lines drawn), with the swapped pair crossing in both views at once
- The sift-down path lights up from parent to bigger child, with a marker on the sinking value and its two children; ties go left and an equal parent stays, and the captions say so
- A phase label (Build heap, Sort) and a heap-size meter: the heap boundary shrinks with every extract while the sorted tail locks in green on the right, in the tree as well
- Counters for comparisons, swaps and sift-downs, plus a row per siftDown call: the walkthrough [7, 2, 9, 4, 3, 8, 1] from the explanation page plays out move by move
- Presets for the page's walkthrough, random, sorted, reversed (already a heap) and duplicates, or type your own numbers (up to 15, so the tree stays readable on a phone)
- Every step in plain words in a caption, with the running lines lit in the code
- Player: play, pause, step back and forward, reset, scrub, 0.5×–4× speed, optional loop
- Plain HTML/CSS/JS and React (Next.js ready) builds, light and dark
- Zero dependencies, about 17 KB gzipped (script and CSS together)
How to use it
Want the heap sort animation in your lesson, slides or blog? Pick your case below. Plain HTML takes about two minutes; no build step, no framework.
-
Unzip
heap-sort-animation-vanilla.zip. Copyheap-sort-animation.cssandheap-sort-animation.jsnext to your page (or into your site's assets folder). -
In your page's
<head>, link the stylesheet and the script once:<link rel="stylesheet" href="heap-sort-animation.css" /> <script src="heap-sort-animation.js" defer></script> -
Wherever you want an animation, add an empty element with
data-heap-sort-animation:<!-- the page's walkthrough: 7 2 9 4 3 8 1 --> <div class="heap-sort-animation" data-heap-sort-animation></div> <!-- your own numbers, no input row and no code: small enough for a slide --> <div class="heap-sort-animation" data-heap-sort-animation data-heap-sort-animation-values="9 4 7 1 8 2 6" data-heap-sort-animation-inputs="false" data-heap-sort-animation-code="false" ></div> -
Open the page. It plays on its own; click it once to give it the keyboard, then Space plays or pauses, ← / → step, Home starts again.
Options are data attributes, all starting with data-heap-sort-animation-:
| Attribute | Default | What it does |
|---|---|---|
-values |
the preset's | Your own numbers, separated by spaces or commas: whole numbers from -999 to 999, up to 15 |
-preset |
walkthrough |
walkthrough (7 2 9 4 3 8 1), random, sorted, reversed or duplicates; ignored when -values is given |
-size |
9 |
How many values random, sorted, reversed and duplicates have (2–15) |
-seed |
7 |
Which seeded array: the same seed always gives the same one |
-speed |
1 |
0.25 to 4 |
-autoplay |
true |
Plays on its own once loaded (never with reduced motion) |
-loop |
false |
Starts again after a pause on the result |
-code |
true |
false hides the code panel |
-inputs |
true |
false hides the row of presets and the box for your own numbers |
To show ties, try data-heap-sort-animation-preset="duplicates": between two equal children the
left one wins, and a parent that equals its bigger child stays put.
Elements added after the page loads (a tab, a modal, a client-side route):
HeapSortAnimation.init(container); it returns a function that undoes it. For full control,
HeapSortAnimation.createHeapSortAnimation(element, options) returns a handle with
setValues, setPreset, shuffle, player (play, pause, stepForward, stepBack, seek,
reset, setSpeed, setLoop) and destroy.
- Slides built from HTML (reveal.js, Slidev, Marp's HTML export): put the
<link>and<script>in the deck's head and the<div>on the slide. Use-inputs="false",-code="false"and-autoplay="false"for a clean slide, then press Space when you reach it. - A blog or a static site (Hugo, Jekyll, Astro, Eleventy, WordPress's Custom HTML block):
upload the two files, add the
<link>and<script>to the page template (or once per post) and paste the<div>into the post. Several animations on one page are fine. - An LMS or any page that lets you add HTML (Moodle, Canvas, a Notion-style embed that accepts
an iframe): host the two files plus a small page that contains the
<div>, and embed that page with an<iframe>. - PowerPoint or Keynote: open the page, pick a speed and record your screen, or take a
screenshot of the frame you want; use
-autoplay="false"and step with →.
It sorts the same example as the explanation on the algorithm page, so you can read the table there and then let your students step through it. The code panel is the JavaScript listing on that page, so the two always agree.
Copy HeapSortAnimation.tsx, heap-sort-animation.css and the core folder into your project:
import { HeapSortAnimation } from "./HeapSortAnimation";
<HeapSortAnimation />
<HeapSortAnimation values={[9, 4, 7, 1, 8, 2, 6]} inputs={false} code={false} autoplay={false} />
<HeapSortAnimation preset="duplicates" size={10} seed={3} loop onFrame={(frame) => console.log(frame.caption)} />The props are the options above, camel-cased (values, preset, size, seed, speed,
autoplay, loop, code, inputs), plus onFrame(frame, index), called with every frame drawn.
Changing a prop starts the sort again. Every other prop goes to the root <div> (className,
id, aria-*…). React 18 or 19.
The file 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 .heap-sort-animation (or a class of your own):
| Property | What it changes |
|---|---|
--heap-node, --heap-parent, --heap-kid, --heap-swap, --heap-sorted |
Values in the heap (violet), the sinking value (cyan), its children and the bigger child (amber: hollow while compared, filled once picked), the swap (pink) and the sorted tail (green), in the bars and in the tree |
--heap-bg, --heap-surface, --heap-surface-strong, --heap-blur |
The background and the frosted glass |
--heap-edge, --heap-shadow, --heap-code-bg |
Borders, shadows, the code panel |
--heap-text, --heap-muted, --heap-on-accent, --heap-ring |
Text, secondary text, text on the coloured nodes and buttons, focus ring |
--heap-font, --heap-mono |
The fonts for words and for code and numbers |
--heap-radius, --heap-bar-radius |
Corners of the player and of the bars |
--heap-duration, --heap-ease |
How fast and how softly bars and nodes cross and glide |
Colours use light-dark(): they follow color-scheme on your page. With
prefers-reduced-motion, nothing plays on its own and frames change instantly; every control
still works.
- Nothing appears: the element needs
data-heap-sort-animation, and both the CSS and the script must load (check the paths in your browser's network tab). - Space or the arrows do nothing: click the animation once first; the shortcuts work while it has focus, and never while you type in the numbers box.
- My numbers changed: values are rounded to whole numbers, kept between -999 and 999, and cut at 15 so the tree stays readable. The box shows what was sorted.
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 the step-by-step heap sort animation: the array and the binary tree it stands for side by side, a shrinking heap and a sorted tail that locks in, the sift-down path, comparison / swap / sift-down counters, live code, player controls, your own numbers and presets, light and dark.
- 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
Merge Sort Animation
Merge sort, step by step: halves splitting level by level, then two sorted runs merging through one buffer
Free - 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
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




