Binary heap (tree view)
4217883371956256412
Array (how the heap is stored)
42
0
17
1
88
2
33
3
71
4
9
5
56
6
25
7
64
8
12
9
AlgoPlus//sorting / heap sort
Read the theory

Heap Sort · Crystalline Hierarchy

A magnetic max-heap reforms after every extraction.

Stability
Unstable
In-Place
Yes
Space Complexity
O(1)
Avg Time
O(n log n)
Size10
Legend
Comparing
Swapping / moving
Tracked (min / key)
Pivot
Sorted / found
Out of focus
AI Tutor Workspace
In a nutshell
Heap sort first arranges the list into a max-heap — a tree shape where every parent is at least as big as its children, so the largest value sits at the top. It swaps that top value to the end, shrinks the heap, and repairs it, repeating until sorted. It runs in guaranteed O(n log n) and needs no extra memory.
Ready
Press play to begin the cinematic walkthrough.
Build a max-heap (crystalline hierarchy), then repeatedly extract the root and reform the heap.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Heap Sort · Crystalline Hierarchy.
Code Simulator
1
def heap_sort(arr):
2
    n = len(arr)
3
    for i in range(n // 2 - 1, -1, -1):
4
        heapify(arr, n, i)
5
    for i in range(n - 1, 0, -1):
6
        arr[i], arr[0] = arr[0], arr[i]
7
        heapify(arr, i, 0)
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.