Loading topic
i is (i - 1) // 2, and its children are 2 * i + 1 and 2 * i + 2. It also assumes you can read Python loops and functions and know what O(n) means. Here we add the ordering 6 min readi to its children at 2*i + 1 and 2*i + 2. It also assumes you know the max-heap property: every parent is at least as large as its children. You should be comfortable reading Python loops and reasoning in Big-O terms.7 min readsift_down function from the previous chapter.7 min readparent(i), left(i), and right(i), the max-heap property, a working sift_down(a, start, end), and build_max_heap(a). It also assumes you are comfortable with Python loops, slicing, and Big-O notation.6 min readsift_down, build_max_heap, and heap_sort functions from the earlier chapters. You should also be comfortable with Big-O notation and with logarithms in base 2. Here we work out how much time heap sort takes and how much extra memory it needs, and we ch7 min readsift_down about n/2 times, and one sift-down can cost O(log n), so the obvious bound is O(n log n). This chapter shows why that obvious bound is loose and pr7 min readsift_down, build_max_heap, and an in-place loop that swaps the root to the end of the array. Python's standard library ships a heap in the heapq module, written in C. This chapter shows how to sort with it, where it differs from the hand-built vers6 min readsift_down, build_max_heap, and heap_sort functions from earlier chapters. It also assumes you can count comparisons in a loop and read logarithms in different bases. Two variants keep heap sort's O(n log n) worst case and O(1) extra space but 6 min read