The Gateway to Computer Science Excellence

+52 votes

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 ____.

+107 votes

Best answer

The optimal algorithm always chooses the smallest sequences for merging.

$20 \ 24 -44, 43$ comparisons

$30 \ 35 -65, 64$ comparisons

$44 \ 50 -94, 93$ comparisons

$65 \ 94 -159, 158$ comparisons

so, totally $43 + 64 + 93 + 158 = 358$ comparisons.

PS: In merge operation we do a comparison of two elements and put one element in the sorted output array. So, every comparison produces one output element. But for the last element we won't need a comparison and we simply insert it to the output array. So for $n$ output elements we need $(n-1)$ comparisons.

$20 \ 24 -44, 43$ comparisons

$30 \ 35 -65, 64$ comparisons

$44 \ 50 -94, 93$ comparisons

$65 \ 94 -159, 158$ comparisons

so, totally $43 + 64 + 93 + 158 = 358$ comparisons.

PS: In merge operation we do a comparison of two elements and put one element in the sorted output array. So, every comparison produces one output element. But for the last element we won't need a comparison and we simply insert it to the output array. So for $n$ output elements we need $(n-1)$ comparisons.

+43

In merge operation we do a comparison of two elements and put one element in the sorted output array. So, every comparison produces one output element. But for the last element we won't need a comparison and we simply insert it to the output array. So for n output elements we need (n-1) comparisons.

+3

But Sir in merge procedure in the two lists we are putting 2 infinities/large numbers at the end.

1 3 5 999 2 4 999

Here after 4 comparisons,the 1 2 3 4 comes down into the final array.lastly to copy 5 into the final array we do compare it 999 in the second list,

1 3 5 999 2 4 999

Here after 4 comparisons,the 1 2 3 4 comes down into the final array.lastly to copy 5 into the final array we do compare it 999 in the second list,

+3

@Arjun sir here two possibilities are there

[A] 20 24 ->44, 43 comparisons

30 35 ->65, 64 comparisons

44 50 ->94, 93 comparisons

65 94 ->159, 158 comparisons

**totally 43 + 64 + 93 + 158 = 358 comparisons**.

[B] 20 24 ->44, 43 comparisons

30 35 ->65, 64 comparisons

44 65 -109, 108comparisons

109 50-159, 158 comparisons

**totally 43+64+108+158 =373 comaprisions**. // shouldn't we keep same order as per question P,Q,R,S,T

and question asked comaprison will be needed in **worst case by optimal algorithm,**

+6

If the algorithm does like that it is no longer optimal. There is no need to keep any order unless explicitly mentioned.

+5

@Arjun Sir how the program will know that it is the last element of list without seeing Infinity on list..

suppose we have two list

1st list contain 2,3

2nd list containn 4,5,6

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

According to u 4 comparison will be needed but how when we compare infinity to 5 then how it will know that next element of 2nd list will be end element of the list

suppose we have two list

1st list contain 2,3

2nd list containn 4,5,6

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

According to u 4 comparison will be needed but how when we compare infinity to 5 then how it will know that next element of 2nd list will be end element of the list

0

@Arjun Sir, Here we do need a min heap to select the two smallest element every time and after merging them we do need to put them backin the min heap. What about the comparisions needed to maintain the min heap? Dont we need to count them?

0

what is the best case number of comparisons performed by optimal algorithm to do the merging in above question?

0

How do we know it is the last element? For that somewhere in the program we will compare (say `loop until i<(n-1)`

) right? Won't that comparison be counted? Please solve my doubt. Thank You.

0

Hello sir, i have never read anywhere to merge two sorted lists which are smaller in size of all the given lists, this looks like more of hamilton path instead of merge procedure. Also i have a doubt when to use two-way merge sort and when to apply merge sort algorithm like explained by @Richa bhardwaj the above comments

It will be good if can have a reference to read from clearing this doubt as nothing is talked about 2-way mergin in Coreman

It will be good if can have a reference to read from clearing this doubt as nothing is talked about 2-way mergin in Coreman

+3

My dear friend , why you are comparing with infinity ?

We know the lower and upper limits, once the one of the two array exhausted, we simply copy the remaining array.

0

this is not the right way of making an optimal tree.Maybe you got a correct answer in this case.For tree to be optimal we have to use greedy method.Hence after selecting (20 24) which equal to be 44 you should select (30 35) as it is another minimum pair.

0

@Prateek kumar Question doesn't say anything about the order the second thing is absolutely incorrect.

0

@Arjun Sir,

The very basic Question. You say that,"** The optimal algorithm always chooses the smallest sequences for merging"..**But,How algorithm will find,the smallest sequences,before merging for next pass?? "

0

@Headshot, how will you come to know about the array has been exhausted, for that u have to do 1 comparison, in worst case there will be one-one element present in both set, here we have to do 1 comparison, who is smaller, after putting the smaller one, we have to do one more comparison to know that one array has been exhausted, then only we can copy the element.

+16 votes

+6 votes

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.5k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.4k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.2k
- Non GATE 1.4k
- Others 1.4k
- Admissions 595
- Exam Queries 573
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 18

50,737 questions

57,297 answers

198,265 comments

104,977 users