{
  "id": "heapsort",
  "title": "Heapsort",
  "lang": "en",
  "source_lang": "en",
  "summary": "Heapsort is an in-place comparison sorting algorithm that uses a binary heap to achieve worst-case O(n log n) running time with constant auxiliary storage.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Heap sort"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/heapsort/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/heapsort/",
      "json": "https://aiwiki.page/zh/heapsort.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Heapsort is a sorting algorithm that organizes elements into a binary heap, repeatedly removes the largest element, and places it at the end of the remaining unsorted portion. Its standard array-based implementation sorts in place, uses constant auxiliary storage, and has worst-case running time \\(O(n\\log n)\\). Unlike quicksort, its worst-case bound does not depend on choosing favorable pivots. Standard heapsort is not stable: elements with equal keys may change their relative order."
    },
    {
      "heading": "Heap structure",
      "text": "A binary heap is a data structure with two properties:\n\n- Shape: it is a complete binary tree, with every level full except possibly the last, which is filled from left to right.\n- Heap order: in a max-heap, each parent's key is at least as large as the keys of its children.\n\nThe root of a max-heap therefore contains a largest element, although the remaining elements are not necessarily in sorted order. A min-heap reverses the ordering condition and places a smallest element at the root.\n\nA complete binary tree can be represented compactly in an array without storing explicit pointers. With zero-based indexing, the children of position \\(i\\) are at\n\n\\[\\operatorname{left}(i)=2i+1, \\qquad \\operatorname{right}(i)=2i+2,\\]\n\nprovided those indices are within the active heap. For \\(i>0\\), its parent is at\n\n\\[\\operatorname{parent}(i)=\\left\\lfloor\\frac{i-1}{2}\\right\\rfloor.\\]\n\nThis representation allows heapsort to keep both the active heap and the sorted output in the original array. For ascending order, the usual implementation uses a max-heap; for descending order, it can use a min-heap."
    },
    {
      "heading": "Algorithm",
      "text": "Heapsort has two phases.\n\nBuilding the heap\n\nThe input array is first rearranged into a max-heap. The standard bottom-up construction visits all internal nodes in reverse index order, starting at\n\n\\[\\left\\lfloor\\frac{n}{2}\\right\\rfloor-1\\]\n\nand ending at the root.\n\nAt each node, a sift-down operation restores heap order in the subtree rooted there. It compares the current element with its larger child. If the child is larger, the two elements are exchanged, and the operation continues downward. When a node is processed during bottom-up construction, both of its child subtrees are already heaps.\n\nLeaves need no processing because a single-element subtree automatically satisfies heap order.\n\nRepeated extraction\n\nAfter heap construction, the largest element is at index \\(0\\). The algorithm:\n\n- Exchanges the root with the last element of the active heap.\n- Shrinks the active heap by one element.\n- Sifts the new root downward to restore heap order.\n- Repeats until at most one element remains in the heap.\n\nThe array is thus divided into an active heap on the left and a sorted suffix on the right. Each extraction adds the largest remaining element to the beginning of that suffix.\n\nPseudocode\n\nThe following pseudocode sorts in ascending order. The parameter size is the number of elements in the active heap, so valid heap indices are strictly less than size.\n\nprocedure sift_down(A, root, size):\n    while 2 * root + 1 < size:\n        child = 2 * root + 1\n\n        if child + 1 < size and A[child] < A[child + 1]:\n            child = child + 1\n\n        if A[root] >= A[child]:\n            return\n\n        swap A[root], A[child]\n        root = child\n\nprocedure heapsort(A):\n    n = length(A)\n\n    for start = floor(n / 2) - 1 downto 0:\n        sift_down(A, start, n)\n\n    for end = n - 1 downto 1:\n        swap A[0], A[end]\n        sift_down(A, 0, end)\n\nA loop whose starting index is below its ending bound performs no iterations. Consequently, this algorithm also handles empty arrays and single-element arrays without special sorting steps.\n\nImplementations can reduce assignments by holding the displaced element in a temporary variable, moving larger children upward, and writing the displaced element only once at its final position."
    },
    {
      "heading": "Example",
      "text": "Consider the array\n\n[4, 10, 3, 5, 1]\n\nBottom-up heap construction produces\n\n[10, 5, 3, 4, 1]\n\nSuccessive extractions and heap repairs produce the following states. The vertical bar separates the active heap from the sorted suffix.\n\n[5, 4, 3, 1 | 10]\n[4, 1, 3    | 5, 10]\n[3, 1       | 4, 5, 10]\n[1          | 3, 4, 5, 10]\n\nThe completed array is\n\n[1, 3, 4, 5, 10]\n\nThe intermediate heap portions are not sorted sequences; only the heap-order condition is maintained."
    },
    {
      "heading": "Correctness",
      "text": "The extraction phase can be explained through a loop invariant. Before each extraction:\n\n- The active prefix is a max-heap.\n- The suffix is sorted in ascending order.\n- Every element in the active heap is no greater than every element in the suffix.\n\nThe root is a largest element of the active heap. Exchanging it with the last active element therefore places the next-largest remaining element immediately before the existing sorted suffix.\n\nOnly the replacement root can violate heap order: its child subtrees have not changed internally. Sift-down repairs that violation while leaving the suffix untouched. When the active heap has at most one element, the entire array is sorted.\n\nThis reasoning assumes comparisons define a consistent ordering. Inconsistent comparison functions can invalidate both the heap property and the sortedness guarantee."
    },
    {
      "heading": "Complexity",
      "text": "Linear-time heap construction\n\nA single sift-down may take \\(O(\\log n)\\) time, but applying that upper bound independently to every node gives an unnecessarily loose bound for bottom-up construction.\n\nMost nodes lie near the leaves and can move only a short distance. The number of nodes of height \\(h\\) decreases geometrically, so the total work is bounded by a sum of the form\n\n\\[O\\left( n\\sum_{h=0}^{\\infty}\\frac{h}{2^h} \\right)=O(n).\\]\n\nThus bottom-up heap construction takes \\(\\Theta(n)\\) time. Constructing a heap by inserting elements individually has an \\(O(n\\log n)\\) worst-case bound instead.\n\nSorting time\n\nThere are \\(n-1\\) extractions, and each requires at most \\(O(\\log n)\\) work. The resulting worst-case time complexity is\n\n\\[\\Theta(n\\log n).\\]\n\nFor random permutations of distinct keys, the usual implementation also has expected running time \\(\\Theta(n\\log n)\\). As a comparison sort, heapsort is subject to the general \\(\\Omega(n\\log n)\\) worst-case comparison lower bound for sorting distinct keys.\n\nBest-case behavior depends on the precise implementation and assumptions about keys. In the early-stopping sift-down shown above, an array of equal keys takes \\(\\Theta(n)\\) time: each heap repair stops immediately. It is therefore misleading to describe every heapsort implementation as taking \\(\\Theta(n\\log n)\\) on every possible input. Nevertheless, heapsort is generally not adaptive to existing sorted order.\n\nAuxiliary storage\n\nWith iterative sift-down, the auxiliary space complexity is \\(O(1)\\), excluding the input array. The heap is stored directly in that array, and only a few indices and temporary values are needed.\n\nAn implementation using recursive sift-down can require \\(O(\\log n)\\) call-stack space."
    },
    {
      "heading": "Stability and ordering",
      "text": "A stable sort preserves the original relative order of records with equal sorting keys. Standard heapsort does not have this property because its long-distance exchanges can move equal-key records past one another.\n\nFor example, two records with the same key can exchange relative positions when one is moved from the root to the end of the active heap.\n\nStability can be imposed by comparing each record using the pair\n\n\\[(\\text{key},\\text{original position}),\\]\n\nbut storing original positions generally requires additional storage unless that information is already available. This changes the storage characteristics of the overall method rather than making ordinary heapsort intrinsically stable.\n\nHeapsort requires only comparisons, not arithmetic on keys. It can therefore sort records, strings, or other objects under a suitable comparator. Its usual complexity bounds assume comparisons and element moves have constant cost; expensive comparisons or large records can substantially affect actual running time."
    },
    {
      "heading": "Practical performance and applications",
      "text": "Heapsort is useful when both a worst-case \\(O(n\\log n)\\) bound and low auxiliary memory are important. Its storage requirements do not grow with input size when implemented iteratively.\n\nIts practical limitations include:\n\n- Memory locality. Sift-down follows parent-to-child paths with increasingly large index jumps. These accesses often make less effective use of the CPU cache than sequential scans.\n- Comparison and movement costs. Each level commonly requires selecting the larger child and then comparing that child with the displaced element.\n- Limited adaptivity. Already sorted or nearly sorted inputs generally do not lead to the improvements available from adaptive sorting methods.\n- Instability. Applications requiring stable ordering need a different algorithm or additional machinery.\n\nPerformance depends on hardware, key distributions, comparison costs, and implementation details. Heapsort often runs more slowly than well-engineered quicksort-based methods despite offering a stronger worst-case guarantee than basic quicksort.\n\nA prominent application is introsort, which begins with quicksort-style partitioning and switches to heapsort when partitioning becomes too deep. This retains much of quicksort's typical performance while preventing quadratic worst-case behavior.\n\nThe underlying heap operations also support a priority queue. However, heapsort is specifically an array-sorting procedure: repeatedly removing items from a separately allocated priority queue is related, but does not necessarily have the same in-place storage properties."
    },
    {
      "heading": "Comparison with related algorithms",
      "text": "Algorithm | Worst-case time | Typical auxiliary storage | Stable in its standard form?\nHeapsort | \\(\\Theta(n\\log n)\\) | \\(O(1)\\) with iterative sift-down | No\nQuicksort | \\(\\Theta(n^2)\\) | Typically \\(O(\\log n)\\) expected stack space; implementation-dependent | No\n[[merge-sort | Merge sort]] | \\(\\Theta(n\\log n)\\) | Usually \\(O(n)\\) for array-based implementations\n[[insertion-sort | Insertion sort]] | \\(\\Theta(n^2)\\) | \\(O(1)\\)\n\nMerge sort combines stability with predictable running time, but conventional array implementations require a temporary buffer. Insertion sort is useful for small or nearly sorted inputs. Heapsort's distinctive combination is worst-case \\(O(n\\log n)\\) time and constant auxiliary storage."
    },
    {
      "heading": "History and variants",
      "text": "J. W. J. Williams introduced heapsort in 1964. Robert W. Floyd published an improved heap-construction method in the same year. Floyd's bottom-up construction establishes the heap in linear time and is standard in modern presentations.\n\nVariants alter the heap structure or the repair procedure:\n\n- Bottom-up heapsort variants can descend along a path of larger children before locating the displaced element's final position, reducing comparisons in some settings. This extraction strategy is distinct from bottom-up heap construction.\n- \\(d\\)-ary heapsort gives each node up to \\(d\\) children. A larger branching factor reduces heap height but increases the work required to select a largest child.\n- Partial heapsort performs only enough extractions to obtain a requested number of largest elements. Building a max-heap and extracting \\(k\\) elements takes \\(O(n+k\\log n)\\) time, although the extracted elements accumulate at the array's end rather than sorting the entire input."
    }
  ],
  "links": [
    "sorting-algorithm",
    "binary-heap",
    "quicksort",
    "data-structure",
    "binary-tree",
    "array-data-structure",
    "pseudocode",
    "loop-invariant",
    "time-complexity",
    "space-complexity",
    "recursion",
    "stable-sorting",
    "cpu-cache",
    "priority-queue",
    "merge-sort",
    "insertion-sort"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}