{
  "slug": "merge-sort",
  "title": "Merge Sort",
  "category": "Sorting",
  "difficulty": "medium",
  "complexity": {
    "time": "O(n log n)",
    "space": "O(n)"
  },
  "idea": "Recursively split the array in half until each piece is one element, then merge sorted pieces back together. Each merge fills a separate buffer one value at a time — comparing the fronts of the two halves — then copies the buffer back, so no value is ever lost or duplicated.",
  "code": [
    "sort(lo, hi): if lo>=hi return; mid=(lo+hi)/2",
    "    sort(lo,mid); sort(mid+1,hi); merge(lo,mid,hi)",
    "merge: compare the fronts of the two halves",
    "    write the smaller into buffer[k++]",
    "    copy the buffer back into the array"
  ],
  "example": {
    "array": [
      6,
      2,
      8,
      4,
      3,
      7,
      1,
      5
    ]
  },
  "invariant": "Each step is the real data structure at that step — the picture is a pure function of it.",
  "stepCount": 40,
  "steps": [
    {
      "i": 0,
      "op": "lanes",
      "caption": "Merge sort: split down to single elements, then merge through a buffer.",
      "note": null,
      "codeLine": 1,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            6,
            2,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": []
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 1,
      "op": "lanes",
      "caption": "Merge sorted halves [0..0] and [1..1].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            6,
            2,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 1,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 2,
      "op": "lanes",
      "caption": "6 vs 2 → take 2 into buffer[0].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            6,
            2,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            "read",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 0,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 1,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 1,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            "insert",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 0,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 3,
      "op": "lanes",
      "caption": "Copy remaining 6 → buffer[1].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            6,
            2,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            "read",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 0,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 1,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 1,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            6,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            "insert",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 1,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 4,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [0..1].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            "write",
            "write",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 1,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            6,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 5,
      "op": "lanes",
      "caption": "Merge sorted halves [2..2] and [3..3].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 2,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 6,
      "op": "lanes",
      "caption": "8 vs 4 → take 4 into buffer[2].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            "read",
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 2,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 3,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 2,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            4,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            "insert",
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            false,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 2,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 7,
      "op": "lanes",
      "caption": "Copy remaining 8 → buffer[3].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            8,
            4,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            "read",
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 2,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 3,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 2,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            4,
            8,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            "insert",
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            false,
            false,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 3,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 8,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [2..3].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            "write",
            "write",
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 2,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            4,
            8,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            false,
            false,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 9,
      "op": "lanes",
      "caption": "Merge sorted halves [0..1] and [2..3].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 10,
      "op": "lanes",
      "caption": "2 vs 4 → take 2 into buffer[0].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            "read",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 0,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 2,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            "insert",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 0,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 11,
      "op": "lanes",
      "caption": "6 vs 4 → take 4 into buffer[1].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            "read",
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 1,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 2,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            4,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            "insert",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 1,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 12,
      "op": "lanes",
      "caption": "6 vs 8 → take 6 into buffer[2].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            "read",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 1,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 3,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            4,
            6,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            "insert",
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 2,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 13,
      "op": "lanes",
      "caption": "Copy remaining 8 → buffer[3].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            6,
            4,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            "read",
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 1,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 3,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            4,
            6,
            8,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            "insert",
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 3,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 14,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [0..3].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            "write",
            "write",
            "write",
            "write",
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 3,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            2,
            4,
            6,
            8,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 15,
      "op": "lanes",
      "caption": "Merge sorted halves [4..4] and [5..5].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 5,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 16,
      "op": "lanes",
      "caption": "3 vs 7 → take 3 into buffer[4].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "read",
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 4,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 5,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 5,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            3,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "insert",
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 4,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 17,
      "op": "lanes",
      "caption": "Copy remaining 7 → buffer[5].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "read",
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 4,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 5,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 5,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            3,
            7,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "insert",
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 5,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 18,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [4..5].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "write",
            "write",
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 5,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            3,
            7,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 19,
      "op": "lanes",
      "caption": "Merge sorted halves [6..6] and [7..7].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 6,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 20,
      "op": "lanes",
      "caption": "1 vs 5 → take 1 into buffer[6].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "read",
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 6,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 6,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            1,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "insert",
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            false,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 6,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 21,
      "op": "lanes",
      "caption": "Copy remaining 5 → buffer[7].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "read"
          ],
          "markers": [
            {
              "name": "i",
              "index": 6,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 6,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "insert"
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            false,
            false
          ],
          "markers": [
            {
              "name": "k",
              "index": 7,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 22,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [6..7].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "write",
            "write"
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 6,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            false,
            false
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 23,
      "op": "lanes",
      "caption": "Merge sorted halves [4..5] and [6..7].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 24,
      "op": "lanes",
      "caption": "3 vs 1 → take 1 into buffer[4].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "read",
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 4,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 6,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            1,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "insert",
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 4,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 25,
      "op": "lanes",
      "caption": "3 vs 5 → take 3 into buffer[5].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "read",
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 4,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            1,
            3,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "insert",
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 5,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 26,
      "op": "lanes",
      "caption": "7 vs 5 → take 5 into buffer[6].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "read"
          ],
          "markers": [
            {
              "name": "i",
              "index": 5,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            1,
            3,
            5,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "insert",
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            false,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 6,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 27,
      "op": "lanes",
      "caption": "Copy remaining 7 → buffer[7].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            3,
            7,
            1,
            5
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "read",
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 5,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "insert"
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            false,
            false
          ],
          "markers": [
            {
              "name": "k",
              "index": 7,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 28,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [4..7].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "write",
            "write",
            "write",
            "write"
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 4,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            false,
            false,
            false,
            false
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 29,
      "op": "lanes",
      "caption": "Merge sorted halves [0..3] and [4..7].",
      "note": null,
      "codeLine": 3,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 30,
      "op": "lanes",
      "caption": "2 vs 1 → take 1 into buffer[0].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "read",
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 0,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 4,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            "insert",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 0,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 31,
      "op": "lanes",
      "caption": "2 vs 3 → take 2 into buffer[1].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            "read",
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 0,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 5,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            "insert",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 1,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 32,
      "op": "lanes",
      "caption": "4 vs 3 → take 3 into buffer[2].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "read",
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 1,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 5,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            "insert",
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 2,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 33,
      "op": "lanes",
      "caption": "4 vs 5 → take 4 into buffer[3].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            "read",
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 1,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 6,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            "insert",
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            true,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 3,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 34,
      "op": "lanes",
      "caption": "6 vs 5 → take 5 into buffer[4].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "read",
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 2,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 6,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            5,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            "insert",
            null,
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            false,
            true,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 4,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 35,
      "op": "lanes",
      "caption": "6 vs 7 → take 6 into buffer[5].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            "read",
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 2,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            "insert",
            null,
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            false,
            false,
            true,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 5,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 36,
      "op": "lanes",
      "caption": "8 vs 7 → take 7 into buffer[6].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "read"
          ],
          "markers": [
            {
              "name": "i",
              "index": 3,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            "insert",
            null
          ],
          "placeholders": [
            false,
            false,
            false,
            false,
            false,
            false,
            false,
            true
          ],
          "markers": [
            {
              "name": "k",
              "index": 6,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 37,
      "op": "lanes",
      "caption": "Copy remaining 8 → buffer[7].",
      "note": null,
      "codeLine": 4,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            2,
            4,
            6,
            8,
            1,
            3,
            5,
            7
          ],
          "focus": [
            null,
            null,
            null,
            "read",
            null,
            null,
            null,
            null
          ],
          "markers": [
            {
              "name": "i",
              "index": 3,
              "tone": "compare"
            },
            {
              "name": "j",
              "index": 7,
              "tone": "compare"
            }
          ],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            "insert"
          ],
          "markers": [
            {
              "name": "k",
              "index": 7,
              "tone": "pivot"
            }
          ],
          "regions": []
        }
      ]
    },
    {
      "i": 38,
      "op": "lanes",
      "caption": "Copy the merged run back into the array [0..7].",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
          ],
          "focus": [
            "write",
            "write",
            "write",
            "write",
            "write",
            "write",
            "write",
            "write"
          ],
          "markers": [],
          "regions": [
            {
              "label": "range",
              "from": 0,
              "to": 7,
              "tone": "window"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "markers": [],
          "regions": []
        }
      ]
    },
    {
      "i": 39,
      "op": "lanes",
      "caption": "Sorted — every half merged into order.",
      "note": null,
      "codeLine": 5,
      "stats": null,
      "lanes": [
        {
          "id": "arr",
          "label": "array",
          "array": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
          ],
          "focus": [
            "settled",
            "settled",
            "settled",
            "settled",
            "settled",
            "settled",
            "settled",
            "settled"
          ],
          "markers": [],
          "regions": [
            {
              "label": "done",
              "from": 0,
              "to": 7,
              "tone": "settled"
            }
          ]
        },
        {
          "id": "buf",
          "label": "merge buffer",
          "array": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
          ],
          "focus": [
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null
          ],
          "placeholders": [
            true,
            true,
            true,
            true,
            true,
            true,
            true,
            true
          ],
          "markers": [],
          "regions": []
        }
      ]
    }
  ]
}