liveBook · Manning

Heapsort

As explained in Python Concurrency with asyncio

Heapsort is a comparison-sorting algorithm that organizes elements using a heap.

Heapsort underlies priority-queue ordering but is not stable: items with equal priorities are not guaranteed to retain insertion order. section 12.2

Preserving insertion order

Equal-priority work items may be processed in reverse insertion order. Add an order field to each item and compare it after priority to preserve insertion order. section 12.2

Figure 12.1 On the left, a binary tree that satisfies the heap property; on the right, a binary search tree that does not satisfy the heap property
12-01

Compare the heap property with a binary search tree to see the structure supporting priority ordering. section 12.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains instability through observed tie ordering and a tie-breaker, but stops short of deriving heapsort internals.
Examples
Demonstrates reversed equal-priority tasks and ordered tie-breaking with Python work-item classes.
What it assumes
Assumes priority queues and comparable work items, while teaching that equal priorities need explicit tie-breaking.
What you can do after
Diagnose tie-order surprises and add a comparison key when priority queues require insertion-order behavior.
How it connects
Connects heapsort with priority queues, stable sorting, heaps, and binary trees.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.