{
  "id": "sorting/heap-sort",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/sorting/heap-sort",
  "topic": {
    "slug": "sorting",
    "title": "Sorting"
  },
  "title": "Heap Sort",
  "tagline": "Turn the array into a heap, then keep taking the biggest off the top.",
  "vizKind": "bars",
  "complexity": {
    "time": {
      "best": "O(n log n)",
      "average": "O(n log n)",
      "worst": "O(n log n)"
    },
    "space": "O(1)",
    "growth": "nlogn",
    "note": "n log n no matter what the input looks like — no bad days, unlike quick sort. And it sorts in place, needing no second array, unlike merge sort. Its only real weakness is cache behaviour: it hops around memory instead of walking it."
  },
  "code": {
    "javascript": "function heapSort(a) {\n  const n = a.length;\n  for (let i = (n >> 1) - 1; i >= 0; i--) {\n    siftDown(a, i, n);\n  }\n  for (let end = n - 1; end > 0; end--) {\n    [a[0], a[end]] = [a[end], a[0]];\n    siftDown(a, 0, end);\n  }\n  return a;\n}\n\nfunction siftDown(a, i, n) {\n  for (;;) {\n    let big = i;\n    const l = 2 * i + 1, r = 2 * i + 2;\n    if (l < n && a[l] > a[big]) big = l;\n    if (r < n && a[r] > a[big]) big = r;\n    if (big === i) return;\n    [a[i], a[big]] = [a[big], a[i]];\n    i = big;\n  }\n}",
    "python": "def heap_sort(a):\n    n = len(a)\n    for i in range(n // 2 - 1, -1, -1):\n        sift_down(a, i, n)\n\n    for end in range(n - 1, 0, -1):\n        a[0], a[end] = a[end], a[0]\n        sift_down(a, 0, end)\n\n    return a\n\n\ndef sift_down(a, i, n):\n    while True:\n        big = i\n        l, r = 2 * i + 1, 2 * i + 2\n        if l < n and a[l] > a[big]: big = l\n        if r < n and a[r] > a[big]: big = r\n        if big == i: return\n        a[i], a[big] = a[big], a[i]\n        i = big\n",
    "java": "int[] heapSort(int[] a) {\n  int n = a.length;\n  for (int i = n / 2 - 1; i >= 0; i--) {\n    siftDown(a, i, n);\n  }\n  for (int end = n - 1; end > 0; end--) {\n    int t = a[0]; a[0] = a[end]; a[end] = t;\n    siftDown(a, 0, end);\n  }\n  return a;\n}\n\nvoid siftDown(int[] a, int i, int n) {\n  while (true) {\n    int big = i;\n    int l = 2 * i + 1, r = 2 * i + 2;\n    if (l < n && a[l] > a[big]) big = l;\n    if (r < n && a[r] > a[big]) big = r;\n    if (big == i) return;\n    int t = a[i]; a[i] = a[big]; a[big] = t;\n    i = big;\n  }\n}",
    "cpp": "vector<int> heapSort(vector<int> a) {\n  int n = a.size();\n  for (int i = n / 2 - 1; i >= 0; i--) {\n    siftDown(a, i, n);\n  }\n  for (int end = n - 1; end > 0; end--) {\n    swap(a[0], a[end]);\n    siftDown(a, 0, end);\n  }\n  return a;\n}\n\nvoid siftDown(vector<int>& a, int i, int n) {\n  for (;;) {\n    int big = i;\n    int l = 2 * i + 1, r = 2 * i + 2;\n    if (l < n && a[l] > a[big]) big = l;\n    if (r < n && a[r] > a[big]) big = r;\n    if (big == i) return;\n    swap(a[i], a[big]);\n    i = big;\n  }\n}"
  },
  "defaultInput": [
    4,
    10,
    3,
    5,
    1,
    8,
    6
  ],
  "defaultTarget": null,
  "inputHint": "Act one builds the heap. Act two drains it, biggest first.",
  "pitfalls": [
    {
      "wrong": "Sifting up when you should sift down.",
      "right": "Building the heap and re-settling the root both sink a value *down*. Sifting up is for inserting a brand-new value at the bottom."
    },
    {
      "wrong": "Forgetting to shrink the heap after parking the maximum.",
      "right": "Once the max is swapped to the end, that slot is finished. Sift down over the smaller range, or you'll drag the sorted values back into the heap."
    },
    {
      "wrong": "Starting heapify from index 0.",
      "right": "Start from the last parent, n/2 − 1, and work backwards. Leaves are already valid heaps of one — sifting them is pure waste."
    }
  ],
  "quiz": [
    {
      "q": "After building the max-heap, where is the largest value?",
      "options": [
        "At the end",
        "At the front (the root)",
        "In the middle",
        "It varies"
      ],
      "answer": 1,
      "why": "The heap rule makes every parent bigger than its children, so the maximum bubbles all the way to the root. That's exactly what makes it easy to grab."
    },
    {
      "q": "What is heap sort's worst-case time?",
      "options": [
        "O(n²)",
        "O(n log n)",
        "O(n)",
        "O(log n)"
      ],
      "answer": 1,
      "why": "O(n log n), guaranteed — nothing about the input can hurt it. That's its edge over quick sort, whose worst case collapses to O(n²)."
    },
    {
      "q": "What does heap sort have that merge sort doesn't?",
      "options": [
        "A better worst case",
        "It sorts in place, with no extra array",
        "It's stable",
        "It's faster in practice"
      ],
      "answer": 1,
      "why": "Both are O(n log n) worst-case, but merge sort needs an extra array of size n. Heap sort does everything with swaps inside the original array: O(1) extra space."
    }
  ],
  "problems": [
    {
      "name": "Kth Largest Element in an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/kth-largest-element-in-an-array/"
    },
    {
      "name": "Sort an Array",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/sort-an-array/"
    },
    {
      "name": "Top K Frequent Elements",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/top-k-frequent-elements/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 34,
    "frames": [
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {},
        "explanation": "Heap sort has two acts. First it rearranges the array into a max-heap, so the biggest value is at the front. Then it repeatedly takes that biggest value and parks it at the end.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "pivot"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "pivot"
          }
        ],
        "codeLine": 4,
        "variables": {
          "parent": 3,
          "biggestChild": 8
        },
        "explanation": "3 is smaller than its child 8. In a max-heap the parent must be bigger, so 3 has to sink.",
        "stats": {
          "comparisons": 2,
          "swaps": 0
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "swap"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 1
        },
        "explanation": "Swap them. 8 rises, 3 sinks one level.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "compare"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 3
        },
        "explanation": "3 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 2,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 10
        },
        "explanation": "10 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 4,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "pivot"
          }
        ],
        "codeLine": 4,
        "variables": {
          "parent": 4,
          "biggestChild": 10
        },
        "explanation": "4 is smaller than its child 10. In a max-heap the parent must be bigger, so 4 has to sink.",
        "stats": {
          "comparisons": 6,
          "swaps": 1
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 2
        },
        "explanation": "Swap them. 10 rises, 4 sinks one level.",
        "stats": {
          "comparisons": 6,
          "swaps": 2
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "pivot"
          }
        ],
        "codeLine": 4,
        "variables": {
          "parent": 4,
          "biggestChild": 5
        },
        "explanation": "4 is smaller than its child 5. In a max-heap the parent must be bigger, so 4 has to sink.",
        "stats": {
          "comparisons": 8,
          "swaps": 2
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 3
        },
        "explanation": "Swap them. 5 rises, 4 sinks one level.",
        "stats": {
          "comparisons": 8,
          "swaps": 3
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 4
        },
        "explanation": "4 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 8,
          "swaps": 3
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c1-10",
            "value": 10
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c6-6",
            "value": 6
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "found"
          }
        ],
        "codeLine": 5,
        "variables": {
          "max": 10
        },
        "explanation": "The array is now a max-heap: every parent is bigger than its children, so 10 — the largest value — has floated to the front.",
        "stats": {
          "comparisons": 8,
          "swaps": 3
        },
        "accent": "found"
      },
      {
        "data": [
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "parked": 10,
          "remaining": 6
        },
        "explanation": "10 is the biggest of what's left, so swap it to position 6. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 8,
          "swaps": 4
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 8,
        "variables": {
          "parent": 6,
          "biggestChild": 8
        },
        "explanation": "6 is smaller than its child 8. In a max-heap the parent must be bigger, so 6 has to sink.",
        "stats": {
          "comparisons": 10,
          "swaps": 4
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 5
        },
        "explanation": "Swap them. 8 rises, 6 sinks one level.",
        "stats": {
          "comparisons": 10,
          "swaps": 5
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 6
        },
        "explanation": "6 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 11,
          "swaps": 5
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "parked": 8,
          "remaining": 5
        },
        "explanation": "8 is the biggest of what's left, so swap it to position 5. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 11,
          "swaps": 6
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "pivot"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 8,
        "variables": {
          "parent": 3,
          "biggestChild": 6
        },
        "explanation": "3 is smaller than its child 6. In a max-heap the parent must be bigger, so 3 has to sink.",
        "stats": {
          "comparisons": 13,
          "swaps": 6
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 2,
            "role": "swap"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 7
        },
        "explanation": "Swap them. 6 rises, 3 sinks one level.",
        "stats": {
          "comparisons": 13,
          "swaps": 7
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "compare"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 3
        },
        "explanation": "3 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 13,
          "swaps": 7
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "parked": 6,
          "remaining": 4
        },
        "explanation": "6 is the biggest of what's left, so swap it to position 4. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 13,
          "swaps": 8
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 8,
        "variables": {
          "parent": 1,
          "biggestChild": 5
        },
        "explanation": "1 is smaller than its child 5. In a max-heap the parent must be bigger, so 1 has to sink.",
        "stats": {
          "comparisons": 15,
          "swaps": 8
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 9
        },
        "explanation": "Swap them. 5 rises, 1 sinks one level.",
        "stats": {
          "comparisons": 15,
          "swaps": 9
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "pivot"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 8,
        "variables": {
          "parent": 1,
          "biggestChild": 4
        },
        "explanation": "1 is smaller than its child 4. In a max-heap the parent must be bigger, so 1 has to sink.",
        "stats": {
          "comparisons": 16,
          "swaps": 9
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "swap"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 10
        },
        "explanation": "Swap them. 4 rises, 1 sinks one level.",
        "stats": {
          "comparisons": 16,
          "swaps": 10
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "compare"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 1
        },
        "explanation": "1 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 16,
          "swaps": 10
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "parked": 5,
          "remaining": 3
        },
        "explanation": "5 is the biggest of what's left, so swap it to position 3. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 16,
          "swaps": 11
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 1,
            "role": "pivot"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 8,
        "variables": {
          "parent": 1,
          "biggestChild": 4
        },
        "explanation": "1 is smaller than its child 4. In a max-heap the parent must be bigger, so 1 has to sink.",
        "stats": {
          "comparisons": 18,
          "swaps": 11
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "swap"
          },
          {
            "index": 1,
            "role": "swap"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 21,
        "variables": {
          "swaps": 12
        },
        "explanation": "Swap them. 4 rises, 1 sinks one level.",
        "stats": {
          "comparisons": 18,
          "swaps": 12
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 1,
            "role": "compare"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 1
        },
        "explanation": "1 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 18,
          "swaps": 12
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 7,
        "variables": {
          "parked": 4,
          "remaining": 2
        },
        "explanation": "4 is the biggest of what's left, so swap it to position 2. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 18,
          "swaps": 13
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "index": 2,
            "role": "sorted"
          },
          {
            "index": 3,
            "role": "sorted"
          },
          {
            "index": 4,
            "role": "sorted"
          },
          {
            "index": 5,
            "role": "sorted"
          },
          {
            "index": 6,
            "role": "sorted"
          }
        ],
        "codeLine": 20,
        "variables": {
          "at": 3
        },
        "explanation": "3 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 19,
          "swaps": 13
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "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": {
          "parked": 3,
          "remaining": 1
        },
        "explanation": "3 is the biggest of what's left, so swap it to position 1. It is now in its final place — and the heap shrinks by one.",
        "stats": {
          "comparisons": 19,
          "swaps": 14
        },
        "accent": "swap"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "pointers": [],
        "highlights": [
          {
            "index": 0,
            "role": "compare"
          },
          {
            "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": 20,
        "variables": {
          "at": 1
        },
        "explanation": "1 is already bigger than its children. The heap rule holds here — stop sinking.",
        "stats": {
          "comparisons": 19,
          "swaps": 14
        },
        "accent": "compare"
      },
      {
        "data": [
          {
            "id": "c4-1",
            "value": 1
          },
          {
            "id": "c2-3",
            "value": 3
          },
          {
            "id": "c0-4",
            "value": 4
          },
          {
            "id": "c3-5",
            "value": 5
          },
          {
            "id": "c6-6",
            "value": 6
          },
          {
            "id": "c5-8",
            "value": 8
          },
          {
            "id": "c1-10",
            "value": 10
          }
        ],
        "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": 10,
        "variables": {
          "comparisons": 19,
          "swaps": 14
        },
        "explanation": "Done in 19 comparisons. Heap sort is n log n in the worst case — like merge sort — but it sorts in place, needing no extra array.",
        "stats": {
          "comparisons": 19,
          "swaps": 14
        },
        "accent": "sorted"
      }
    ]
  }
}