IITP · Semester ISummaries · Quizzes · Assignments

ECS 5101 — Design and Analysis of Algorithms

Assignment 2 — Merge Sort, Quick Sort, Binary Search

Source: IITP\Sem-1\ECS5101-DAA\assignments\assignment-2.md

Topic: merge step cost · stable sorts · quick-sort pivots and recurrences · binary-search complexity · brute-force subarrays Questions: 20 MCQ Source: class WhatsApp group (Chats/WhatsApp-Cheat-code/chat.md), set "IIT Patna DAA Assignment-2 — Questions + Answers" (13 Oct 2026).

Fidelity note: the export gives only the correct option text, not the distractor options.

Verification: ✅ all 20 re-derived — all correct. Q3 and Q13 are the same question with different option letters; Q6's "median-of-medians" and Q17's "already sorted" both need the caveat noted below.


Quick answer key

Q Answer Q Answer
1 d — O(m+n) 11 b — equal elements may swap
2 c — continue searching left after a hit 12 a — relative order preserved
3 c — O(log n) 13 b — O(log n) (dup of Q3)
4 b — O(n log n) 14 d — 7 merges
5 c — n(n+1)/2 subarrays 15 a — 6 and 7
6 b — median-of-medians pivot 16 c — O(n) auxiliary space
7 c — 2T(n/2) + n 17 c — O(n log n)
8 c — worst-case likelihood 18 c — 6
9 d — less auxiliary memory 19 c — 10
10 b — both ascending and descending 20 c — n+1

Q1. During merge sort, the merge step combines two sorted arrays of sizes m and n. Worst-case time?

✅ d — O(m + n)

Each element is compared at most once and copied exactly once, so the work is linear in the total input size.


Q2. Which modification finds the first occurrence of duplicates?

✅ c — Continue searching the left half after finding a match

Standard binary search returns some occurrence. To get the first, on a hit you record the index and keep searching left. Related: the mirror image (search right) gives the last occurrence; both stay O(log n).


✅ c — O(log n) — halving the range. (Same question reappears as Q13 with the answer letter b.)


Q4. Average-case complexity of quick sort:

✅ b — O(n log n) — balanced partitions on average.


Q5. Brute-force maximum-subarray checks:

✅ c — n(n+1)/2 subarrays

The number of contiguous subarrays is Σ_{k=1..n} (n−k+1) = n(n+1)/2. Related: the brute-force algorithm is therefore Θ(n²) subarrays × O(n) sum each = Θ(n³), unless prefix sums are used.


Q6. Which pivot strategy guarantees worst-case O(n log n) quick sort?

✅ b — Median-of-medians pivot (deterministic linear-time selection, "BFPRT"). Caveat: median-of-medians guarantees O(n) pivot selection ⇒ O(n log n) total. Randomised quick sort gives O(n log n) only with high probability, not a guarantee. Median-of-three (Q8) gives no guarantee at all.


Q7. Choosing the true median as pivot gives the recurrence:

✅ c — 2T(n/2) + n — two halves of size n/2 plus linear partitioning.


Q8. Median-of-three primarily reduces:

✅ c — the likelihood of the worst case

It is a heuristic, not a bound: it makes bad splits rare on typical input, but an adversary can still force quadratic behaviour.


Q9. Quick sort is preferred over merge sort for in-memory sorting because:

✅ d — Less auxiliary memory

Quick sort partitions in place (O(log n) stack); merge sort needs O(n) extra for the merge buffer. Related: merge sort is preferred where stability or linked lists / external data matter.


Q10. Which input produces the worst case if the first element is the pivot?

✅ b — Both ascending and descending sorted

Either extreme is already at one end, so every partition is 0 / n−1 → Θ(n²).


Q11. Quick sort is unstable because:

✅ b — Equal elements may swap positions during the long-distance swaps of partitioning.


Q12. Merge sort is stable because:

✅ a — Equal elements preserve their relative order after merging

The standard merge takes from the left run on ties (≤), which keeps the original order.


✅ b — O(log n)


Q14. Merge sort on 8 elements — how many merge operations?

✅ d — 7

A merge tree with n leaves has n−1 internal nodes; each internal node is one merge. Related: n−1 is independent of the split shape, for any n.


Q15. Merge sort on 13 elements produces subarrays of sizes:

✅ a — 6 and 7 — n/2 floor and ceil.


Q16. Which statement about merge-sort memory is correct?

✅ c — Requires O(n) auxiliary space for merging.


Q17. If the input is already sorted, merge-sort complexity becomes:

✅ c — O(n log n) — it still splits and merges everything; the best case equals the worst case. Related: this is the big contrast with insertion sort, which is O(n) on sorted input — the reason hybrid sorts (Timsort) use insertion sort on small runs.


Q18. Merge-sort recursion depth for 64 elements:

✅ c — 6 — log₂64.


Q19. Worst-case comparisons in binary search for 1024 elements?

✅ c — 10

The search range shrinks 1024 → 511 → 255 → 127 → 63 → 31 → 15 → 7 → 3 → 1 → 0: that is 10 comparisons, i.e. log₂1024 = 10. Related: a 1,023-element array also needs 10 (one short of the next power of two); a 2,048-element array would need 11.


Q20. Number of unsuccessful searches possible in a sorted array of n elements?

✅ c — n + 1

A key can fall into one of n+1 gaps (before the first, between any two, after the last). Related: this is the same count as the number of external nodes in a binary-search decision tree.