Skip to content
Computing Simple #bubblesort#sorting#beginner

Bubblesort: Pass-by-Pass

Adjacent-swap passes bubble the largest element to the end each round. O(n^2) but visually obvious.

A free, animated bubblesort: pass-by-pass you can read here or embed on any website, from Scrollchart.

Bubblesort: Pass-by-Pass

Bubblesort: Pass-by-PassEach pass locks one more element (green); 9 passes x ~9 compares = O(n²) = 45 total comparisons for n=10UnsortedPass 19 comparesPass 28 comparesPass 37 comparesPass 46 compares

A bar array of 10 elements sorts via repeated adjacent comparisons and swaps. Each pass bubbles the largest unsorted element to its final position at the right. Five successive snapshots (unsorted through pass 4) are stacked vertically so the progressive locking of elements is visible all at once. A right-hand panel derives the O(n^2) cost as the sum n-1 + n-2 + ... + 1 = n(n-1)/2, with concrete counts at n=10, 100, and 1,000.

Good for

  • Introductory CS courses teaching adjacent-swap sorting
  • Complexity-analysis blogs deriving O(n^2) from first principles
  • Side-by-side comparisons with O(n log n) sorts to make the quadratic penalty visceral

Source & accuracy

This bubblesort: pass-by-pass is an editorial illustration built to represent the concept accurately. Where it shows figures, they are typical or representative values chosen to make the relationship clear, not a single underlying dataset. The diagram and its explainer are reviewed and maintained centrally, and updated over time as understanding improves.

Adjacent swaps propagate the largest element rightward

Bubble sort repeatedly scans the array, comparing adjacent pairs and swapping if out of order. After the first pass, the largest element has bubbled to the end. After the second pass, the second-largest is in its final position. After n-1 passes, the array is sorted. Each pass takes O(n) time, giving O(n^2) total. The algorithm is rarely used in practice because faster sorts exist, but it remains pedagogically useful for learning loop structure and sorting invariants.

Optimization and the concept of comparison-based sorting

A small optimization: after each pass, one fewer comparison is needed (since we know the last k elements are in final position). A bigger optimization: stop early if a pass makes no swaps (the array is sorted). Despite these tweaks, bubble sort remains O(n^2) in the worst case. It illustrates a fundamental fact about comparison-based sorts: you cannot do better than O(n log n) for the general case because you must resolve O(n log n) bits of ordering information through comparisons.

Embed this diagram

Add this animated bubblesort: pass-by-pass to your own site. Copy one line of HTML, or use the embed builder for theme and sizing options.

Reference

What this is
A free, embeddable, animated bubblesort: pass-by-pass for any website.
Who uses it
Developer blogs, CS educators.
How to embed
Copy one line of HTML. No signup. No watermark. Works in WordPress, Webflow, Ghost, Substack, plain HTML.
File size
iframe embed, ~80 KB gzipped (loads on demand, does not block your page paint).
License
Free forever. Editorial explainer text included; updated centrally over time.

Embed format options

Copy the universal HTML snippet, the WordPress shortcode, or an iframe fallback - see the WordPress plugin page for details. Any format keeps the same Core Web Vitals profile and the same explainer text.

Embed snippet
<div data-scrollchart="bubblesort-passes" data-scrollchart-v="1"></div>
<script src="https://scrollchart.com/embed.js" async></script>

Frequently asked questions

Where can I get a free animated "Bubblesort: Pass-by-Pass" for my website?
Scrollchart provides "Bubblesort: Pass-by-Pass" as a free, embeddable animated diagram you can add to any website with one line of HTML. No signup is required and there is no watermark. The diagram and its explainer text are served from scrollchart.com, so the embed stays current without any maintenance on your end.
How do I embed a bubblesort: pass-by-pass in a developer or tech blog?
Copy the one-line snippet from the Scrollchart diagram page and paste it into your post HTML. It works in any static site generator, CMS, or hand-coded HTML page. The embed is a thin loader, not an iframe, so the content is fully in your DOM.