{
  "id": "sorting/bubble-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/bubble-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Bubble Sort",
  "tagline": "Compare neighbours, swap if they're wrong. Repeat until calm.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n²)",
      "worst": "O(n²)"
    },
    "space": "O(1)",
    "growth": "quadratic",
    "note": "Roughly n²/2 comparisons in the worst case: 10 items cost about 45 comparisons, 100 items cost about 5,000. The best case is O(n) only because of the early-exit check — one clean pass with no swaps proves the list is already sorted."
  },
  "code": {
    "javascript": "function bubbleSort(a) {\n  for (let i = 0; i < a.length - 1; i++) {\n    let swapped = false;\n    for (let j = 0; j < a.length - 1 - i; j++) {\n      if (a[j] > a[j + 1]) {\n        [a[j], a[j + 1]] = [a[j + 1], a[j]];\n        swapped = true;\n      }\n    }\n    if (!swapped) break;\n  }\n  return a;\n}",
    "python": "def bubble_sort(a):\n    for i in range(len(a) - 1):\n        swapped = False\n        for j in range(len(a) - 1 - i):\n            if a[j] > a[j + 1]:\n                a[j], a[j + 1] = a[j + 1], a[j]\n                swapped = True\n\n        # nothing moved this pass?\n        if not swapped:\n            break\n    return a\n",
    "java": "int[] bubbleSort(int[] a) {\n  for (int i = 0; i < a.length - 1; i++) {\n    boolean swapped = false;\n    for (int j = 0; j < a.length - 1 - i; j++) {\n      if (a[j] > a[j + 1]) {\n        int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;\n        swapped = true;\n      }\n    }\n    if (!swapped) break;\n  }\n  return a;\n}",
    "cpp": "vector<int> bubbleSort(vector<int> a) {\n  for (int i = 0; i + 1 < a.size(); i++) {\n    bool swapped = false;\n    for (int j = 0; j + 1 < a.size() - i; j++) {\n      if (a[j] > a[j + 1]) {\n        swap(a[j], a[j + 1]);\n        swapped = true;\n      }\n    }\n    if (!swapped) break;\n  }\n  return a;\n}"
  },
  "defaultInput": [
    8,
    3,
    5,
    1,
    9,
    2,
    7
  ],
  "defaultTarget": null,
  "inputHint": "Type numbers separated by spaces. Try one that's already sorted.",
  "pitfalls": [
    {
      "wrong": "Leaving out the 'did anything swap?' check.",
      "right": "Without it, an already-sorted list still costs the full n² passes. That one boolean turns the best case from O(n²) into O(n)."
    },
    {
      "wrong": "Looping j all the way to the end on every pass.",
      "right": "After pass i, the last i items are already final. Use `j < n - 1 - i` and stop re-checking values you've proven are done."
    },
    {
      "wrong": "Reaching for bubble sort in real code.",
      "right": "Don't. Every language ships a sort that is dramatically faster. Bubble sort is for understanding what sorting *is*, not for doing it."
    }
  ],
  "quiz": [
    {
      "q": "After one full pass of bubble sort, what do you know for certain?",
      "options": [
        "The list is sorted",
        "The smallest value is at the front",
        "The largest value is at the end",
        "Nothing yet"
      ],
      "answer": 2,
      "why": "Every comparison pushes the bigger value right, so the largest value gets carried all the way to the end. It never has to move again."
    },
    {
      "q": "You run bubble sort on a list that is already sorted. With the early-exit check, what happens?",
      "options": [
        "It still does n² comparisons",
        "One pass, no swaps, and it stops",
        "It crashes",
        "It reverses the list"
      ],
      "answer": 1,
      "why": "One pass finds nothing to swap, which proves the whole list is in order. That is the best case: O(n)."
    },
    {
      "q": "Roughly how many comparisons does bubble sort need for 100 items in the worst case?",
      "options": [
        "100",
        "About 700",
        "About 5,000",
        "About 1,000,000"
      ],
      "answer": 2,
      "why": "About n²/2 = 100 × 99 / 2 ≈ 5,000. Ten times the data costs a hundred times the work — the signature of O(n²)."
    }
  ],
  "problems": [
    {
      "name": "Sort Colors",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-colors/"
    },
    {
      "name": "Height Checker",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/height-checker/"
    },
    {
      "name": "Sort an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-an-array/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 38,
    "frames": [
      {
        "data": [
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {},
        "explanation": "Bubble sort compares two neighbours at a time. The biggest value keeps floating to the end, like a bubble rising to the surface.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 0,
          "left": 8,
          "right": 3
        },
        "explanation": "Is 8 bigger than 3? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 1,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "j": 0,
          "swaps": 1
        },
        "explanation": "Swap them. 3 moves left, 8 moves right.",
        "stats": {
          "comparisons": 1,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 1,
          "left": 8,
          "right": 5
        },
        "explanation": "Is 8 bigger than 5? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "j": 1,
          "swaps": 2
        },
        "explanation": "Swap them. 5 moves left, 8 moves right.",
        "stats": {
          "comparisons": 2,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 2,
          "left": 8,
          "right": 1
        },
        "explanation": "Is 8 bigger than 1? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 3,
          "swaps": 2
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "j": 2,
          "swaps": 3
        },
        "explanation": "Swap them. 1 moves left, 8 moves right.",
        "stats": {
          "comparisons": 3,
          "swaps": 3
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 3
          },
          {
            "name": "j+1",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 3,
          "left": 8,
          "right": 9
        },
        "explanation": "Is 8 bigger than 9? No — leave them alone.",
        "stats": {
          "comparisons": 4,
          "swaps": 3
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          },
          {
            "name": "j+1",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 4,
          "left": 9,
          "right": 2
        },
        "explanation": "Is 9 bigger than 2? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 5,
          "swaps": 3
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          },
          {
            "name": "j+1",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "j": 4,
          "swaps": 4
        },
        "explanation": "Swap them. 2 moves left, 9 moves right.",
        "stats": {
          "comparisons": 5,
          "swaps": 4
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 5
          },
          {
            "name": "j+1",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 0,
          "j": 5,
          "left": 9,
          "right": 7
        },
        "explanation": "Is 9 bigger than 7? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 6,
          "swaps": 4
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 5
          },
          {
            "name": "j+1",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "swap"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 0,
          "j": 5,
          "swaps": 5
        },
        "explanation": "Swap them. 7 moves left, 9 moves right.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "i": 0,
          "swappedThisPass": true
        },
        "explanation": "End of pass 1. 9 is now in its final place — nothing to its right can beat it.",
        "stats": {
          "comparisons": 6,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 0,
          "left": 3,
          "right": 5
        },
        "explanation": "Is 3 bigger than 5? No — leave them alone.",
        "stats": {
          "comparisons": 7,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 1,
          "left": 5,
          "right": 1
        },
        "explanation": "Is 5 bigger than 1? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 8,
          "swaps": 5
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 1,
          "j": 1,
          "swaps": 6
        },
        "explanation": "Swap them. 1 moves left, 5 moves right.",
        "stats": {
          "comparisons": 8,
          "swaps": 6
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 2,
          "left": 5,
          "right": 8
        },
        "explanation": "Is 5 bigger than 8? No — leave them alone.",
        "stats": {
          "comparisons": 9,
          "swaps": 6
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 3
          },
          {
            "name": "j+1",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 3,
          "left": 8,
          "right": 2
        },
        "explanation": "Is 8 bigger than 2? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 10,
          "swaps": 6
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 3
          },
          {
            "name": "j+1",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 1,
          "j": 3,
          "swaps": 7
        },
        "explanation": "Swap them. 2 moves left, 8 moves right.",
        "stats": {
          "comparisons": 10,
          "swaps": 7
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          },
          {
            "name": "j+1",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 1,
          "j": 4,
          "left": 8,
          "right": 7
        },
        "explanation": "Is 8 bigger than 7? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 11,
          "swaps": 7
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 4
          },
          {
            "name": "j+1",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 1,
          "j": 4,
          "swaps": 8
        },
        "explanation": "Swap them. 7 moves left, 8 moves right.",
        "stats": {
          "comparisons": 11,
          "swaps": 8
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "i": 1,
          "swappedThisPass": true
        },
        "explanation": "End of pass 2. 8 is now in its final place — nothing to its right can beat it.",
        "stats": {
          "comparisons": 11,
          "swaps": 8
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 2,
          "j": 0,
          "left": 3,
          "right": 1
        },
        "explanation": "Is 3 bigger than 1? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 12,
          "swaps": 8
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 2,
          "j": 0,
          "swaps": 9
        },
        "explanation": "Swap them. 1 moves left, 3 moves right.",
        "stats": {
          "comparisons": 12,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 2,
          "j": 1,
          "left": 3,
          "right": 5
        },
        "explanation": "Is 3 bigger than 5? No — leave them alone.",
        "stats": {
          "comparisons": 13,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 2,
          "j": 2,
          "left": 5,
          "right": 2
        },
        "explanation": "Is 5 bigger than 2? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 14,
          "swaps": 9
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 2,
          "j": 2,
          "swaps": 10
        },
        "explanation": "Swap them. 2 moves left, 5 moves right.",
        "stats": {
          "comparisons": 14,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 3
          },
          {
            "name": "j+1",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 2,
          "j": 3,
          "left": 5,
          "right": 7
        },
        "explanation": "Is 5 bigger than 7? No — leave them alone.",
        "stats": {
          "comparisons": 15,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "i": 2,
          "swappedThisPass": true
        },
        "explanation": "End of pass 3. 7 is now in its final place — nothing to its right can beat it.",
        "stats": {
          "comparisons": 15,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 0,
          "left": 1,
          "right": 3
        },
        "explanation": "Is 1 bigger than 3? No — leave them alone.",
        "stats": {
          "comparisons": 16,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 1,
          "left": 3,
          "right": 2
        },
        "explanation": "Is 3 bigger than 2? Yes — they are in the wrong order.",
        "stats": {
          "comparisons": 17,
          "swaps": 10
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "i": 3,
          "j": 1,
          "swaps": 11
        },
        "explanation": "Swap them. 2 moves left, 3 moves right.",
        "stats": {
          "comparisons": 17,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 2
          },
          {
            "name": "j+1",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 3,
          "j": 2,
          "left": 3,
          "right": 5
        },
        "explanation": "Is 3 bigger than 5? No — leave them alone.",
        "stats": {
          "comparisons": 18,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "i": 3,
          "swappedThisPass": true
        },
        "explanation": "End of pass 4. 5 is now in its final place — nothing to its right can beat it.",
        "stats": {
          "comparisons": 18,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 0
          },
          {
            "name": "j+1",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 4,
          "j": 0,
          "left": 1,
          "right": 2
        },
        "explanation": "Is 1 bigger than 2? No — leave them alone.",
        "stats": {
          "comparisons": 19,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "j",
            "index": 1
          },
          {
            "name": "j+1",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 5,
        "variables": {
          "i": 4,
          "j": 1,
          "left": 2,
          "right": 3
        },
        "explanation": "Is 2 bigger than 3? No — leave them alone.",
        "stats": {
          "comparisons": 20,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 10,
        "variables": {
          "i": 4,
          "swappedThisPass": false
        },
        "explanation": "A whole pass with no swaps at all. That can only mean the list is already sorted, so we stop early.",
        "stats": {
          "comparisons": 20,
          "swaps": 11
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 12,
        "variables": {
          "comparisons": 20,
          "swaps": 11
        },
        "explanation": "Done. Sorted in 20 comparisons and 11 swaps.",
        "stats": {
          "comparisons": 20,
          "swaps": 11
        }
      }
    ]
  }
}