Heap sort: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Build a max heap and repeatedly extract its largest value.

Keep the maximum at the heap root, exchange it into the sorted suffix, and repair the remaining active heap.

Open the 3D visualization

Heap sort example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Heap sort

Sorting with a guaranteed worst-case bound.

Choosing an alternative

Merge sort is stable but needs a separate buffer.

Worked example: input and state changes

Example input

38, 12, 65, 8, 43, 27, 91, 19

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 11: Restore max heap.

    State at this checkpoint
    {
      "values": [
        91,
        43,
        38,
        19,
        12,
        27,
        65,
        8
      ],
      "active": [
        0,
        2
      ]
    }
  3. Step 22: Inspect heap parent and children.

    State at this checkpoint
    {
      "values": [
        8,
        43,
        38,
        19,
        12,
        27,
        65,
        91
      ],
      "active": [
        0,
        1
      ]
    }
  4. Step 32: Inspect heap parent and children.

    State at this checkpoint
    {
      "values": [
        12,
        19,
        27,
        8,
        38,
        43,
        65,
        91
      ],
      "active": [
        0,
        2
      ]
    }
  5. Step 43: Complete: values are in ascending order.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": []
    }

Expected final state

Complete: values are in ascending order.

Complexity and input bounds

Family: Sorting. Time complexity: O(n log n).

Space complexity: O(1) iterative auxiliary space.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers between 0 and 99.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Only the active prefix remains a heap; do not repair the sorted suffix.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Sorts a in place. n is the array length. Return value 0 indicates completion. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n) {
    for (let start = Math.floor(n / 2) - 1; start >= -n + 1; start--) {
        let size = n; let i = start;
        if (start < 0) {
            size = n + start;
            let t = a[0]; a[0] = a[size]; a[size] = t; i = 0;
        }
        while (2 * i + 1 < size) {
            let child = 2 * i + 1;
            if (child + 1 < size && a[child + 1] > a[child]) { child++; }
            if (a[i] >= a[child]) { break; }
            let t = a[i]; a[i] = a[child]; a[child] = t; i = child;
        }
    }
    return 0;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.