- 2069 Bhadra · 2+6 marks
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 sorting | External sorting |
|---|---|
| All data fits in main memory (RAM) | Data is too large and is kept on disk or tape |
| Direct access to any element | Data is read in blocks; access is sequential/block-wise |
| Examples: bubble, quick, heap, insertion sort | Examples: external merge sort, polyphase merge |
| Cost is measured in comparisons | Cost 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:
- Build a max-heap from the array (heapify from the last parent n/2 − 1 down to 0).
- 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.
- Restore the heap property by sifting the new root down.
- 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:
| Step | Node heapified | Array |
|---|---|---|
| start | – | 25, 57, 48, 37, 12, 92, 86, 33 |
| 1 | a[3] | 25, 57, 48, 37, 12, 92, 86, 33 |
| 2 | a[2] | 25, 57, 92, 37, 12, 48, 86, 33 |
| 3 | a[1] | 25, 57, 92, 37, 12, 48, 86, 33 |
| 4 | a[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:
| Pass | Max moved to end | Heap part after re-heapify | Sorted part |
|---|---|---|---|
| 1 | 92 (last position 8) | 86, 57, 48, 37, 12, 33, 25 | 92 |
| 2 | 86 (last position 7) | 57, 37, 48, 25, 12, 33 | 86, 92 |
| 3 | 57 (last position 6) | 48, 37, 33, 25, 12 | 57, 86, 92 |
| 4 | 48 (last position 5) | 37, 25, 33, 12 | 48, 57, 86, 92 |
| 5 | 37 (last position 4) | 33, 25, 12 | 37, 48, 57, 86, 92 |
| 6 | 33 (last position 3) | 25, 12 | 33, 37, 48, 57, 86, 92 |
| 7 | 25 (last position 2) | 12 | 25, 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)
Related questions
- Stable vs unstable sorting; heap · Data Structure and Algorithm, chapter 7
- Heap sort on 37, 33, 26, 92, ... · Data Structure and Algorithm, chapter 7
- Heap tree insertion and heap sort · Data Structure and Algorithm, chapter 7
- Algorithm for an exchange sort · Data Structure and Algorithm, chapter 7 · asked 2×
- Merge and radix sort on 142, 523, ... · Data Structure and Algorithm, chapter 7
- Radix sort on 25, 57, 48, 37, ... · Data Structure and Algorithm, chapter 7
- Internal/external risks; Nepal; hydropower · Project Engineering, chapter 5
- Page size impact; internal, external fragmentation · Operating System, chapter 4
- Noise, distortion, interference; internal and external noise · Communication Systems, chapter 1
- External vs internal noise; interference sources; need for modulation · Communication Systems, chapter 1