GateAI
Searching, Sorting and Hashing

GATE CSE 2014 Set 2 · Q38

NAT2 marksMedium

Time

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 ‾\underline{\hspace{1cm}}.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 358

Searching, Sorting and Hashing

To merge two sorted sequences of sizes mm and nn, the maximum (worst-case) number of comparisons required is:

Comparisons=m+n−1\text{Comparisons} = m + n - 1

To minimize the total number of comparisons, we use the Optimal Merge Pattern (a greedy Huffman-like approach), repeatedly selecting the two sequences of smallest length to merge.

### Step-by-step Merging:

Initial list of lengths: {20,24,30,35,50}\{20, 24, 30, 35, 50\}

  1. First Merge:

    • Choose the two smallest: 2020 and 2424.

    • New sequence size: 20+24=4420 + 24 = 44.

    • Worst-case comparisons: 20+24−1=4320 + 24 - 1 = 43.

    • Remaining lengths: {30,35,44,50}\{30, 35, 44, 50\}.

  2. Second Merge:

    • Choose the two smallest: 3030 and 3535.

    • New sequence size: 30+35=6530 + 35 = 65.

    • Worst-case comparisons: 30+35−1=6430 + 35 - 1 = 64.

    • Remaining lengths: {44,50,65}\{44, 50, 65\}.

  3. Third Merge:

    • Choose the two smallest: 4444 and 5050.

    • New sequence size: 44+50=9444 + 50 = 94.

    • Worst-case comparisons: 44+50−1=9344 + 50 - 1 = 93.

    • Remaining lengths: {65,94}\{65, 94\}.

  4. Fourth Merge:

    • Merge the last two: 6565 and 9494.

    • New sequence size: 65+94=15965 + 94 = 159.

    • Worst-case comparisons: 65+94−1=15865 + 94 - 1 = 158.

### Total Worst-Case Comparisons:

Total Comparisons=43+64+93+158=358\text{Total Comparisons} = 43 + 64 + 93 + 158 = 358

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Searching, Sorting and Hashing previous year question is Q38 of the GATE CSE 2014 Set 2 paper. Find more GATE CSE PYQs on Searching, Sorting and Hashing in Algorithms, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Searching, Sorting and Hashing PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Searching, Sorting and Hashing in GATE CSE

Searching, Sorting and Hashing has 27 GATE CSE PYQs across 20 papers from 2012 to 2026. 4 of them are 2-mark NAT questions like this one, and it is the only one in GATE CSE 2014 Set 2.

How GATE tests Searching, Sorting and Hashing

More from GATE CSE 2014 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 2 · Q38 · NAT

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 1cm.

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.