Tiem Cong Nghe

Showing suggestions.

Opening the store's index…
↑↓ to move↵ to openesc to close
Tiem Cong Nghe search
Animationv1.0.0~17 KB gzipped

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

FreeFree
Format

.zip · HTML / CSS / JS · v1.0.0

Free — no account, no email. Always the newest version.

Live preview

Version
Preview width
Zoom

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 siftDown call 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.

  1. Unzip heap-sort-animation-vanilla.zip. Copy heap-sort-animation.css and heap-sort-animation.js next to your page (or into your site's assets folder).

  2. 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>
    
  3. 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>
    
  4. 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.

Ask about this product

Not sure it fits your project? Ask — the answer comes from this page.

Answers are written by AI from this page and can be wrong. The page itself is what counts.

Changelog

  1. 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.

  2. 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.

Your rating
Never shown. Only used if we need to reach you about your review.
0 / 2000

Your name and review are shown on this page; your email is not. See the terms.

  • 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