Awesome q2a theme
0 votes
15 views
while merging two sorted array of size m,n respectively why total number of comparision is m+n-1?.

suppose

1st list  contain 2,3

2nd list containn 4,5,6

insert infinity in first list at end and infinity in second list at end.

1st  comparison of (2,4)  ----------->2 will come in Final Array

2nd comparison of (3,4) ----------->3 will come in Final Array

3rd comparison of (infinity, 4)----------->4 will come in Final Array

4th comparison of (infinity,5)----------->5 will come in Final Array

5th comparison of (infinity,6)----------->6 will come in Final Array

so according to above procedure there will be total of m+n comparision i.e here 5.
ago in Algorithms by (6 points) | 15 views

1 Answer

0 votes
After you insert $5$ into the result array, there is only one element ($6$) left. Why do you need another comparison to insert it ? That is where the $-1$ comes into picture.

Technically, in this example - it will take less than $m+n-1$ comparisons. After you insert $3$, left array is empty. And since right array is already sorted - you can insert into the result array directly without further comparisons.

$m+n-1$ is the upper bound.
ago by (1.2k points)
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
Top Users Jan 2020
  1. shashin

    1163 Points

  2. Vimal Patel

    307 Points

  3. Deepakk Poonia (Dee)

    305 Points

  4. Debapaul

    237 Points

  5. Satbir

    192 Points

  6. SuvasishDutta

    137 Points

  7. Pratyush Priyam Kuan

    118 Points

  8. tp21

    108 Points

  9. DukeThunders

    96 Points

  10. pranay562

    95 Points

Monthly Top User and those within 60% of his/her points will get a share of monthly revenue of GO subject to a minimum payout of Rs. 500. Current monthly budget for Top Users is Rs. 75.
2,989 questions
1,509 answers
8,936 comments
89,814 users