Ternary Heap Sort

Heap sort built on a ternary (3-ary) heap: every node has up to three children, so the heap is shallower than a binary heap and each sift-down visits fewer levels. Builds a max-heap in place, then repeatedly swaps the root to the end and restores the heap over the shrinking prefix.

Best O(n log n) Avg O(n log n) Worst O(n log n) Space O(1) Stable No In-place Yes Comparison-based

How it works

Heap sort built on a ternary (3-ary) heap: every node has up to three children, so the heap is shallower than a binary heap and each sift-down visits fewer levels. Builds a max-heap in place, then repeatedly swaps the root to the end and restores the heap over the shrinking prefix.

Implementation

function ternaryHeapSort(arr, stats) {
  const n = arr.length;
  function siftDown(size, root) {
    while (true) {
      let largest = root;
      const c1 = 3 * root + 1;
      const c2 = 3 * root + 2;
      const c3 = 3 * root + 3;
      if (c1 < size) {
        compare(c1, largest);
        if (arr[c1] > arr[largest]) largest = c1;
      }
      if (c2 < size) {
        compare(c2, largest);
        if (arr[c2] > arr[largest]) largest = c2;
      }
      if (c3 < size) {
        compare(c3, largest);
        if (arr[c3] > arr[largest]) largest = c3;
      }
      if (largest === root) break;
      [arr[root], arr[largest]] = [arr[largest], arr[root]];
      swap(root, largest);
      root = largest;
    }
  }
  for (let i = Math.floor((n - 2) / 3); i >= 0; i--) {
    siftDown(n, i);
  }
  for (let end = n - 1; end > 0; end--) {
    [arr[0], arr[end]] = [arr[end], arr[0]];
    markSorted(end);
    swap(0, end);
    siftDown(end, 0);
    checkpoint(n - end, n);
  }
  markSorted(0);
}