Tech Store

Showing suggestions.

Opening the store's index…
↑↓ to move↵ to openesc to close
Tech Store search
Animationv1.0.0~14 KB gzipped

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

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 bubble sort animation from the short video. It sorts the video's input, 5 2 8 1 7 3 6 4, and ends at exactly 25 comparisons and 14 swaps, 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 bubbleSort listing on the Bubble Sort algorithm page, line for line, so what you watch is what the page teaches.

Every pass is easy to follow. The two neighbours being compared light up with a j and j + 1 tag. When they are out of order the bigger bar hops over the smaller one and they trade places; the swapped flag turns on. The biggest value of the pass lands at the end, and the sorted tail grows from the right with a few bubbles. When a pass leaves swapped off, the sort stops there: the early exit, spelled out in the caption.

  • Counters for comparisons, swaps and pass, and a row per pass showing how many comparisons it made and how many of them swapped
  • Presets for the video's input, random, nearly sorted (so the early exit is easy to see) and reversed, 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 · 9

  • Animates bubbleSort with the swapped early exit exactly as the free algorithm page writes it, line for line in the code panel
  • The pair being compared lights up; on a swap the bigger bar hops over the smaller one, and the sorted tail grows from the right
  • The swapped flag is a switch on stage: it turns on with the first swap of a pass, and a pass that leaves it off stops the sort, with a caption that says so
  • Counters for comparisons, swaps and pass, plus a row per pass: the default input 5 2 8 1 7 3 6 4 ends at exactly 25 comparisons and 14 swaps
  • Presets for the video's input, random, nearly sorted (the early exit shows) and reversed, 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 14 KB gzipped (script and CSS together)

How to use it

Watched the video and want to use the animation? Pick your case below. Plain HTML takes about two minutes; no build step, no framework.

  1. Unzip bubble-sort-animation-vanilla.zip. Copy bubble-sort-animation.css and bubble-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="bubble-sort-animation.css" />
    <script src="bubble-sort-animation.js" defer></script>
    
  3. Wherever you want an animation, add an empty element with data-bubble-sort-animation:

    <!-- the video's input: 5 2 8 1 7 3 6 4 -->
    <div class="bubble-sort-animation" data-bubble-sort-animation></div>
    
    <!-- your own numbers, no input row and no code: small enough for a slide -->
    <div
      class="bubble-sort-animation"
      data-bubble-sort-animation
      data-bubble-sort-animation-values="9 4 7 1 8"
      data-bubble-sort-animation-inputs="false"
      data-bubble-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-bubble-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 video video (5 2 8 1 7 3 6 4), random, nearly-sorted or reversed; ignored when -values is given
-size 8 How many bars random, nearly-sorted and reversed 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

For a pass that stops early, try data-bubble-sort-animation-preset="nearly-sorted": one pass puts the pairs right and the next pass swaps nothing.

Elements added after the page loads (a tab, a modal, a client-side route): BubbleSortAnimation.init(container); it returns a function that undoes it. For full control, BubbleSortAnimation.createBubbleSortAnimation(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 bubble sort animation with the early exit: bars that cross when they swap, a sorted tail that grows, the swapped flag, comparison / swap / pass 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.
  • AnimationSorting

    Sorting Race

    Six sorting algorithms race side by side on the same data — one operation per tick, medals at the finish

    Free
  • AnimationSorting

    Quick Sort Animation

    Quick sort, step by step: the pivot, Hoare's two pointers and a call stack that stays shallow

    Free