Skip to content

Heapsort

A comparison sort that builds a heap and repeatedly extracts its extremal element to produce an ordered sequence.

Version
v2 · 2026-11-14 · History
Domain-specific #
9815
Domain group
Applied Sciences & Engineering
Origin domain
Computer Science & Software Engineering
Subdomain
Comparison Sorting Algorithms → Computer Science & Software Engineering
Aliases
Heap sort

Core Idea

Heapsort orders a finite input by maintaining a heap over the not-yet-emitted elements. In a max-heap, the largest remaining element is at the root; the usual array form swaps that root into the final free slot, shrinks the heap, and restores the invariant. Repeating those actions grows a sorted suffix until every element is placed. A min-heap can instead pop successive smallest elements into a separate output list. Both enact the same heap-selection sorting relation, but they differ in storage layout.

For the standard binary-heap method, the worst-case number of operations grows as O(n log n). The familiar O(1) auxiliary-space result applies to the array in-place version; it should not be assigned to Python's documented push/pop example, which allocates heap and result lists. Equal-key order is not generally preserved, so the usual algorithm is unstable. Heapsort may be used as a fallback when another sort threatens poor worst-case behavior, but that surrounding introspective algorithm is not part of heapsort's own identity.

How would you explain it like I'm…

Biggest Toy to the End

Imagine a pile of blocks arranged so the biggest one is always on top. You take the biggest, put it at the end of a line, and fix the pile so the next biggest comes to the top. Keep doing that, and your line ends up in order from small to big.

Sort by Popping the Top

Heapsort is a way for a computer to put things in order using a heap, a special pile where the biggest item is always on top. The computer takes the top item and puts it in the last empty spot at the end of the list. Then it fixes the heap so the next biggest rises to the top, and repeats. Each time, the sorted part at the end grows by one until everything is sorted. If two items are tied, they might swap places, so heapsort doesn't promise to keep ties in their original order.

Heap-Selection Sorting

Heapsort sorts a list by keeping the unsorted elements in a heap. With a max-heap, the largest remaining element is at the root; the in-place array version swaps it into the last free slot, shrinks the heap by one, and restores the heap property, building a sorted section at the end. Alternatively, a min-heap can pop the smallest elements one by one into a separate output list — same idea, different storage. Its worst-case running time grows like n log n. The in-place array version needs only constant extra memory, but the version that builds new lists does not. It is unstable, meaning equal items may not stay in their original order.

 

Heapsort orders a finite input by heap selection: maintain a heap over the remaining elements and repeatedly emit its extreme. In the standard in-place array version, the max-heap root is swapped into the last free slot, the heap boundary shrinks, and sift-down restores the invariant, so a sorted suffix grows until done. Alternatively, a min-heap can pop successive minima into a separate output list; both realize the same sorting relation but differ in memory layout. With a binary heap, worst-case cost is O(n log n). The O(1) auxiliary-space claim applies only to the in-place array version — not, for example, to Python's documented push/pop recipe, which allocates heap and result lists. The usual algorithm is unstable: equal keys may be reordered. Heapsort is sometimes the fallback inside introspective sorting when another method risks bad worst cases, but that hybrid is not part of heapsort itself.

Scope of Application

The heap invariant and complete sorted output matter more than the storage layout.

  • Array sorting. Use the shrinking max-heap suffix method for bounded extra storage.
  • Library pedagogy. Compare Python's min-heap demonstration with the in-place array variant.
  • Complexity analysis. Separate worst-case time, auxiliary memory, and stability.
  • Hybrid sorts. Recognize heap fallback as a use rather than a defining phase.

Clarity

Heapsort must build or populate a heap, repeatedly extract its extreme root, restore the remaining heap, and finish with a sorted sequence. A priority queue that serves only occasional requests is the nearest miss. The usual array max-heap algorithm is in place; Python's documented min-heap example allocates separate output. Neither form guarantees equal-key stability.

Manages Complexity

The heap invariant turns each next-extreme choice into a logarithmic repair rather than a full scan. This yields a worst-case O(n log n) comparison bound while hiding practical costs of heap accesses and key comparison. Time, memory, and stability remain separate coordinates: the implementation form, not the name alone, settles the last two.

Abstract Reasoning

  1. Specify the input and comparison/key order.
  2. Construct or populate a heap with its extremal element at the root.
  3. Extract the root into the ordered output or array suffix.
  4. Repair the heap invariant over the remaining elements.
  5. Repeat to exhaustion and state the variant's storage and stability limits.

Knowledge Transfer

The heap-build/extract/repair strategy carries from the in-place array algorithm to Python's separate-output min-heap version because the extremal invariant and full ordered emission remain intact. The O(1) auxiliary-space claim does not transfer to the latter. A scheduler can use the same heap operations but is only analogous unless it consumes the collection as a complete sorting problem.

Relationships to Other Abstractions

Local relationship map for HeapsortParents appear above the current abstraction, mutual partners to the right, and children below. Node labels state whether each abstraction is prime or domain-specific; colors identify relation types.HeapsortDOMAINDomain-specific abstraction: Sorting Algorithm — is a kind ofSortingAlgorithmDOMAIN

Current abstraction Heapsort Domain-specific

Parents (1) — outgoing typed relations

  • Heapsort is a kind of Sorting Algorithm Domain-specific

    Heapsort rearranges finite comparable input into order by maintaining and draining a heap.

Hierarchy path (1) — routes to 1 parentless root

Neighborhood in Abstraction Space

Heapsort sits in a moderately populated region (60th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.

Family — Memory Storage, Retrieval & Encoding (9 abstractions)

Nearest neighbors

Computed from structural-signature embeddings · 2026-11-14