Question
Suppose P, Q, R, S, T are sorted sequences having lengths 20, 24, 30, 35, 50 respectively. They are to be merged into a single sequence by merging together two sequences at a time. The number of comparisons that will be needed in the worst case by the optimal algorithm for doing this is _____.
Official answer and solution
- Official answer
- 358
- From
- Official GATE 2014 answer key
Solution
Construct the optimal 2-way merge tree using Huffman's greedy algorithm and calculate worst-case comparisons at each merge step.
-
Comparison Cost for Merging Two Lists:
Merging two sorted lists of lengths and requires at most comparisons in the worst case. -
Optimal Merge Pattern (Greedy Selection):
Always merge the two shortest available lists at each step.
Initial list sizes: .-
Step 1: Merge 20 and 24:
Available sizes: .
-
Step 2: Merge 30 and 35:
Available sizes: .
-
Step 3: Merge 44 and 50:
Available sizes: .
-
Step 4: Merge 65 and 94:
-
-
Total Worst-Case Comparisons:
358
Calculating record movement cost (m + n) instead of comparison cost (m + n - 1), which would mistakenly give 362.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2014 Computer Science and Information Technology (CS) Session 2
- Question
- 48 of 65, printed as CS Q.38
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.