Cartesian Tree Sort

Min priority queue sort.

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

How it works

Min priority queue sort.

Implementation

function cartesianTreeSort(arr) {
  class Node { constructor(v) { this.val=v; this.left=this.right=null; } }
  let root = null;
  const stack = [];
  for (const v of arr) {
    let node = new Node(v), last = null;
    while (stack.length && stack.at(-1).val > v)
      last = stack.pop();
    node.left = last;
    if (stack.length) stack.at(-1).right = node;
    else root = node;
    stack.push(node);
  }
  // In-order traversal of root fills sorted array
}