Sub topic 14
Variants: Bottom-Up Heap Sort and d-ary Heaps
This chapter assumes you can already write sift_down, build_max_heap, and heap_sort, and that you know heap sort runs in O(n log n) time. Both variants below keep that bound. They change the constant factors: how many comparisons are made and how the memory is accessed.
Bottom-Up Heap Sort
In the standard sift-down, each level costs two comparisons. The first picks the larger child. The second checks whether the sinking element is already big enough to stop. During the sort phase, the element being sifted came from the end of the array, so it is usually small. It almost always sinks all the way to a leaf, which makes the second comparison wasted work at most levels.
Bottom-up heap sort, sometimes credited to Wegener, splits the work into two passes:
- Walk from the root to a leaf, always stepping to the larger child. This costs one comparison per level.
- Climb back up that path until you find a value at least as large as the sinking element. Because the element belongs near the bottom, this climb is short.
- Put the element at that position and shift each value on the path above it up one level.
def sift_down_bottom_up(a, start, end):
# Pass 1: descend to a leaf along the larger child.
j = start
while True:
child = 2 * j + 1
if child >= end:
break
if child + 1 < end and a[child + 1] > a[child]:
child += 1
j = child
# Pass 2: climb until a[j] >= the sinking element.
while a[j] < a[start]:
j = (j - 1) // 2
# Place the element at j and shift the path above j up one level.
x = a[j]
a[j] = a[start]
while j > start:
j = (j - 1) // 2
a[j], x = x, a[j]
def heap_sort_bottom_up(a):
n = len(a)
for start in range(n // 2 - 1, -1, -1):
sift_down_bottom_up(a, start, n)
for end in range(n - 1, 0, -1):
a[0], a[end] = a[end], a[0]
sift_down_bottom_up(a, 0, end)
data = [5, 2, 9, 1, 7, 3, 8, 6]
heap_sort_bottom_up(data)
print(data) # [1, 2, 3, 5, 6, 7, 8, 9]The standard version makes about 2n log₂ n comparisons. The bottom-up version makes about n log₂ n + O(n). The savings show up most when comparisons are expensive, for example with string keys or a custom comparison function. For plain integers, the extra index work can cancel out the gain.
d-ary Heaps
A d-ary heap gives each node up to d children instead of 2. The index formulas generalize directly:
- children of
i:d*i + 1throughd*i + d - parent of
i:(i - 1) // d
The tree height drops to log_d n. Each level now needs d − 1 comparisons to find the largest child, so there are fewer levels but more work per level.
def sift_down_d(a, start, end, d):
root = start
while True:
first = d * root + 1
if first >= end:
break
last = min(first + d, end)
child = max(range(first, last), key=a.__getitem__)
if a[root] >= a[child]:
break
a[root], a[child] = a[child], a[root]
root = child
def heap_sort_d(a, d=4):
n = len(a)
for start in range((n - 2) // d, -1, -1):
sift_down_d(a, start, n, d)
for end in range(n - 1, 0, -1):
a[0], a[end] = a[end], a[0]
sift_down_d(a, 0, end, d)
data = [5, 2, 9, 1, 7, 3, 8, 6, 4]
heap_sort_d(data, d=3)
print(data) # [1, 2, 3, 4, 5, 6, 7, 8, 9]The last non-leaf node is the parent of index n − 1, which is (n - 2) // d.
| Variant | Height | Comparisons per sift-down | Main benefit |
|---|---|---|---|
| Binary, standard | log₂ n | about 2 log₂ n | Simplest |
| Binary, bottom-up | log₂ n | about log₂ n + small | Fewer comparisons |
| d-ary (d = 4) | log₄ n | about 3 log₄ n = 1.5 log₂ n | Siblings sit next to each other in memory, so fewer cache misses |
Exercise
In a 3-ary heap stored in a zero-indexed array, what are the children of index 2, and what is the parent of index 10?
Show answer
The children are 3·2+1 = 7, 8, and 9. The parent of 10 is (10 − 1) // 3 = 3.
Exercise
Why does bottom-up heap sort save more comparisons during the sort phase than during the build phase?
Show answer
In the sort phase, the element moved to the root comes from the end of the array, so it is usually small and belongs near a leaf. The short climb back up then costs few comparisons. In the build phase, most sift-downs start at low nodes with short paths, so the standard version already stops early and there is little to save.