Heap Sort
7 min read.
A comparison-based sorting algorithm that uses a Binary Heap data structure. It divides its input into a sorted and an unsorted region, and iteratively shrinks the unsorted region by extracting the largest element.
Think of it this way: Think of a corporate hierarchy where the boss (largest number) is always at the top. To sort, you repeatedly 'retire' the boss and promote the next best candidate to the top position.
The heap part of the array (indices 0..6) drawn as a tree:
Start: arrange the array as a max-heap, then repeatedly move the max to the back.
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Sort with a heap
Order the steps of in-place heap sort.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Quiz
20 questionsWhat is the "Heap Property" in a Max Heap?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.