LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

How does merge-sort split a sequence whose length is not a power of two, and does that change its running time?

It cuts at $\lfloor n/2 \rfloor$, so the two halves differ by at most one element. The tree is no longer perfect, but every level still halves the sizes, the height is $\lceil \log_2 n \rceil$, and the running time stays $O(n \log n)$.

Example with $n = 7$: 7 → 3 + 4 → (1 + 2) and (2 + 2) → single elements. Some leaves sit one level higher than others, which changes nothing in the analysis: every level still handles at most $n$ elements, and there are $\lceil \log_2 7 \rceil = 3$ levels.

Accepting only lengths $2^k$ is a common simplification for a first merge-sort over arrays: both halves then have exactly $n/2$ elements, so one length serves for both new arrays, and the merge knows that its output is exactly twice as long as one half. A general version computes the two lengths separately, $\lfloor n/2 \rfloor$ and $n - \lfloor n/2 \rfloor$, and sizes the output as the sum of both, which works for any $n$.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026