GATE DA · Programming, Data Structures and Algorithms
Divide and conquer: merge sort and quicksort
Divide and conquer with recurrences, merge sort and quicksort: the merge and partition steps, comparison counts, best, average and worst cases, space, stability and how pivot choice decides quicksort's running time.
Both algorithms are recursive; tracing them means following the call tree.
Logarithms and geometric series
Recurrences are solved by summing work over levels.
Concept notes
1. Divide and conquer and recurrences
A divide and conquer algorithm (1) divides the input into smaller subproblems, (2) conquers them recursively, and (3) combines their answers. Its running time is a recurrence: if it makes calls on inputs of size and spends dividing and combining, then .
Recursion tree method: level has nodes of size . Add the work per level and sum over about levels.
Master theorem (compare with ):
Case
Condition
1
2
3
and regular
Common results:
→ (merge sort, balanced quicksort).
→ (binary search).
→ (quicksort worst case).
for a constant → , since each level still does at most work and there are levels.
2. Merge sort
Merge sort splits the array into two halves, sorts each recursively, and merges the two sorted halves.
def merge(a, b):
i = j = 0
out = []
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i]); i += 1
else:
out.append(b[j]); j += 1
return out + a[i:] + b[j:]
Merge cost: merging sorted lists of sizes and takes at least comparisons (one list runs out first) and at most (they interleave to the end).
Running time: , so in the best, average and worst case. For , the worst-case number of comparisons is (17 for ).
Properties:
Stable, because ties take from the left half (<=).
Not in-place on arrays: the merge needs extra space; the recursion depth is .
Works well on linked lists (merge needs no extra array) and for external sorting of data on disk.
Bottom-up merge sort merges runs of size 1, 2, 4, … without recursion; after pass every block of size is sorted.
Merge sort on [8, 3, 5, 1, 7, 2, 6, 4]: split down to single items, then merge back up
3. Quicksort and partitioning
Quicksort picks a pivot, partitions the array so that items pivot come before it and larger items after it, then sorts the two sides recursively. No combine step is needed.
Lomuto partition (pivot = last item):
def partition(a, lo, hi):
p = a[hi]
i = lo - 1
for j in range(lo, hi):
if a[j] <= p:
i += 1
a[i], a[j] = a[j], a[i]
a[i + 1], a[hi] = a[hi], a[i + 1]
return i + 1
It makes exactly comparisons on items and puts the pivot in its final sorted position. Hoare partition scans from both ends and swaps out-of-place pairs; it usually makes fewer swaps but does not place the pivot at the split point.
Quicksort is in-place (only the recursion stack) and not stable: a partition swap can carry an item past an equal one.
After a partition, the pivot never moves again, so after partition calls at least items are in their final positions. Tracing questions often ask for the array after the first partition: follow the given code exactly, because Lomuto and Hoare give different arrays.
4. Quicksort: best, average and worst case
Quicksort's time depends on how evenly the pivot splits the array.
Best case: the pivot is always the median. .
Worst case: the pivot is always the smallest or largest item, leaving sides of size and . , which gives comparisons: . With first- or last-item pivots this happens on already sorted or reverse-sorted input, and on arrays of all-equal items with Lomuto.
Average case (random input, or random pivot): , about comparisons.
Any constant-ratio split, even 1 : 99, still gives .
Choosing the pivot at random, or as the median of three, makes the worst case very unlikely but not impossible. Using the true median (found in linear time) gives a guaranteed , at a large constant cost.
Merge sort
Quicksort
Best
Average
Worst
Extra space
average stack, worst
Stable
Yes
No
In-place
No
Yes
In practice quicksort is often faster because it works in place with good cache behaviour.
Formulas to remember
Divide and conquer recurrence
Merge sort
Comparisons to merge sizes m and k
Merge sort worst-case comparisonsFor a power of 2
Quicksort worst case
Quicksort average comparisons
Common mistakes
Saying merge sort is in the best case; it always does work.
Assuming an unbalanced but constant-ratio split makes quicksort quadratic; only splits like and do.
Mixing Lomuto and Hoare partitions when tracing; they give different arrays.
Calling quicksort stable, or merge sort in-place on arrays.
Using instead of as the most comparisons in a merge.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Counting comparisons in a merge
numerical
Merge [2, 5, 9, 14] and [3, 4, 10, 15, 20] with the merge function above. How many comparisons are made?
Step 1: Compare the fronts until one list empties.
Try it on paper first.
Example 2: Tracing a Lomuto partition
numerical
Apply the Lomuto partition (pivot = last item) to [7, 2, 9, 4, 3, 8, 5]. Give the array afterwards and the pivot's index.
Step 1: Set the pivot and scan j = 0, 1.
Try it on paper first.
Example 3: Worst case of quicksort
numerical
Quicksort with the last item as pivot (Lomuto) is run on the sorted array [1, 2, 3, 4, 5, 6]. How many comparisons does it make?
Step 1: What does the first partition do?
Try it on paper first.
Example 4: An unbalanced split is still n log n
conceptual
Suppose every partition splits the array in the ratio 1 : 9. Is quicksort still ?
Step 1: Draw the recursion tree.
Try it on paper first.
Previous year questions
2 official GATE DA questions on this topic, newest first.
10 questions: up to 2 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.