Model answer
At each level there are two half-size subproblems plus linear merging: T(n)=2T(n/2)+Θ(n). The recursion has log₂n levels and each level performs Θ(n) work, so T(n)=Θ(n log n).
Use the key terms, then explain the reasoning.
Merge sort recursively divides the input, sorts both halves, and linearly merges the sorted results.
Merge sort recursively reduces the problem to single-element sorted ranges. Merging compares the front unconsumed values of two sorted ranges, so each merge level processes every element once.
There are log n levels and O(n) work per level, giving Θ(n log n) in every case. Arrays require an auxiliary buffer; linked lists can merge by relinking nodes.
Merge two sorted halves
| Operation | Best | Average | Worst | Extra space |
|---|---|---|---|---|
| Sort | O(n log n) | O(n log n) | O(n log n) | O(n) |
Taking from the left on equality preserves the original order of equal values.
Open a prompt when you are ready to check your answer.