Merge sort and quicksort are both divide and conquer — which phase does each spend its per-level work in?
answer
- one sort works before recursing, one after
- which one needs a pivot placed?
- count comparisons in merge sort's split step
- when quicksort's calls return, what's left to do?
- pre-order work versus post-order work
basics
~20 sQuicksort spends in the divide step: partitioning is a linear pass, and when the halves return there is nothing to combine. Merge sort splits by index in constant time and pays its linear pass when combining.
solid answer
~40 sBoth fit the same split-solve-combine template, but they load opposite ends of it. Quicksort's divide step partitions the range around a pivot — one linear pass — and the pivot lands in its final position, so when the two recursive calls return the range is already sorted and the combine step is empty. Merge sort's divide step is just an index calculation, O(1) bookkeeping with no comparisons at all; every comparison happens on the way back up, in the linear merge that interleaves two sorted halves. Both therefore do O(n) work per level and, with balanced splits, about log n levels, which is where the shared O(n log n) comes from. The difference that matters is *when* the work happens: quicksort's is pre-order, merge sort's is post-order.
go deeper
Be ready to say in one breath that quicksort does its linear work while splitting and merge sort does it while combining. Naming which phase is empty for quicksort is the whole answer at this level.
Explain why the placement follows from the mechanism: the pivot lands in its final position, so nothing is left to combine, while two independently sorted halves carry no information about their interleaving.
Connect placement to behaviour under real data: a positional split is data-independent, a value-based split is not, so only one of the two has a shape that hostile input can distort.
Own the framing that both sorts share an average bound but not a risk profile, and be able to say which structural property you would rely on when a pipeline must meet a bound rather than an average.
### One template, three instances Divide and conquer has three phases: **divide** the input into subproblems, **conquer** them by recursing, and **combine** the sub-results into the answer. Every instance pays for all three phases, but how much cost sits in each phase is what tells the instances apart — and it is the distribution, not the total, that an interviewer is probing when they ask you to compare merge sort and quicksort. Use a concrete workload: a year of readings from a sensor fleet, each tagged with a timestamp, and the job is to put roughly half a million of them in chronological order. ### Quicksort — heavy divide, empty combine Quicksort picks a pivot value and rearranges the range so that everything ordering before the pivot sits to its left and everything after sits to its right. That rearrangement touches every element in the range once, so the divide step costs O(n) for a range of size n. Crucially, the pivot ends up at the index it will occupy in the final sorted order and never moves again. Now recurse on the left part and on the right part. When those two calls return, both sides are sorted, the pivot between them is already correct, and the whole range is in order. There is nothing left to do — the function simply returns. Quicksort's combine step is *empty*. All of its comparison work happened **before** the recursive calls. ### Merge sort — trivial divide, heavy combine Merge sort divides by position: compute the midpoint of the index range and hand each half to a recursive call. No values are compared, nothing is moved; the divide step is O(1) arithmetic per call. When the two calls return, you hold two sorted sequences and no information about how they interleave, so the combine step has to walk both of them and produce one ordered output — a linear pass for a range of size n. All of merge sort's comparison work happens **after** the recursive calls. ### The picture side by side | algorithm | divide cost | combine cost | work per level | comparisons happen | |---|---|---|---|---| | quicksort | O(n) partition | none | O(n) | before recursing (pre-order) | | merge sort | O(1) midpoint | O(n) merge | O(n) | after recursing (post-order) | | binary search | O(1) compare | none | O(1) | at the split itself | Binary search is included because it shows the template's third shape: it recurses into only one of the two halves, so it does neither an expensive divide nor any combine, and its per-level cost is constant rather than linear. ### Why the placement is more than trivia The two sorts share an average bound of O(n log n) precisely because each does O(n) work per level over about log n levels of balanced splits. But *where* the work sits determines how each behaves when the assumption breaks: - Merge sort splits **by position**. The split ratio is 50/50 no matter what the data looks like, so the number of levels is fixed by n alone and the algorithm's shape is data-independent. - Quicksort splits **by value**. Where the boundary falls depends on the pivot and the data, so the shape of the recursion is data-dependent. Its per-level cost stays O(n); what degrades on hostile input is the *number of levels*. That is why the follow-up to this question is almost always "and what if the input is already sorted?" — a question about quicksort's split ratio, not about its partition cost. A second consequence: because quicksort finishes a range's work before recursing, it sorts the array in place with no result to carry back up. Because merge sort produces its answer on the way up, it needs somewhere to build the merged output. That structural fact — pre-order work returns nothing, post-order work returns something — is the root of most of the practical differences between the two. ### The claim to state carefully "Quicksort has no merge step" is correct. "Quicksort has no linear step" is not — the partition *is* the linear step, just on the other side of the recursion. And O(n) per level times log n levels is a *balanced-split* statement for both algorithms: it holds unconditionally for merge sort and only on well-behaved splits for quicksort.
- If quicksort's combine step is empty, why is it still O(n log n) on average?Because the emptiness of the combine step doesn't remove the linear work — it moves it. Each level of recursion partitions every element of the level's ranges exactly once, so a level costs O(n), and balanced splits give about log n levels. The total is the same O(n log n); it is simply paid on the way down instead of on the way up.
- Does merge sort's divide step do any comparisons at all?None. It computes a midpoint index and issues two recursive calls, which is constant-time bookkeeping per call regardless of the values involved. That is exactly why merge sort's recursion shape never depends on the data: the split is positional, so the two halves are the same size whatever the timestamps look like.
Quicksort tidies the room before sending the children to their corners; merge sort sends them off immediately and does all the tidying when they come back.
saying these in an interview costs you the question
- Claims quicksort has an expensive merge step after recursing
- Says merge sort's split step is where the comparisons happen
- Assumes every divide-and-conquer algorithm needs a linear combine
- Cannot say what happens after quicksort's two recursive calls return