Heap Sort Visualizer

Watch Heap Sort build a max heap and extract values into sorted order with animated operations, playback controls, and metrics.

Current status: Building the max heap: compare children and lift the larger value toward the root.

Comma-separated numbers, up to 20 values.

Balanced

Live operation

compare

Step 1 / 67
42
[0]
18
[1]
33
[2]
9
[3]
27
[4]
16
[5]
30
[6]
5
[7]
11
[8]
21
[9]
Compare heap nodesSift / extract swapSorted suffix

Live binary heap

42[0]18[1]33[2]9[3]27[4]16[5]30[6]5[7]11[8]21[9]

Comparisons

1

Moves / writes

0

Confirmed sorted

0 / 10

Algorithm notes

What to watch for

Heap Sort uses the array as a complete binary tree. Heapify establishes the max-heap rule; each extraction moves the current maximum to the sorted suffix.

Concept guide

Review the mental model, tradeoffs, and practical use cases after you experiment.

Heap Sort Complete Info Card

Comparison SortIn-Place

Heap Sort is an efficient sorting algorithm that uses a binary heap data structure. It first builds a max heap from the input data, then repeatedly extracts the maximum element to produce a sorted array.

Algorithm Characteristics

Time Complexity (Best)

Same as average case

O(n log n)

Time Complexity (Average)

Consistent performance

O(n log n)

Time Complexity (Worst)

Guaranteed performance

O(n log n)

Space Complexity

In-place sorting algorithm

O(1)

Stable

May change order of equal elements

No

Comparison Sort

Compares elements to determine order

Yes

Sorting Process Steps

1

Build a max heap from input array

2

Swap root (max) with last element

3

Reduce heap size by one

4

Heapify the new root

5

Repeat until heap size is 1

Heap Operations

Heapify

Maintain heap property at nodeO(log n)

Build Heap

Convert array to heapO(n)

Extract Max

Remove and return max elementO(log n)

Comparison with Other O(n log n) Sorts

AlgorithmTimeSpaceStable
Quick SortO(n log n) avgO(log n)No
Merge SortO(n log n)O(n)Yes
Heap SortO(n log n)O(1)No

Advantages

  • Guaranteed O(n log n) performance
  • Memory efficient (in-place)
  • No recursion (avoids stack overflow)

Limitations

  • Not stable (equal elements may be reordered)
  • Poor cache performance compared to Quick Sort
  • Slower in practice than Quick Sort for arrays
Did You Know? Heap Sort is often used in embedded systems because it provides guaranteed O(n log n) performance with constant O(1) space.
In-placeO(n log n)Heap-based