Tree Sort

Builds a binary search tree and traverses it.

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

How it works

Builds a binary search tree and traverses it.

Implementation

function treeSort(arr) {
  class Node { constructor(v) { this.val=v; this.left=this.right=null; } }
  function insert(root, v) {
    if (!root) return new Node(v);
    if (v < root.val) root.left = insert(root.left, v);
    else root.right = insert(root.right, v);
    return root;
  }
  let root = null;
  for (const v of arr) root = insert(root, v);
  // In-order traversal fills sorted array
}