Skip to content

Merge Sort

Task: Sort this array in ascending order

382743398210SORTED

What you are watching

Merge sort on seven values, drawn as the recursion it is. The array is split in half, each half is split again, and the splitting stops when a piece holds a single value, which is sorted by definition. The pieces then come back up: two sorted runs are merged by comparing their front values and taking the smaller, which is where the ordering is actually created. Nothing sorts on the way down and nothing is compared until the way back up. Each level of the tree walks past every value once, and halving the array down to single values takes log n levels, so the sort costs n log n on every input there is.