In-place Heap Sort
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Note: This is not a heap yet! That’s why we’re heapifying.
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 17 has no effect.
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 17 has no effect.
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 41 has no effect.
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 26 has no effect.
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 19 has no effect.
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
32
15
2
17
19
26
41
17
17
Input:
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 17 has no effect.
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
The blue coloring is to make it clear that the three 17s are all part of the same heap. I’ve also grayed out the “root of a heap” statement about the last two 17s since this is redundant information (all subheap nodes are also roots of that subheap).
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
2
17
19
26
41
17
17
Input:
32
15
2
17
19
26
41
17
17
Sinking 2 does something!
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
41
17
19
26
2
17
17
Input:
32
15
41
17
19
26
2
17
17
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
15
41
17
19
26
2
17
17
Input:
32
15
41
17
19
26
2
17
17
Sinking 15 does something!
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
19
41
17
15
26
2
17
17
Input:
32
19
41
17
15
26
2
17
17
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
32
19
41
17
15
26
2
17
17
Input:
32
19
41
17
15
26
2
17
17
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
Sinking 32 does something!
In-place Heap Sort: Phase 1: Heapification
Heap sorting N items:
Punchline: Since tree rooted at position 0 is the root of a heap, then entire array is a heap.
41
19
32
17
15
26
2
17
17
Input:
41
19
32
17
15
26
2
17
17
(No room to leave an unused, spot, so we will actually use position zero for this algorithm!)
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
root
of a
heap
In-place Heap Sort
Heap sorting N items:
41
19
32
17
15
26
2
17
17
Input:
41
19
32
17
15
26
2
17
17
Size: 9
In-place Heap Sort
Heap sorting N items:
41
19
32
17
15
26
2
17
17
Input:
41
19
32
17
15
26
2
17
17
Size: 9
In-place Heap Sort
Heap sorting N items:
32
19
26
17
15
17
2
17
41
Input:
32
19
26
17
15
17
2
17
Size: 8
sorted
In-place Heap Sort
Heap sorting N items:
32
19
26
17
15
17
2
17
41
Input:
32
19
26
17
15
17
2
17
Size: 8
sorted
In-place Heap Sort
Heap sorting N items:
26
19
17
17
15
17
2
32
41
Input:
26
19
17
17
15
17
2
Size: 7
sorted
In-place Heap Sort
Heap sorting N items:
26
19
17
17
15
17
2
32
41
Input:
26
19
17
17
15
17
2
Size: 7
sorted
In-place Heap Sort
Heap sorting N items:
Give the array after this delete.
26
19
17
17
15
17
2
32
41
Input:
26
19
17
17
15
17
2
Size: 7
sorted
In-place Heap Sort
Heap sorting N items:
From here on out, the process is just the same, so verbose steps are omitted...
19
17
17
2
15
17
26
32
41
Input:
19
17
17
2
15
17
Size: 6
sorted
In-place Heap Sort
Heap sorting N items:
2
15
15
17
17
19
26
32
41
Input:
Size: 0
sorted