Multiway Powersort
The same node-power merge policy as plain Powersort — only the tree is k-ary instead of
binary. Boundary powers are the depth of the shallowest node of a virtual, perfectly balanced
k-way tree (i.e. base‑k digits of the run midpoints instead of bits).
The run stack is now only weakly increasing, so several runs can share a power; when a smaller
power arrives, the whole block of equal-power runs is merged in one go with a single
k-way merge (2 to k runs at a time). Fewer, wider merges mean less memory
traffic — the win for heavy objects. Pick k below and step through it.
Press play to run Multiway Powersort.
Note: for clarity this demo uses ascending runs only and skips the
minRunLen extension — real implementations also reverse descending runs and pad short
runs by insertion sort. The finale here follows the plain policy (merge the top equal-power block
repeatedly); production k-way code additionally reshuffles the leftover stack so that almost every
finale merge is a full k-way merge.
What changed vs. plain Powersort? Only two things. (1) A boundary's power is the number
of leading identical base-k digits of the two run midpoints (plain Powersort uses base 2), so
powers index a virtual k-ary tree. (2) The stack invariant relaxes from strictly to
weakly increasing: the pop condition becomes top.power > p (strict) instead of
>=, so up to k−1 runs can pile up at the same power and are then closed
together by a single k-way merge. Everything else — run detection, the near-optimal
merge order, stability — is identical. See
Multiway Powersort (ALENEX 2023).