Tiem Cong Nghe

Showing suggestions.

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

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

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

  1. Unzip insertion-sort-animation-vanilla.zip. Copy insertion-sort-animation.css and insertion-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="insertion-sort-animation.css" />
    <script src="insertion-sort-animation.js" defer></script>
    
  3. 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>
    
  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-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.

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

  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

    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