Math Fundamentals18 sections · 814 units
Open in Course

Logarithms in Merge Sort (Divide and conquer)

Tree height analysis

Merge sort splits an array of size nn into two halves, recursively sorts each half, then merges them. The recursion tree has height log2(n)\log_2(n).

Why? At each level, you halve the array size. Level 00 has 11 array of size nn. Level 11 has 22 arrays of size n/2n/2. Level 22 has 44 arrays of size n/4n/4. At level kk, you have 2k2^k arrays of size n/2kn / 2^k.

You stop when array size is 11, so n/2k=1n / 2^k = 1, giving k=log2(n)k = \log_2(n). Each level does O(n)O(n) work (merging), so total time is O(nlogn)O(n \log n).