Skip to content
StackDecode
SortingAnimated

Tree Sort

Insert into a binary search tree, then read in-order.

Time

O(n log n) avg, O(n²) worst

Extra space

O(n)

Use it for

Streaming data where a tree is wanted anyway.

How it works

  1. Insert every value into a binary search tree (smaller values go left, larger go right).
  2. Read the tree in-order (left, node, right): that visits the values in ascending order.
  3. Average O(n log n); O(n²) if the tree degenerates (for example, already-sorted input).

Watch it run

Loading animation…