# Least number of comparisons to merge any two lists in increasing order into one list in increasing order.

This is part of self-study.

What is the least number of comparisons needed to merge any two lists in increasing order into one list in increasing order when the number of elements in the two lists are

a) 1, 4? b) 2, 4? c) 3, 4? d) 4, 4?

For part a (1, 4), the book I'm using suggests that, to add one element to a list of n elements, binary search can be used to search for the position in which to insert the element, which would take $\left \lceil log_2 n \right \rceil$ comparisons. In this case (two lists with 1 and 4 elements, respectively), the number of comparisons made by the binary search would be $\left \lceil log_2 4 \right \rceil = 2$. Then, since there are five possible insertion points, one more comparison would be needed to find where to put the element (as was pointed out below in the comments). So, in total, 3 comparisons are needed.

But I'm not sure what to do with the other cases. For part b, to insert the two elements $a_1$ and $a_2$ into the list $\{b_1,b_2,b_3,b_4\}$, I could also use binary search. For inserting $a_1$, it would take 3 comparisons, like in the first case. Now I have to insert $a_2$ into the newly formed 5 element list, which contains $a_1$. I could do another binary search, but, this time, I don't need to do the binary search for the whole 5 elements list, because, since $a_2$ is greater than $a_1$ (because both lists are ordered), I can exclude $a_1$ and all elements previous to $a_1$ from the search, because $a_2$ will be certainly greater than them all.

So, I think I should consider the worst case, in which $a_1$ will have been inserted in the beginning of the list (that is, the 5 elements list will be $\{a_1,b_1,b_2,b_3,b_4\}$. In this case, the second binary search will be done for a list of 4 elements, so it will take 3 comparisons. Thus, for part b, two lists, with 2 and 4 elements, respectively, can be merged with a total of 6 comparisons in the worst case.

Notice that I am considering the worst case here because the question asks for the least number of comparisons for any two lists of 2 and 4 elements.

As an example for part b, consider the lists $\{1, 2\}$ and $\{3, 4, 5, 6\}$. The first binary search will insert 1 in the beginning of the second list, giving the list $L = \{1, 3, 4, 5, 6\}$. The second binary search will be done in the sublist $\{3, 4, 5, 6\}$ of the list L and will insert 2 in the beginning of this sublist: $\{2, 3, 4, 5, 6\}$. Thus, the final list is $\{1, 2, 3, 4, 5, 6\}$.

However, since the book I'm using states that the upper bound for merging two lists with m and n elements is $m + n - 1$ comparisons, it seems that I can't use this binary search technique here, because the upper bound for merging the 2 and 4 element lists would be 5 comparisons, which is less than the 6 comparisons needed by the technique above.

Is there some way to use fewer comparisons?

joriki: Thank you for the answer. I understand why $m+n-1$ is the upper bound. But, in the original post, I found a strategy for case a (where the list are of 1 and 4 elements) that gives a number of comparisons which is lower than $m+n-1$ for any two lists of sizes 1 and 4; I couldn't find such strategy for the (2, 4), (3, 4) and (4, 4) cases. Maybe, for each one of these three other cases, there is no strategy that guarantees a number of comparisons smaller than (m+n-1) to merge any two lists. But I have no clue how to prove it...

Any hints?

• I fail to see how 2 comparisons are enough to insert any element in an ordered list of 4. It seems that the best strategy yields 3 comparisons in the worst case.
– Did
Jan 6 '12 at 22:37
• @Didier, for instance, consider adding "2" in the list {1,3,4,5}. The binary search algorithm will divide the list in half {1,3 | 4,5}, and compare "2" with the last element of the first half ("3"). Because 2 < 3, the algorithm will chose the first half: {1,3}. Then, this list will be split in half: {1 | 3}. The algorithm will now compare "2" with the "1". Because 2 > 1, 2 will be inserted after "1". 2 comparisons were made to find where to insert "2".
– favq
Jan 6 '12 at 23:02
• @user1131904: but to take your example, inserting "9" into (2,4,6,8), 9 would have to be be compared with 4, then with 6 and finally with 8. Didier´s point is that two comparisons can only help you choose between four possible insertion points ($2^2$) and with a list of four elements there are five possible insertion points. Jan 6 '12 at 23:29
• @Henry, you are right, I hadn't realized that, besides the comparisons of the binary search, 1 more comparison would be necessary for deciding where to insert the element. So, in my example, besides the 2 comparisons, 1 more comparison is needed to see that 2 < 3.
– favq
Jan 6 '12 at 23:44
• @Didier: I edited the original question to include the extra comparison that was missing. This modified the original reasoning a little.
– favq
Jan 7 '12 at 10:52

The bound $m+n-1$ is achieved by a linear merge: Start with a pointer at the beginning of each list, compare the two elements pointed to, add the smaller one to the merged list and increase that pointer. In case you have to compare the last two elements, you've made $(m-1)+(n-1)$ comparisons to get to that point, so the worst case takes $m+n-1$ operations.

[Edit in response to the edit in the question:]

For simplicity, let's assume that all values are different so that all comparisons have a binary result. (Since this is about worst-case behaviour, we can make arbitrary assumptions about the input.) With $k$ binary results, you can distinguish at most $2^k$ different cases. The result of merging two sorted lists of lengths $m$ and $n$ can be specified by the positions of the $m$ elements of the one list in the merged list, and there are $\binom{m+n}m$ different arrangements of these positions. Thus you need at least $\left\lceil\log_2\binom{m+n}m\right\rceil$ comparisons. That solves cases c) and d), since

$$\left\lceil\log_2\binom{3+4}3\right\rceil=\left\lceil\log_235\right\rceil=6=3+4-1$$

and

$$\left\lceil\log_2\binom{4+4}4\right\rceil=\left\lceil\log_270\right\rceil=7=4+4-1\;.$$

Case b) needs a bit more work, since

$$\left\lceil\log_2\binom{2+4}2\right\rceil=\left\lceil\log_215\right\rceil=4\lt2+4-1\;.$$

To allow the merge to be done with $4$ comparisons, the first comparison would have to divide the $15$ possibilities into two groups of $8$ and $7$; otherwise one of the groups would have more than $8$ possibilities and couldn't be resolved with the remaining $3$ comparisons. Due to the symmetry between greater and lesser, we only have to consider comparisons involving the first element of the two-element list as first comparisons. Comparing the first element of the two-element list with one of the four elements of the other list divides the $15$ possibilities into groups of $10/5$, $6/9$, $3/12$ and $1/14$, respectively. Since there's a group with more than $8$ elements in each case, it's impossible to do the merge with $4$ comparisons in all cases.

• Thank you for this answer; I've updated the original post.
– favq
Apr 9 '12 at 21:56
• @user1131904: OK, I've edited the answer in response. Apr 9 '12 at 22:51