MCS-208 · Unit 9

Searching and Sorting Techniques

Section 7 of 9

Block 3 · Unit 9Searching and Sorting Techniques10 min · Section 7 of 9

9.7Heap Sort

Covers IGNOU MCS-208 §9.5.5

Heap sort uses a special complete binary tree, the heap, stored in an array. It sorts in O(nlog⁡n)O(n \log n) 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 jj are at 2j2j and 2j+12j + 1, and its parent at ⌊j/2⌋\lfloor j/2 \rfloor. (With 0-based C arrays: 2j+12j + 1, 2j+22j + 2, ⌊(j−1)/2⌋\lfloor (j - 1)/2 \rfloor, 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(log⁡n)O(\log n).
  • 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(log⁡n)O(\log n).

There are two ways to build a heap from nn keys:

  • Top-down: insert the keys one by one into an empty heap: O(nlog⁡n)O(n \log n).
  • Bottom-up: put the keys in the array as given, then sift down every internal node from position ⌊n/2⌋\lfloor n/2 \rfloor back to 1: only O(n)O(n), because most nodes are near the bottom.

Heap sort

  1. Build a max-heap from the array (bottom-up).
  2. For size =n= 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(nlog⁡n)O(n \log n) in every case (building O(n)O(n) plus n−1n - 1 deletions of O(log⁡n)O(\log n)); extra space O(1)O(1); not stable.

Comparison of the internal sorting methods

MethodBestAverageWorstExtra spaceStable
Insertion sortO(n)O(n)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)Yes
Bubble sort (with flag)O(n)O(n)O(n2)O(n^2)O(n2)O(n^2)O(1)O(1)Yes (with <)
Quick sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n2)O(n^2)O(log⁡n)O(\log n) stackNo
Merge sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(n)O(n)Yes
Heap sortO(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(nlog⁡n)O(n \log n)O(1)O(1)No

No comparison-based sort can beat O(nlog⁡n)O(n \log n) 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 2j2j, 2j+12j+1; parent ⌊j/2⌋\lfloor j/2 \rfloor.
  • Insert = sift up; delete max = move last to root and sift down; both O(log⁡n)O(\log n).
  • Heap sort: build (O(n)O(n)), then n−1n-1 deletions: O(nlog⁡n)O(n \log n) worst case, in place, not stable.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a 1-based heap array, where are the children of position 6?

Show answer

Answer: C. 12 and 13

2⋅6=122 \cdot 6 = 12 and 2⋅6+1=132 \cdot 6 + 1 = 13. Position 3 is its parent (⌊6/2⌋\lfloor 6/2 \rfloor).

Question 2 · multiple choice

Which array is a max-heap?

Show answer

Answer: A. 90 70 80 30 60 50

Check each parent: 90 ≥ 70, 80; 70 ≥ 30, 60; 80 ≥ 50. In the second array 75 > its parent 70; the last array is a min-heap order.

Question 3 · multiple choice

What is the time to build a heap bottom-up from nn keys?

Show answer

Answer: A. O(n)O(n)

Most nodes are near the leaves and sift down only a little; the total is O(n)O(n). Top-down insertion is O(nlog⁡n)O(n \log n).

Question 4 · multiple choice

What does this program print?

C · q9-heap.c
#include <stdio.h>

int main(void)
{
    int a[8] = {0, 50, 30, 40, 10, 20, 35};   /* 1-based max-heap, 6 keys */
    int n = 6;
    a[++n] = 45;                               /* insert 45 at position 7 */
    int j = n;
    while (j > 1 && a[j / 2] < a[j]) {
        int t = a[j]; a[j] = a[j / 2]; a[j / 2] = t;
        j /= 2;
    }
    for (int i = 1; i <= n; i++) printf("%d ", a[i]);
    printf("(45 is at %d)\n", j);
    return 0;
}
Show answer

Answer: D. 50 30 45 10 20 35 40 (45 is at 3)

45 starts at position 7, whose parent (3) holds 40 < 45: swap. Its new parent (1) holds 50 > 45: stop.

Question 5 · multiple choice

Which sorting method is O(nlog⁡n)O(n \log n) in the worst case and needs only O(1)O(1) extra space?

Show answer

Answer: C. Heap sort

Quick sort can be O(n2)O(n^2); merge sort needs O(n)O(n) extra space; insertion sort is O(n2)O(n^2).

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\lfloor 5/2 \rfloor = 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).

↑ ↓ move↵ openTab switch type