Bubble sort: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Compare neighbors and move the largest remaining value to the end.

A completed pass fixes the largest remaining value at the right boundary; an unchanged pass permits early termination.

Open the 3D visualization

Bubble sort example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Bubble sort

Teaching adjacent swaps and loop invariants.

Choosing an alternative

Insertion sort handles nearly sorted arrays more efficiently; merge sort scales better.

Worked example: input and state changes

Example input

38, 12, 65, 8, 43, 27, 91, 19

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 10: Compare positions 5 and 6.

    State at this checkpoint
    {
      "values": [
        12,
        38,
        8,
        43,
        27,
        65,
        91,
        19
      ],
      "active": [
        5,
        6
      ]
    }
  3. Step 21: Swap neighbors.

    State at this checkpoint
    {
      "values": [
        12,
        8,
        38,
        27,
        43,
        19,
        65,
        91
      ],
      "active": [
        5,
        6
      ]
    }
  4. Step 31: Compare positions 1 and 2.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        27,
        38,
        19,
        43,
        65,
        91
      ],
      "active": [
        1,
        2
      ]
    }
  5. Step 41: Complete: values are in ascending order.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": []
    }

Expected final state

Complete: values are in ascending order.

Complexity and input bounds

Family: Sorting. Time complexity: O(n²).

Space complexity: O(1) auxiliary space.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers between 0 and 99.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

The early-exit version has a linear best case, despite quadratic worst-case work.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n) {
    for (let end = n - 1; end > 0; end--) {
        let changed = 0;
        for (let j = 0; j < end; j++) {
            if (a[j] > a[j + 1]) { let t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; changed = 1; }
        }
        if (changed == 0) { break; }
    }
    return 0;
}

Source

No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.