{
  "id": "powersort",
  "title": "Powersort",
  "lang": "en",
  "source_lang": "en",
  "summary": "Powersort is an adaptive, stable merge-sorting method that uses run positions to choose a nearly optimal order for merging already sorted subsequences.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "PowerSort"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/powersort/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/powersort/",
      "json": "https://aiwiki.page/zh/powersort.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Powersort is an adaptive merge-sorting method that chooses an efficient order for combining sorted subsequences, called runs. Introduced by J. Ian Munro and Sebastian Wild in 2018, it uses the positions and lengths of consecutive runs to construct a nearly optimal merge tree with little scheduling overhead. Its principal contribution is a merge policy: a rule for deciding which adjacent runs to merge and when. Combined with suitable run detection and stable merging, it provides a stable sorting algorithm that exploits existing order in the input."
    },
    {
      "heading": "Runs and the merge-order problem",
      "text": "A natural mergesort begins by identifying portions of an input that are already sorted. For example, an array might consist of several ascending runs of very different lengths. Descending runs can also be detected and reversed, provided that equal elements are handled in a way that preserves stability.\n\nOnce runs have been found, the algorithm must decide how to combine them. This choice matters because an element may be copied or compared repeatedly as its run participates in successive merges.\n\nIn the standard abstract cost model, merging runs of lengths \\(a\\) and \\(b\\) costs \\(a+b\\). Consider three consecutive runs of lengths \\(4\\), \\(4\\), and \\(8\\):\n\n- Merging the first two runs and then merging the result with the third costs \\(8+16=24\\).\n- Merging the last two runs and then merging the result with the first costs \\(12+16=28\\).\n\nBoth schedules produce the same sorted result, but the first does less total merge work.\n\nFor the usual stable, contiguous-run merging framework, only adjacent runs are combined. Consequently, the merge schedule is an alphabetic binary tree: its leaves represent the original runs in their original left-to-right order. If run \\(i\\) has length \\(r_i\\) and lies at depth \\(d_i\\), the total merge cost is\n\n\\[C=\\sum_{i=1}^{k} r_i d_i.\\]\n\nThe scheduling problem is therefore to build a low-cost ordered binary tree whose heavier leaves tend to have smaller depths.\n\nThis resembles Huffman coding, but ordinary Huffman coding does not preserve a prescribed leaf order. Simply merging the two smallest runs, wherever they occur, is therefore not a valid substitute."
    },
    {
      "heading": "Node powers",
      "text": "Powersort assigns a small integer, called a node power, to each boundary between consecutive runs. This integer indicates the level at which the two runs become separated in a conceptual, balanced subdivision of the entire input interval.\n\nLet the input have length \\(n\\), and let two adjacent runs occupy half-open intervals\n\n\\[[a,b), \\qquad [b,c).\\]\n\nTheir normalized midpoints are\n\n\\[x=\\frac{a+b}{2n}, \\qquad y=\\frac{b+c}{2n}.\\]\n\nTheir boundary power is the smallest nonnegative integer \\(p\\) for which\n\n\\[\\left\\lfloor 2^p x\\right\\rfloor \\ne \\left\\lfloor 2^p y\\right\\rfloor.\\]\n\nSince both midpoints lie in \\([0,1)\\), this first separation occurs at \\(p\\geq 1\\).\n\nGeometrically, repeatedly bisect the interval \\([0,1)\\). The power is the first subdivision level at which the two midpoints fall into different cells:\n\n- A small power marks a relatively high-level division of the input.\n- A large power marks a more local division that should generally be resolved earlier.\n\nEquivalently, the power identifies the first differing position in the midpoints’ binary expansions, using the floor-based definition to handle subdivision boundaries precisely.\n\nThe powers are calculated from the original adjacent runs, rather than repeatedly recomputed from whatever larger runs happen to exist after merging. Implementations can use integer arithmetic and bit operations instead of floating-point midpoint calculations."
    },
    {
      "heading": "Stack-based scheduling",
      "text": "Powersort processes runs from left to right while maintaining pending runs and their boundary powers in a stack.\n\nConceptually, its scheduling procedure is:\n\n- Identify the next run.\n- Compute the power of its boundary with the preceding original run.\n- Merge pending groups whose boundary powers are greater than the new power.\n- Retain the new boundary and continue scanning.\n- After the final run, merge the remaining pending groups.\n\nA newly encountered low-power boundary signals a high-level division. More local merges on its left must therefore be completed before that division is retained for later processing.\n\nThe resulting schedule corresponds to a Cartesian tree over the boundary powers, with smaller powers nearer the root. This connects a simple stack procedure to the intended hierarchical merge tree.\n\nFor runs of lengths \\(4\\), \\(4\\), and \\(8\\), the normalized midpoints are \\(1/8\\), \\(3/8\\), and \\(3/4\\). The two boundary powers are \\(2\\) and \\(1\\). Encountering the second, lower-power boundary causes the first two runs to be merged before their combined run is merged with the final run.\n\nThe increasing-power stack invariant bounds the number of pending entries by \\(O(\\log n)\\). Scheduling requires only linear work in the number of runs, apart from the cost of computing powers."
    },
    {
      "heading": "Adaptivity and complexity",
      "text": "Powersort adapts to the distribution of run lengths, not merely to the total input size.\n\nFor \\(k\\) runs of lengths \\(r_1,\\ldots,r_k\\), define\n\n\\[p_i=\\frac{r_i}{n}, \\qquad H=\\sum_{i=1}^{k}p_i\\log_2\\frac{1}{p_i}.\\]\n\nHere \\(H\\) is the entropy of the run-length distribution. It measures how dispersed the input is among its runs: equal-sized runs have \\(H=\\log_2 k\\), while one dominant run and several small runs can have substantially lower entropy.\n\nIn the binary merge-cost model, \\(nH\\) is a lower bound on the weighted path length of any merge tree, including one without the alphabetic restriction. Powersort achieves a merge cost bounded by\n\n\\[C\\leq nH+2n.\\]\n\nThus its merge cost is within an additive linear term of the optimal alphabetic merge cost. “Nearly optimal” refers to this guarantee; it does not mean that Powersort always selects the exact minimum-cost tree.\n\nWith linear-time run detection and ordinary linear-time merging, its time complexity is\n\n\\[O\\bigl(n(1+H)\\bigr),\\]\n\nwhich implies \\(O(n\\log k)\\) for \\(k\\geq 2\\) and the usual \\(O(n\\log n)\\) worst-case bound. An already sorted input can be recognized in linear time without performing merges.\n\nThe scheduling stack requires \\(O(\\log n)\\) auxiliary space. This is separate from the memory needed by the merge operation: conventional array-based stable implementations can require \\(O(n)\\) temporary storage overall. A small scheduling stack does not make the complete sorting algorithm in-place."
    },
    {
      "heading": "Relationship to Timsort",
      "text": "Powersort and Timsort share a practical approach:\n\n- Detect existing sorted runs.\n- Extend very short runs when useful, commonly with insertion sort.\n- Merge adjacent runs stably.\n- Use efficient merging routines, potentially including galloping searches.\n\nTheir central difference is the merge policy. Classical Timsort uses inequalities involving the lengths of pending runs to decide when to merge. Powersort instead uses boundary powers derived from run positions and lengths.\n\nThis separation allows Powersort scheduling to replace Timsort’s merge-order rules without replacing the surrounding run detection, comparison handling, temporary-buffer management, or merging machinery.\n\nIn CPython 3.11, the implementation underlying Python’s list.sort() and sorted() adopted Powersort’s merge strategy. This was a change to the internal scheduling policy, not to the public sorting interface or its stability guarantee. Descriptions of Python’s sorting implementation as Timsort can therefore refer to a broader implementation whose merge scheduling uses Powersort."
    },
    {
      "heading": "Scope and limitations",
      "text": "Powersort’s mathematical guarantee concerns a particular cost model. Actual execution time also depends on element-comparison costs, data movement, branch behavior, cache effects, and details of the merging implementation.\n\nFor example, merging runs of lengths \\(a\\) and \\(b\\) need not perform exactly \\(a+b\\) comparisons. An ordinary merge uses at most \\(a+b-1\\), and galloping or other shortcuts can reduce comparisons substantially on favorable data. Minimizing abstract merge cost therefore does not necessarily minimize comparisons or elapsed time on every input.\n\nThe guarantee also applies to the runs supplied to the merge policy. Extending short runs changes that run sequence, and the cost of doing so must be included when analyzing the complete sorting implementation.\n\nFinally, Powersort is an approximation to optimal alphabetic merging rather than an exact optimizer. Exact methods can construct minimum-cost ordered merge trees, but require more elaborate scheduling machinery. Powersort trades exact optimality for a compact, left-to-right procedure with low overhead and an additive linear bound on excess merge cost."
    }
  ],
  "links": [
    "merge-sort",
    "stable-sorting",
    "sorting-algorithm",
    "array-data-structure",
    "binary-tree",
    "huffman-coding",
    "stack-data-type",
    "information-entropy",
    "time-complexity",
    "timsort",
    "insertion-sort",
    "python-programming-language",
    "cpu-cache"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}