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
.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 the insertion sort animation. It sorts the worked example of
the algorithm page, 5 2 8 1 7 3 6 4, and ends at exactly 19 comparisons and 14 shifts, but you
can pause it, step through it, slow it down and change the numbers. The step-by-step code in the
panel is the insertionSort listing on the Insertion Sort algorithm page,
line for line, so what you watch is what the page teaches.
The picture is a hand of cards. Each pass lifts the key out of the row and holds it above the
bars as a card. Every value bigger than the key slides one slot right underneath it, and the dashed
gap where the key used to be travels left. When the next value is not bigger (or j runs off
the front), the card drops into the gap and the sorted prefix grows by one. Nothing is ever
swapped: a shift is one write, a swap is two, and a meter beside the code counts both.
- Counters for comparisons, shifts, pass and inversions left (each shift removes exactly one), and a row per key showing how many comparisons it took and how many shifted
- Presets for the worked example, random, nearly sorted (where insertion sort shines), reversed (the worst case) and duplicates, whose equal values wear letters so you can watch the sort stay stable; 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 insertionSort exactly as the free algorithm page writes it, line for line in the code panel
- The key lifts out of the row as a playing card and hovers above it; every bigger value slides one slot right underneath, and the dashed gap travels left until the card drops in
- Shifting, not swapping: one write per moved value, with a meter that sets the writes against what a swapping sort would make
- A sorted prefix that grows by one each pass, a j pointer, the key in hand and a j + 1 tag for the gap, with j = -1 shown when the loop runs off the front
- Counters for comparisons, shifts, pass and inversions left, plus a row per key: the worked example 5 2 8 1 7 3 6 4 ends at exactly 19 comparisons and 14 shifts, one shift per inversion
- Presets for the worked example, random, nearly sorted (where it shines), reversed (the worst case) and duplicates, whose equal values wear letters to show the sort is stable; or type your own numbers (up to 24)
- 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 16 KB gzipped (script and CSS together)
How to use it
Want the animation in a lesson, a slide deck or a blog post? Pick your case below. Plain HTML takes about two minutes; no build step, no framework.
-
Unzip
insertion-sort-animation-vanilla.zip. Copyinsertion-sort-animation.cssandinsertion-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="insertion-sort-animation.css" /> <script src="insertion-sort-animation.js" defer></script> -
Wherever you want an animation, add an empty element with
data-insertion-sort-animation:<!-- the worked example of the algorithm page: 5 2 8 1 7 3 6 4 --> <div class="insertion-sort-animation" data-insertion-sort-animation></div> <!-- your own numbers, no input row and no code: small enough for a slide --> <div class="insertion-sort-animation" data-insertion-sort-animation data-insertion-sort-animation-values="9 4 7 1 8" data-insertion-sort-animation-inputs="false" data-insertion-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-insertion-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 24 |
-preset |
example |
example (5 2 8 1 7 3 6 4), random, nearly-sorted, reversed or duplicates; ignored when -values is given |
-size |
8 |
How many bars random, nearly-sorted, reversed and duplicates have (2–24) |
-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 see where insertion sort shines, try data-insertion-sort-animation-preset="nearly-sorted": almost
every key is compared once and stays put. For stability, try -preset="duplicates": equal values wear
letters (a, b, c) and keep their order.
Elements added after the page loads (a tab, a modal, a client-side route):
InsertionSortAnimation.init(container); it returns a function that undoes it. For full control,
InsertionSortAnimation.createInsertionSortAnimation(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 worked example as the algorithm page, so you can read the page first and then let your students step through it. The code panel is the JavaScript listing on that page, so the two always agree.
Copy InsertionSortAnimation.tsx, insertion-sort-animation.css and the core folder into your project:
import { InsertionSortAnimation } from "./InsertionSortAnimation";
<InsertionSortAnimation />
<InsertionSortAnimation values={[9, 4, 7, 1, 8]} inputs={false} code={false} autoplay={false} />
<InsertionSortAnimation preset="nearly-sorted" 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 .insertion-sort-animation (or a class of your own):
| Property | What it changes |
|---|---|
--insertion-bar, --insertion-compare, --insertion-shift, --insertion-sorted |
The bars not looked at yet (violet), the value being compared (cyan), the values shifted right (pink) and the sorted prefix (green) |
--insertion-key |
The key card, its dashed gap and the beam above it (amber), and the number colour in the code |
--insertion-card-face, --insertion-card-text |
The face of the key card (a gradient) and the ink on it |
--insertion-bg, --insertion-surface, --insertion-surface-strong, --insertion-blur |
The background and the frosted glass |
--insertion-edge, --insertion-shadow, --insertion-code-bg |
Borders, shadows, the code panel |
--insertion-text, --insertion-muted, --insertion-on-accent, --insertion-ring |
Text, secondary text, text on the coloured tags and buttons, focus ring |
--insertion-font, --insertion-mono |
The fonts for words and for code and numbers |
--insertion-radius, --insertion-bar-radius |
Corners of the player and of the bars |
--insertion-duration, --insertion-ease |
How fast and how softly bars cross and slide |
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-insertion-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 24. 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 insertion sort animation: the key lifted out of the row as a card, bigger bars sliding right into a gap that travels left, a sorted prefix that grows each pass, comparison / shift / pass / inversion counters, a writes meter, live code, player controls, your own numbers and presets (including duplicates that show stability), 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
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




