Heap sort uses a special complete binary tree, the heap, stored in an array. It sorts in O(nlogn) time in the worst case, like merge sort, but needs no extra array.
Heaps
The heap is stored in an array level by level. With positions numbered from 1 (as in the study material): the children of position j are at 2j and 2j+1, and its parent at ⌊j/2⌋. (With 0-based C arrays: 2j+1, 2j+2, ⌊(j−1)/2⌋, as in Unit 6.)
Heap operations
Insert (sift up): put the new key at the next free position (the end of the array), then swap it with its parent while it is larger. O(logn).
Delete the maximum (sift down): move the last key to the root, then swap it with its larger child while it is smaller than that child. O(logn).
There are two ways to build a heap from n keys:
Top-down: insert the keys one by one into an empty heap: O(nlogn).
Bottom-up: put the keys in the array as given, then sift down every internal node from position ⌊n/2⌋ back to 1: only O(n), because most nodes are near the bottom.
Heap sort
Build a max-heap from the array (bottom-up).
For size =n down to 2: swap the root (largest key) with the last element of the heap, reduce the heap size by 1, and sift the new root down.
The largest keys collect at the end of the array, so the array ends up in ascending order. Time O(nlogn) in every case (building O(n) plus n−1 deletions of O(logn)); extra space O(1); not stable.
Comparison of the internal sorting methods
Method
Best
Average
Worst
Extra space
Stable
Insertion sort
O(n)
O(n2)
O(n2)
O(1)
Yes
Bubble sort (with flag)
O(n)
O(n2)
O(n2)
O(1)
Yes (with <)
Quick sort
O(nlogn)
O(nlogn)
O(n2)
O(logn) stack
No
Merge sort
O(nlogn)
O(nlogn)
O(nlogn)
O(n)
Yes
Heap sort
O(nlogn)
O(nlogn)
O(nlogn)
O(1)
No
No comparison-based sort can beat O(nlogn) comparisons in the worst case (Unit 1).
Key takeaways
Max-heap: complete binary tree, parent ≥ children; root is the maximum.
Array form (1-based): children 2j, 2j+1; parent ⌊j/2⌋.
Insert = sift up; delete max = move last to root and sift down; both O(logn).
Heap sort: build (O(n)), then n−1 deletions: O(nlogn) worst case, in place, not stable.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Build a max-heap bottom-up from 10 20 15 30 40 and show the array after each sift-down; then show the array after the first step of heap sort.
Show worked answer
Array (1-based): 10 20 15 30 40; n = 5, start at ⌊5/2⌋=2. Sift down position 2 (20): children 30, 40; larger is 40 > 20: swap → 10 40 15 30 20. Sift down position 1 (10): children 40, 15; swap with 40 → 40 10 15 30 20; 10 now at position 2 with children 30, 20; swap with 30 → 40 30 15 10 20. Heap: 40 30 15 10 20. First heap-sort step: swap root with last → 20 30 15 10 | 40; sift down 20: children 30, 15; swap with 30 → 30 20 15 10 | 40 (20’s child 10 is smaller, stop).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/searching-and-sorting-techniques/heap-sort/ to read this page.