Skip to main content

Heap sort with example

Short answer

Internal sorting fits all data in RAM; external sorting handles data on disk with sequential block access. Heap sort builds a max-heap in O(n), then repeatedly swaps root with the last heap element and sifts down. Example sorted: 12,25,33,37,48,57,86,92. Time O(n log n), space O(1).

Differentiate internal and external sorting. Explain heap sort with an appropriate example.

Internal and external sorting

Internal sortingExternal sorting
All data fits in main memory (RAM)Data is too large and is kept on disk or tape
Direct access to any elementData is read in blocks; access is sequential/block-wise
Examples: bubble, quick, heap, insertion sortExamples: external merge sort, polyphase merge
Cost is measured in comparisonsCost is dominated by disk I/O operations

Heap sort

Heap sort uses a max-heap: an almost complete binary tree in which every parent is ≥ its children, stored in an array (children of index i at 2i+1 and 2i+2).

Algorithm:

  1. Build a max-heap from the array (heapify from the last parent n/2 − 1 down to 0).
  2. Swap the root (largest) with the last element of the heap, and reduce the heap size by one. The largest element is now in its final place.
  3. Restore the heap property by sifting the new root down.
  4. Repeat steps 2–3 until only one element remains.
void sift(int a[], int i, int n) {            /* sift a[i] down in heap of size n */
    int l, g, t;
    while ((l = 2 * i + 1) < n) {
        g = (a[l] > a[i]) ? l : i;
        if (l + 1 < n && a[l + 1] > a[g]) g = l + 1;
        if (g == i) return;
        t = a[i]; a[i] = a[g]; a[g] = t; i = g;
    }
}
void heap_sort(int a[], int n) {
    int i, t;
    for (i = n / 2 - 1; i >= 0; i--) sift(a, i, n);   /* build heap */
    for (i = n - 1; i > 0; i--) {
        t = a[0]; a[0] = a[i]; a[i] = t;              /* move max to end */
        sift(a, 0, i);                                /* repair heap */
    }
}

Example: sort 25, 57, 48, 37, 12, 92, 86, 33

Build the max-heap:

StepNode heapifiedArray
start–25, 57, 48, 37, 12, 92, 86, 33
1a[3]25, 57, 48, 37, 12, 92, 86, 33
2a[2]25, 57, 92, 37, 12, 48, 86, 33
3a[1]25, 57, 92, 37, 12, 48, 86, 33
4a[0]92, 57, 86, 37, 12, 48, 25, 33

Initial heap: 92, 57, 86, 37, 12, 48, 25, 33

level 0:  92
level 1:  57   86
level 2:  37   12   48   25
level 3:  33

Sorting phase:

PassMax moved to endHeap part after re-heapifySorted part
192 (last position 8)86, 57, 48, 37, 12, 33, 2592
286 (last position 7)57, 37, 48, 25, 12, 3386, 92
357 (last position 6)48, 37, 33, 25, 1257, 86, 92
448 (last position 5)37, 25, 33, 1248, 57, 86, 92
537 (last position 4)33, 25, 1237, 48, 57, 86, 92
633 (last position 3)25, 1233, 37, 48, 57, 86, 92
725 (last position 2)1225, 33, 37, 48, 57, 86, 92
––(empty)12, 25, 33, 37, 48, 57, 86, 92

Sorted output: 12, 25, 33, 37, 48, 57, 86, 92

Complexity: O(n log n) in best, average and worst case; O(1) extra space; not stable.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 7 questions →