AiTechWorlds
AiTechWorlds
Watch Heap Sort build a max-heap and extract the maximum repeatedly, with synced code and O(n log n) guarantees.
def heapify(a, n, i):
largest, l, r = i, 2*i+1, 2*i+2
if l < n and a[l] > a[largest]: largest = l
if r < n and a[r] > a[largest]: largest = r
if largest != i:
a[i], a[largest] = a[largest], a[i]
heapify(a, n, largest)
def heap_sort(a):
n = len(a)
for i in range(n//2 - 1, -1, -1): heapify(a, n, i)
for i in range(n-1, 0, -1):
a[0], a[i] = a[i], a[0]
heapify(a, i, 0)Heap Sort builds a max-heap from the array, then repeatedly swaps the root (largest) to the end and re-heapifies — guaranteed O(n log n) with O(1) extra space.
| Best | O(n log n) |
| Average | O(n log n) |
| Worst | O(n log n) |
| Space | O(1) |
| Stable | No |
| In-place | Yes |
The Heap Sort Visualizer animates how Heap Sort works: it first builds a max-heap from the array, then repeatedly swaps the root (largest value) to the end and re-heapifies the rest. Heap Sort runs in O(n log n) in the best, average, and worst case, uses O(1) extra space, and is not stable.
Generate an array
Click New Array or adjust the size slider for a random dataset.
Play the animation
Press Play to watch the heap build and the max repeatedly move to the sorted tail.
Step and scrub
Step forward/back or drag the timeline to study each heapify.
Read the synced code
The highlighted pseudocode line tracks heapify; switch tabs for C++, Java, Python, or JavaScript.
100% Private — No Server Required
All processing happens directly in your browser. No data is uploaded, stored, or transmitted to any server.
Last reviewed on June 14, 2026 by the AiTechWorlds Tools Team. All processing runs locally in your browser.