This page is the module index for heaps.
Review the heap invariant and heapsort, then see priority queues for indexed updates.
heaps/heapsort.py—heapifyis O(n);heapsortis O(n log n) time and O(1) extra memory. Not stable.heaps/priority_queue.py— min-heap withdecrease_keythrough an index map. Push, pop, and decrease-key are O(log n).
Industry
A working engineer uses heapq as a priority queue. Python documents a
min-heap whose smallest item is always heap[0], with heapify in linear
time. Removing that root and restoring the heap is logarithmic, and
repeating the removal sorts in O(n log n). The same page notes that this
heapsort is not stable, which is also true of heapsort here.