🌟 What Is Heap Sort?
Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure.
It works in two main stages:
- Build a heap from the given data.
- Repeatedly remove the largest (or smallest) element from the heap and rebuild it until everything is sorted.
Think of it like a game:
You first build a mountain of numbers where the largest number sits on the top.
Then, you keep taking the top stone (largest one), put it aside, and rebuild the mountain with the rest — until the mountain disappears and your stones are lined up neatly in order.
🌳 What Is a Heap?
A heap is a special type of binary tree that satisfies the heap property.
There are two types:
- Max Heap: The parent node is always greater than or equal to its children.
→ The largest element is always at the top (root). - Min Heap: The parent node is always smaller than or equal to its children.
→ The smallest element is always at the top.
For sorting in ascending order, we usually use a Max Heap.
⚙️ How Heap Sort Works (Step-by-Step)
Let’s say we have this array:
[4, 10, 3, 5, 1]
Step 1: Build a Max Heap
We rearrange the array to make it a Max Heap.
10
/ \
5 3
/ \
4 1
Now the largest element (10) is at the top!
Step 2: Swap and Reduce Heap Size
We swap the root (10) with the last element (1).
[1, 5, 3, 4, 10]
Then we reduce the heap size (ignore the last sorted element 10) and heapify again (rebuild the heap to maintain the max heap property).
After heapifying:
5
/ \
4 3
/
1
Step 3: Repeat Until Sorted
We repeat the process:
- Swap the root (largest element) with the last unsorted element.
- Reduce heap size.
- Heapify again.
After all steps:
Sorted array → [1, 3, 4, 5, 10]
🖼️ Diagram: Heap Sort in Action
Initial Array: [4, 10, 3, 5, 1]
Step 1: Build Max Heap
10
/ \
5 3
/ \
4 1
Step 2: Swap top (10) with last → [1, 5, 3, 4, 10]
Heapify again
5
/ \
4 3
/
1
Step 3: Continue swapping and heapifying
Final Sorted Array → [1, 3, 4, 5, 10]
💡 Simple Analogy
Imagine a teacher asking all students to form a line from tallest to shortest.
Instead of comparing each student one by one, the teacher builds a pyramid where the tallest student always climbs to the top.
Then the teacher sends that tallest one out of the pyramid and rebuilds it with the remaining students — repeating until everyone stands in perfect order. 👩🏫📏
That’s exactly what Heap Sort does with numbers!
🧩 Pseudocode (Easy to Follow)
HeapSort(arr):
buildMaxHeap(arr)
for i from end to start:
swap(arr[0], arr[i])
heapify(arr, 0, i)
⏱️ Time Complexity
| Operation | Time |
|---|---|
| Building Heap | O(n) |
| Each Deletion + Heapify | O(log n) |
| Total Time | O(n log n) |
| Space Used | O(1) (in-place) |
✅ Heap Sort is very efficient — even in the worst case, it remains O(n log n).
💪 Advantages
- Works efficiently for large datasets.
- Doesn’t need extra space (it’s done inside the same array).
- Guaranteed O(n log n) performance — no nasty surprises.
⚠️ Disadvantages
- Not stable (equal elements may not keep their original order).
- Slightly slower in practice than Quick Sort for smaller inputs.
- Heap operations can be tricky to visualize at first.
🧠 Quick Summary Table
| Feature | Description |
|---|---|
| Type | Comparison-based |
| Data Structure Used | Binary Heap |
| Time Complexity | O(n log n) |
| Space Complexity | O(1) |
| Sorting Type | In-place |
| Stability | Not Stable |
| Best For | Large datasets |