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
- Insert every value into a binary search tree (smaller values go left, larger go right).
- Read the tree in-order (left, node, right): that visits the values in ascending order.
- Average O(n log n); O(n²) if the tree degenerates (for example, already-sorted input).
Watch it run
Loading animation…