Reqflow
Sorting·Intermediate

Merge Sort

Time O(n log n)Space O(n)
·

Merge sort splits the array in half recursively until each piece is a single element (which is trivially sorted), then merges those pieces back together in sorted order. The merge step walks both halves simultaneously and always picks the smaller element. Because the work is split log n ways and each level does O(n) merge work, the total is O(n log n) — faster than the quadratic sorts and guaranteed regardless of input order.

Example: Sort 6 numbers using divide and conquer.

When to use this

See also

← All algorithms