Heap Sort - Heapify Operation
Heapify Operation
Heapify maintains the heap property by moving elements up or down the heap.
Heapify Down
When an element violates the heap property, we “heapify down” by comparing with children and swapping if needed.
Implementation
Building a Heap
To build a max heap from an array, heapify all non-leaf nodes from bottom to top.
Key Points
- Start from bottom: Heapify non-leaf nodes from bottom up
- Compare with children: Check both left and right children
- Swap if needed: Move larger child up
- Recurse: Continue heapifying the affected subtree
What’s Next?
Now that you understand heapify, let’s see the complete heap sort algorithm.
Discussion
Loading comments...