TAOCP 5.3.2 Exercise 18
Let the two sorted sequences have lengths $m$ and $n$.
Section 5.3.2: Minimum-Comparison Merging
Exercise 18. [M40] [M40] Study the average number of comparisons used by binary merge.
Verified: yes
Solve time: 2m16s
Let the two sorted sequences have lengths $m$ and $n$. Assume the keys are distinct and all $\binom{m+n}{m}$ interleavings of the two sequences are equally likely. This induces a uniform random sequence of $m$ symbols $A$ and $n$ symbols $B$, and binary merge proceeds by comparing heads until one sequence is exhausted.
Step 1: Relating comparisons to the exhaustion point
During merging, each comparison produces exactly one output element. Comparisons continue until the first moment when one of the two input sequences becomes empty. After that point, the remaining elements of the other sequence are appended without further comparisons.
Let $C$ be the number of comparisons and let $L$ be the number of elements output after the first exhaustion (with no comparisons). Since every element is output either via a comparison or in the final tail, we have
$$ C + L = m + n, \quad \text{so} \quad C = m + n - L. $$
Hence,
$$ \mathbb{E}[C] = m + n - \mathbb{E}[L]. $$
Step 2: Interpreting $L$ via the last occurrence of the minority symbol
Assume $m \le n$. The argument for $n < m$ is symmetric.
The process stops comparisons when the last $A$ is consumed. After the last $A$ appears in the random interleaving, only $B$'s remain. Therefore $L$ is exactly the number of $B$'s occurring after the last $A$.
Let $N = m+n$. The $m$ positions of the $A$'s form a uniformly random $m$-subset of ${1,\dots,N}$. Let $X$ be the maximum position of an $A$. Then
$$ L = N - X. $$
So
$$ \mathbb{E}[L] = N - \mathbb{E}[X]. $$
Step 3: Expected maximum position of a random subset
For a uniformly random $m$-subset of ${1,\dots,N}$, the expected maximum is the classical order statistic:
$$ \mathbb{E}[X] = \frac{m(N+1)}{m+1}. $$
Substituting $N = m+n$,
$$ \mathbb{E}[X] = \frac{m(m+n+1)}{m+1}. $$
Thus,
$$ \mathbb{E}[L] = (m+n) - \frac{m(m+n+1)}{m+1}. $$
Step 4: Expected number of comparisons
Now compute:
$$ \mathbb{E}[C] = (m+n) - \mathbb{E}[L] = (m+n) - \left((m+n) - \frac{m(m+n+1)}{m+1}\right) = \frac{m(m+n+1)}{m+1}. $$
Step 5: Symmetric case
If $n \le m$, the same argument with roles reversed gives
$$ \mathbb{E}[C] = \frac{n(m+n+1)}{n+1}. $$
Final result
$$ \boxed{ \mathbb{E}[C] = \begin{cases} \dfrac{m(m+n+1)}{m+1}, & m \le n,\[6pt] \dfrac{n(m+n+1)}{n+1}, & n \le m. \end{cases} } $$
This gives the exact average number of comparisons used by binary merge under a uniform random interleaving model.