{
  "id": "sorting/merge-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/merge-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Merge Sort",
  "tagline": "Split until it's trivial, then merge back in order.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n log n)",
      "average": "O(n log n)",
      "worst": "O(n log n)"
    },
    "space": "O(n)",
    "growth": "nlogn",
    "note": "Same speed on the best day and the worst day — nothing about the input can hurt it. The price is memory: it needs a second array to merge into, so it is not O(1) space like the simple sorts."
  },
  "code": {
    "javascript": "function mergeSort(a, lo = 0, hi = a.length - 1) {\n  if (lo >= hi) return a;\n  const mid = Math.floor((lo + hi) / 2);\n  mergeSort(a, lo, mid);\n  mergeSort(a, mid + 1, hi);\n  merge(a, lo, mid, hi);\n  return a;\n}\n\nfunction merge(a, lo, mid, hi) {\n  const left = a.slice(lo, mid + 1);\n  const right = a.slice(mid + 1, hi + 1);\n  let i = 0, j = 0, k = lo;\n  while (i < left.length && j < right.length) {\n    if (left[i] <= right[j]) a[k++] = left[i++];\n    else a[k++] = right[j++];\n  }\n  while (i < left.length) a[k++] = left[i++];\n  while (j < right.length) a[k++] = right[j++];\n}",
    "python": "def merge_sort(a, lo=0, hi=None):\n    if hi is None: hi = len(a) - 1\n    if lo >= hi: return a\n    mid = (lo + hi) // 2\n    merge_sort(a, lo, mid)\n    merge_sort(a, mid + 1, hi)\n    merge(a, lo, mid, hi)\n    return a\n\ndef merge(a, lo, mid, hi):\n    left = a[lo:mid + 1]\n    right = a[mid + 1:hi + 1]\n    i = j = 0\n    k = lo\n    while i < len(left) and j < len(right):\n        if left[i] <= right[j]:\n            a[k] = left[i]; i += 1\n        else:\n            a[k] = right[j]; j += 1\n        k += 1\n    while i < len(left):\n        a[k] = left[i]; i += 1; k += 1\n    while j < len(right):\n        a[k] = right[j]; j += 1; k += 1\n",
    "java": "void mergeSort(int[] a, int lo, int hi) {\n  if (lo >= hi) return;\n  int mid = (lo + hi) / 2;\n  mergeSort(a, lo, mid);\n  mergeSort(a, mid + 1, hi);\n  merge(a, lo, mid, hi);\n}\n\n\nvoid merge(int[] a, int lo, int mid, int hi) {\n  int[] left = Arrays.copyOfRange(a, lo, mid + 1);\n  int[] right = Arrays.copyOfRange(a, mid + 1, hi + 1);\n  int i = 0, j = 0, k = lo;\n  while (i < left.length && j < right.length) {\n    if (left[i] <= right[j]) a[k++] = left[i++];\n    else a[k++] = right[j++];\n  }\n  while (i < left.length) a[k++] = left[i++];\n  while (j < right.length) a[k++] = right[j++];\n}",
    "cpp": "void mergeSort(vector<int>& a, int lo, int hi) {\n  if (lo >= hi) return;\n  int mid = (lo + hi) / 2;\n  mergeSort(a, lo, mid);\n  mergeSort(a, mid + 1, hi);\n  merge(a, lo, mid, hi);\n}\n\n\nvoid merge(vector<int>& a, int lo, int mid, int hi) {\n  vector<int> left(a.begin() + lo, a.begin() + mid + 1);\n  vector<int> right(a.begin() + mid + 1, a.begin() + hi + 1);\n  int i = 0, j = 0, k = lo;\n  while (i < left.size() && j < right.size()) {\n    if (left[i] <= right[j]) a[k++] = left[i++];\n    else a[k++] = right[j++];\n  }\n  while (i < left.size()) a[k++] = left[i++];\n  while (j < right.size()) a[k++] = right[j++];\n}"
  },
  "defaultInput": [
    8,
    3,
    5,
    1,
    9,
    2,
    7
  ],
  "defaultTarget": null,
  "inputHint": "Watch the list break apart into single items, then rebuild in order.",
  "pitfalls": [
    {
      "wrong": "Forgetting the base case.",
      "right": "A piece of one item must return immediately. Without that, the splitting never stops and the call stack overflows."
    },
    {
      "wrong": "Losing the leftovers after the merge loop ends.",
      "right": "One half always runs out first. The rest of the other half is already sorted — you must still copy it over, or those values vanish."
    },
    {
      "wrong": "Using `<` instead of `<=` when comparing the two fronts.",
      "right": "With `<`, equal values swap their original order. That makes the sort unstable, which quietly breaks anything that was sorted by a second key first."
    },
    {
      "wrong": "Assuming n log n is 'a bit better' than n².",
      "right": "On a million items it is the difference between 20 million steps and 500 billion — seconds versus days. It is not a small win."
    }
  ],
  "quiz": [
    {
      "q": "Why is a single item 'already sorted'?",
      "options": [
        "It isn't — that's a shortcut",
        "There is nothing for it to be out of order with",
        "Because we sorted it earlier",
        "Only if the list was sorted to begin with"
      ],
      "answer": 1,
      "why": "Order is a relationship between two or more things. One item alone cannot be out of order — which is why the splitting can stop there."
    },
    {
      "q": "What is merge sort's worst-case time?",
      "options": [
        "O(n²)",
        "O(n log n)",
        "O(n)",
        "O(log n)"
      ],
      "answer": 1,
      "why": "O(n log n) — the same as its best case. Nothing about the input can make merge sort have a bad day, which is why it's used where predictability matters."
    },
    {
      "q": "What does merge sort pay that bubble sort doesn't?",
      "options": [
        "It needs extra memory to merge into",
        "It needs the list sorted first",
        "It's harder to read",
        "It's slower on big lists"
      ],
      "answer": 0,
      "why": "Merging needs somewhere to put the result — an extra array of size n. That O(n) space is the price of the speed."
    }
  ],
  "problems": [
    {
      "name": "Merge Sorted Array",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/merge-sorted-array/"
    },
    {
      "name": "Sort an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-an-array/"
    },
    {
      "name": "Count of Smaller Numbers After Self",
      "difficulty": "Hard",
      "url": "https://leetcode.com/problems/count-of-smaller-numbers-after-self/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 61,
    "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": "Merge sort splits the list in half, over and over, until every piece is a single item. A single item is already sorted — then it merges the pieces back together in order.",
        "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": "lo",
            "index": 0
          },
          {
            "name": "mid",
            "index": 3
          },
          {
            "name": "hi",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 4,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 0,
          "mid": 3,
          "hi": 6,
          "depth": 0
        },
        "explanation": "Split positions 0–6 down the middle: a left half (0–3) and a right half (4–6).",
        "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": "lo",
            "index": 0
          },
          {
            "name": "mid",
            "index": 1
          },
          {
            "name": "hi",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 0,
          "mid": 1,
          "hi": 3,
          "depth": 1
        },
        "explanation": "Split positions 0–3 down the middle: a left half (0–1) and a right half (2–3).",
        "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": "lo",
            "index": 0
          },
          {
            "name": "mid",
            "index": 0
          },
          {
            "name": "hi",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 0,
          "mid": 0,
          "hi": 1,
          "depth": 2
        },
        "explanation": "Split positions 0–1 down the middle: a left half (0–0) and a right half (1–1).",
        "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": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 0,
          "hi": 0,
          "depth": 3
        },
        "explanation": "A piece with one item (8) is already sorted. Nothing to do.",
        "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": [],
        "highlights": [
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 1,
          "hi": 1,
          "depth": 3
        },
        "explanation": "A piece with one item (3) is already sorted. Nothing to do.",
        "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": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 0,
          "mid": 0,
          "hi": 1,
          "depth": 2
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "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": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 1
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 8,
          "right": 3,
          "taking": 3
        },
        "explanation": "Compare the two fronts: 8 and 3. 3 is smaller, so it goes next.",
        "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": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 3,
          "mergedSoFar": 1
        },
        "explanation": "3 joins the merged run.",
        "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": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "placed": 8
        },
        "explanation": "The right half is empty, so the rest of the left half (8) is already in order. Copy it straight over.",
        "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": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "lo": 0,
          "hi": 1,
          "depth": 2,
          "comparisons": 1
        },
        "explanation": "Positions 0–1 are now one sorted run.",
        "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": "lo",
            "index": 2
          },
          {
            "name": "mid",
            "index": 2
          },
          {
            "name": "hi",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 2,
          "mid": 2,
          "hi": 3,
          "depth": 2
        },
        "explanation": "Split positions 2–3 down the middle: a left half (2–2) and a right half (3–3).",
        "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": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 2,
          "hi": 2,
          "depth": 3
        },
        "explanation": "A piece with one item (5) is already sorted. Nothing to do.",
        "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": [],
        "highlights": [
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 3,
          "hi": 3,
          "depth": 3
        },
        "explanation": "A piece with one item (1) is already sorted. Nothing to do.",
        "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": "left",
            "index": 2
          },
          {
            "name": "right",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 2,
          "mid": 2,
          "hi": 3,
          "depth": 2
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "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": "left",
            "index": 2
          },
          {
            "name": "right",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 5,
          "right": 1,
          "taking": 1
        },
        "explanation": "Compare the two fronts: 5 and 1. 1 is smaller, so it goes next.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 1,
          "mergedSoFar": 1
        },
        "explanation": "1 joins the merged run.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "placed": 5
        },
        "explanation": "The right half is empty, so the rest of the left half (5) is already in order. Copy it straight over.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "lo": 2,
          "hi": 3,
          "depth": 2,
          "comparisons": 2
        },
        "explanation": "Positions 2–3 are now one sorted run.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 0,
          "mid": 1,
          "hi": 3,
          "depth": 1
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 2
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 3,
          "right": 1,
          "taking": 1
        },
        "explanation": "Compare the two fronts: 3 and 1. 1 is smaller, so it goes next.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 1,
          "mergedSoFar": 1
        },
        "explanation": "1 joins the merged run.",
        "stats": {
          "comparisons": 3,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 1
          },
          {
            "name": "right",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 3,
          "right": 5,
          "taking": 3
        },
        "explanation": "Compare the two fronts: 3 and 5. 3 is smaller, so it goes next.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 3,
          "mergedSoFar": 2
        },
        "explanation": "3 joins the merged run.",
        "stats": {
          "comparisons": 4,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 2
          },
          {
            "name": "right",
            "index": 3
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 8,
          "right": 5,
          "taking": 5
        },
        "explanation": "Compare the two fronts: 8 and 5. 5 is smaller, so it goes next.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 5,
          "mergedSoFar": 3
        },
        "explanation": "5 joins the merged run.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "placed": 8
        },
        "explanation": "The right half is empty, so the rest of the left half (8) is already in order. Copy it straight over.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "lo": 0,
          "hi": 3,
          "depth": 1,
          "comparisons": 5
        },
        "explanation": "Positions 0–3 are now one sorted run.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "lo",
            "index": 4
          },
          {
            "name": "mid",
            "index": 5
          },
          {
            "name": "hi",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 4,
          "mid": 5,
          "hi": 6,
          "depth": 1
        },
        "explanation": "Split positions 4–6 down the middle: a left half (4–5) and a right half (6–6).",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "lo",
            "index": 4
          },
          {
            "name": "mid",
            "index": 4
          },
          {
            "name": "hi",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "pivot"
          }
        ],
        "codeLine": 3,
        "variables": {
          "lo": 4,
          "mid": 4,
          "hi": 5,
          "depth": 2
        },
        "explanation": "Split positions 4–5 down the middle: a left half (4–4) and a right half (5–5).",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 4,
          "hi": 4,
          "depth": 3
        },
        "explanation": "A piece with one item (9) is already sorted. Nothing to do.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 5,
          "hi": 5,
          "depth": 3
        },
        "explanation": "A piece with one item (2) is already sorted. Nothing to do.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 4
          },
          {
            "name": "right",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 4,
          "mid": 4,
          "hi": 5,
          "depth": 2
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "stats": {
          "comparisons": 5,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 4
          },
          {
            "name": "right",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 9,
          "right": 2,
          "taking": 2
        },
        "explanation": "Compare the two fronts: 9 and 2. 2 is smaller, so it goes next.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 2,
          "mergedSoFar": 1
        },
        "explanation": "2 joins the merged run.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "placed": 9
        },
        "explanation": "The right half is empty, so the rest of the left half (9) is already in order. Copy it straight over.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "lo": 4,
          "hi": 5,
          "depth": 2,
          "comparisons": 6
        },
        "explanation": "Positions 4–5 are now one sorted run.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 2,
        "variables": {
          "lo": 6,
          "hi": 6,
          "depth": 2
        },
        "explanation": "A piece with one item (7) is already sorted. Nothing to do.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 4
          },
          {
            "name": "right",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "active"
          },
          {
            "index": 5,
            "role": "active"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 4,
          "mid": 5,
          "hi": 6,
          "depth": 1
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "stats": {
          "comparisons": 6,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 4
          },
          {
            "name": "right",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 2,
          "right": 7,
          "taking": 2
        },
        "explanation": "Compare the two fronts: 2 and 7. 2 is smaller, so it goes next.",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "swap"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 2,
          "mergedSoFar": 1
        },
        "explanation": "2 joins the merged run.",
        "stats": {
          "comparisons": 7,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c4-9",
            "value": 9
          },
          {
            "id": "c6-7",
            "value": 7
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 5
          },
          {
            "name": "right",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 9,
          "right": 7,
          "taking": 7
        },
        "explanation": "Compare the two fronts: 9 and 7. 7 is smaller, so it goes next.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": [],
        "highlights": [
          {
            "index": 5,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 7,
          "mergedSoFar": 2
        },
        "explanation": "7 joins the merged run.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 18,
        "variables": {
          "placed": 9
        },
        "explanation": "The right half is empty, so the rest of the left half (9) is already in order. Copy it straight over.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 6,
        "variables": {
          "lo": 4,
          "hi": 6,
          "depth": 1,
          "comparisons": 8
        },
        "explanation": "Positions 4–6 are now one sorted run.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "active"
          },
          {
            "index": 1,
            "role": "active"
          },
          {
            "index": 2,
            "role": "active"
          },
          {
            "index": 3,
            "role": "active"
          },
          {
            "index": 4,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "pivot"
          }
        ],
        "codeLine": 10,
        "variables": {
          "lo": 0,
          "mid": 3,
          "hi": 6,
          "depth": 0
        },
        "explanation": "Both halves are sorted now. Merge them by repeatedly taking whichever front value is smaller.",
        "stats": {
          "comparisons": 8,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": "left",
            "index": 0
          },
          {
            "name": "right",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 1,
          "right": 2,
          "taking": 1
        },
        "explanation": "Compare the two fronts: 1 and 2. 1 is smaller, so it goes next.",
        "stats": {
          "comparisons": 9,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 1,
          "mergedSoFar": 1
        },
        "explanation": "1 joins the merged run.",
        "stats": {
          "comparisons": 9,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "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": "left",
            "index": 1
          },
          {
            "name": "right",
            "index": 4
          }
        ],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 3,
          "right": 2,
          "taking": 2
        },
        "explanation": "Compare the two fronts: 3 and 2. 2 is smaller, so it goes next.",
        "stats": {
          "comparisons": 10,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 2,
          "mergedSoFar": 2
        },
        "explanation": "2 joins the merged run.",
        "stats": {
          "comparisons": 10,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 2
          },
          {
            "name": "right",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 3,
          "right": 7,
          "taking": 3
        },
        "explanation": "Compare the two fronts: 3 and 7. 3 is smaller, so it goes next.",
        "stats": {
          "comparisons": 11,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 3,
          "mergedSoFar": 3
        },
        "explanation": "3 joins the merged run.",
        "stats": {
          "comparisons": 11,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 3
          },
          {
            "name": "right",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 5,
          "right": 7,
          "taking": 5
        },
        "explanation": "Compare the two fronts: 5 and 7. 5 is smaller, so it goes next.",
        "stats": {
          "comparisons": 12,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 5,
          "mergedSoFar": 4
        },
        "explanation": "5 joins the merged run.",
        "stats": {
          "comparisons": 12,
          "swaps": 0
        }
      },
      {
        "data": [
          {
            "id": "c3-1",
            "value": 1
          },
          {
            "id": "c5-2",
            "value": 2
          },
          {
            "id": "c1-3",
            "value": 3
          },
          {
            "id": "c2-5",
            "value": 5
          },
          {
            "id": "c0-8",
            "value": 8
          },
          {
            "id": "c6-7",
            "value": 7
          },
          {
            "id": "c4-9",
            "value": 9
          }
        ],
        "pointers": [
          {
            "name": "left",
            "index": 4
          },
          {
            "name": "right",
            "index": 5
          }
        ],
        "highlights": [
          {
            "index": 4,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 8,
          "right": 7,
          "taking": 7
        },
        "explanation": "Compare the two fronts: 8 and 7. 7 is smaller, so it goes next.",
        "stats": {
          "comparisons": 13,
          "swaps": 0
        }
      },
      {
        "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": 4,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          }
        ],
        "codeLine": 16,
        "variables": {
          "placed": 7,
          "mergedSoFar": 5
        },
        "explanation": "7 joins the merged run.",
        "stats": {
          "comparisons": 13,
          "swaps": 0
        }
      },
      {
        "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": "left",
            "index": 5
          },
          {
            "name": "right",
            "index": 6
          }
        ],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "compare"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "left": 8,
          "right": 9,
          "taking": 8
        },
        "explanation": "Compare the two fronts: 8 and 9. 8 is smaller, so it goes next.",
        "stats": {
          "comparisons": 14,
          "swaps": 0
        }
      },
      {
        "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": 5,
            "role": "swap"
          },
          {
            "index": 0,
            "role": "sorted"
          },
          {
            "index": 1,
            "role": "sorted"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          }
        ],
        "codeLine": 15,
        "variables": {
          "placed": 8,
          "mergedSoFar": 6
        },
        "explanation": "8 joins the merged run.",
        "stats": {
          "comparisons": 14,
          "swaps": 0
        }
      },
      {
        "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": 19,
        "variables": {
          "placed": 9
        },
        "explanation": "The left half is empty, so the rest of the right half (9) is already in order. Copy it straight over.",
        "stats": {
          "comparisons": 14,
          "swaps": 0
        }
      },
      {
        "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": 6,
        "variables": {
          "lo": 0,
          "hi": 6,
          "depth": 0,
          "comparisons": 14
        },
        "explanation": "Positions 0–6 are now one sorted run.",
        "stats": {
          "comparisons": 14,
          "swaps": 0
        }
      },
      {
        "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": 7,
        "variables": {
          "comparisons": 14
        },
        "explanation": "Done in 14 comparisons. Halving the problem is what keeps merge sort at n log n even in the worst case.",
        "stats": {
          "comparisons": 14,
          "swaps": 0
        }
      }
    ]
  }
}