I need some help formulating a proof that shows that two lists of size n can be merged in 2n - 1 comparisons. I understand the essence behind it, but have difficulty proving it mathematically.
I understand that the worst case occurs when merging two lists of size n, the two lists only contain elements that are interleaved. For example:
List 1: [1, 3, 5]
List 2: [2, 4, 6]
Starting off with the first element in List 1:
1 < 2 ? yes. Output = [1]
3 < 2 ? no. Output = [1, 2]
3 < 4 ? yes. Output = [1, 2, 3]
5 < 4 ? no. Output = [1, 2, 3, 4]
6 < 5 ? no. Output = [1, 2, 3, 4, 5]
Append last element: Output = [1, 2, 3, 4, 5, 6]
Any tips on how I can start developing a mathematical proof for this?
The best thing is to explicitly write down a corresponding algorithm, such as
Given sorted input lists $L_1$, $L_2$ of lengths $n,m$, produce a merged (sorted) list $O$.
Then use the usual tools to analize an algorithm that step 3 is executed exactly $m+n-1$ times if $m,n>0$ (and $0$ times if one of $m,n$ is zero).