{
  "slug": "bubble-sort",
  "title": "Bubble Sort",
  "category": "Sorting",
  "difficulty": "easy",
  "complexity": {
    "time": "O(n²)",
    "space": "O(1)"
  },
  "idea": "Repeatedly compare neighbours and swap them if out of order. Each pass carries the largest remaining value to its final place at the right end.",
  "code": [
    "for pass in 0 .. n-2:",
    "    for j in 0 .. n-2-pass:",
    "        if a[j] > a[j+1]:",
    "            swap a[j], a[j+1]"
  ],
  "example": {
    "array": [
      5,
      2,
      9,
      1,
      6,
      3
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 29,
  "steps": [
    {
      "i": 0,
      "op": "say",
      "caption": "Bubble the largest remaining value to the right on every pass.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "array": [
        5,
        2,
        9,
        1,
        6,
        3
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [],
      "regions": []
    },
    {
      "i": 1,
      "op": "compare",
      "caption": "Compare neighbours 5 and 2.",
      "note": "5 > 2 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 1
      },
      "array": [
        5,
        2,
        9,
        1,
        6,
        3
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 2,
      "op": "swap",
      "caption": "Swap 5 and 2.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 1,
        "swaps": 1
      },
      "array": [
        2,
        5,
        9,
        1,
        6,
        3
      ],
      "focus": [
        "swap",
        "swap",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 3,
      "op": "compare",
      "caption": "Compare neighbours 5 and 9.",
      "note": "5 ≤ 9 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 2,
        "swaps": 1
      },
      "array": [
        2,
        5,
        9,
        1,
        6,
        3
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 4,
      "op": "compare",
      "caption": "Compare neighbours 9 and 1.",
      "note": "9 > 1 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 3,
        "swaps": 1
      },
      "array": [
        2,
        5,
        9,
        1,
        6,
        3
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 5,
      "op": "swap",
      "caption": "Swap 9 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 3,
        "swaps": 2
      },
      "array": [
        2,
        5,
        1,
        9,
        6,
        3
      ],
      "focus": [
        null,
        null,
        "swap",
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 6,
      "op": "compare",
      "caption": "Compare neighbours 9 and 6.",
      "note": "9 > 6 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 4,
        "swaps": 2
      },
      "array": [
        2,
        5,
        1,
        9,
        6,
        3
      ],
      "focus": [
        null,
        null,
        null,
        "compare",
        "compare",
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 7,
      "op": "swap",
      "caption": "Swap 9 and 6.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 4,
        "swaps": 3
      },
      "array": [
        2,
        5,
        1,
        6,
        9,
        3
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        "swap",
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 8,
      "op": "compare",
      "caption": "Compare neighbours 9 and 3.",
      "note": "9 > 3 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 5,
        "swaps": 3
      },
      "array": [
        2,
        5,
        1,
        6,
        9,
        3
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "compare",
        "compare"
      ],
      "markers": [
        {
          "name": "j",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 9,
      "op": "swap",
      "caption": "Swap 9 and 3.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 5,
        "swaps": 4
      },
      "array": [
        2,
        5,
        1,
        6,
        3,
        9
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "swap",
        "swap"
      ],
      "markers": [
        {
          "name": "j",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": []
    },
    {
      "i": 10,
      "op": "settle",
      "caption": "Index 5 now holds its final value.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 5,
        "swaps": 4
      },
      "array": [
        2,
        5,
        1,
        6,
        3,
        9
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        "settled"
      ],
      "markers": [
        {
          "name": "j",
          "index": 4,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 11,
      "op": "compare",
      "caption": "Compare neighbours 2 and 5.",
      "note": "2 ≤ 5 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 6,
        "swaps": 4
      },
      "array": [
        2,
        5,
        1,
        6,
        3,
        9
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 12,
      "op": "compare",
      "caption": "Compare neighbours 5 and 1.",
      "note": "5 > 1 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 7,
        "swaps": 4
      },
      "array": [
        2,
        5,
        1,
        6,
        3,
        9
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 13,
      "op": "swap",
      "caption": "Swap 5 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 7,
        "swaps": 5
      },
      "array": [
        2,
        1,
        5,
        6,
        3,
        9
      ],
      "focus": [
        null,
        "swap",
        "swap",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 14,
      "op": "compare",
      "caption": "Compare neighbours 5 and 6.",
      "note": "5 ≤ 6 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 8,
        "swaps": 5
      },
      "array": [
        2,
        1,
        5,
        6,
        3,
        9
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 15,
      "op": "compare",
      "caption": "Compare neighbours 6 and 3.",
      "note": "6 > 3 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 9,
        "swaps": 5
      },
      "array": [
        2,
        1,
        5,
        6,
        3,
        9
      ],
      "focus": [
        null,
        null,
        null,
        "compare",
        "compare",
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 16,
      "op": "swap",
      "caption": "Swap 6 and 3.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 9,
        "swaps": 6
      },
      "array": [
        2,
        1,
        5,
        3,
        6,
        9
      ],
      "focus": [
        null,
        null,
        null,
        "swap",
        "swap",
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 5,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 17,
      "op": "settle",
      "caption": "Index 4 now holds its final value.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 9,
        "swaps": 6
      },
      "array": [
        2,
        1,
        5,
        3,
        6,
        9
      ],
      "focus": [
        null,
        null,
        null,
        null,
        "settled",
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 3,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 18,
      "op": "compare",
      "caption": "Compare neighbours 2 and 1.",
      "note": "2 > 1 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 10,
        "swaps": 6
      },
      "array": [
        2,
        1,
        5,
        3,
        6,
        9
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 19,
      "op": "swap",
      "caption": "Swap 2 and 1.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 10,
        "swaps": 7
      },
      "array": [
        1,
        2,
        5,
        3,
        6,
        9
      ],
      "focus": [
        "swap",
        "swap",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 20,
      "op": "compare",
      "caption": "Compare neighbours 2 and 5.",
      "note": "2 ≤ 5 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 11,
        "swaps": 7
      },
      "array": [
        1,
        2,
        5,
        3,
        6,
        9
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 21,
      "op": "compare",
      "caption": "Compare neighbours 5 and 3.",
      "note": "5 > 3 → swap",
      "codeLine": 3,
      "stats": {
        "compares": 12,
        "swaps": 7
      },
      "array": [
        1,
        2,
        5,
        3,
        6,
        9
      ],
      "focus": [
        null,
        null,
        "compare",
        "compare",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 22,
      "op": "swap",
      "caption": "Swap 5 and 3.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 12,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        null,
        null,
        "swap",
        "swap",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 4,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 23,
      "op": "settle",
      "caption": "Index 3 now holds its final value.",
      "note": null,
      "codeLine": 4,
      "stats": {
        "compares": 12,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        null,
        null,
        null,
        "settled",
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 2,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 3,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 24,
      "op": "compare",
      "caption": "Compare neighbours 1 and 2.",
      "note": "1 ≤ 2 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 13,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        "compare",
        "compare",
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 0,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 3,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 25,
      "op": "compare",
      "caption": "Compare neighbours 2 and 3.",
      "note": "2 ≤ 3 → leave",
      "codeLine": 3,
      "stats": {
        "compares": 14,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        null,
        "compare",
        "compare",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 3,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 26,
      "op": "settle",
      "caption": "Index 2 now holds its final value.",
      "note": null,
      "codeLine": 3,
      "stats": {
        "compares": 14,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        null,
        null,
        "settled",
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 2,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 27,
      "op": "say",
      "caption": "A full pass made no swaps — the array is already sorted.",
      "note": null,
      "codeLine": 3,
      "stats": {
        "compares": 14,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        null,
        null,
        null,
        null,
        null,
        null
      ],
      "markers": [
        {
          "name": "j",
          "index": 1,
          "tone": "compare"
        }
      ],
      "regions": [
        {
          "label": "sorted",
          "from": 2,
          "to": 5,
          "tone": "settled"
        }
      ]
    },
    {
      "i": 28,
      "op": "settle",
      "caption": "Sorted — every value is in its final place.",
      "note": null,
      "codeLine": 3,
      "stats": {
        "compares": 14,
        "swaps": 8
      },
      "array": [
        1,
        2,
        3,
        5,
        6,
        9
      ],
      "focus": [
        "settled",
        "settled",
        "settled",
        "settled",
        "settled",
        "settled"
      ],
      "markers": [],
      "regions": [
        {
          "label": "sorted",
          "from": 0,
          "to": 5,
          "tone": "settled"
        }
      ]
    }
  ]
}