Sort Target
Algorithm
Speed
Normal ~12s · 150 ms/step
Weak heapSort(1993)
▶ Try this Source
O(n log n) ✗ Stable ✓ In-place
🔧 How it works

Builds a weak heap, a relaxed binary tree where each node only needs to dominate its right subtree, tracked with one reverse-bit per node, then merges sub-heaps to extract elements in sorted order.

🎯 Key property

Every merge of two weak heaps costs exactly one comparison, reducing total comparisons for n extractions to the theoretical minimum of n⌈log₂ n⌉, closer to the information-theoretic bound than any standard heap variant.

👁 Watch for
  • Compare each weak-heap merge requires exactly one comparison between a parent and the root of its distinguished child (determined by the reverse-bit)
  • Swap when the merge comparison fails, the parent and distinguished child values are exchanged and the reverse-bit for that node is toggled, maintaining the weak heap invariant
  • End of extraction the maximum is removed from the root; the left and right sub-heaps are re-merged one comparison at a time to restore the weak heap structure
Initial State
This is the array before sorting. Follow Weak heapSort step by step.
Weak Heap
SORTED 5 3 8 1 9 2 7 4
0 / 80 ops ( 0.0%) 0.0%
Sorting is
a spectacle.
Algorithms at work, in a daily vertical feed. Watch, play, challenge — and go deeper when you're ready.
Loading 0%
An unhandled error has occurred. Reload 🗙