• recategorized by
56,668 views
44 44 votes

For merging two sorted lists of sizes $m$ and $n$ into a sorted list of size $m+n$, we require comparisons of

  1. $O(m)$

  2. $O(n)$

  3. $O(m+n)$

  4. $O(\log m + \log n)$

6 Answers

Best answer
74 74 votes

Answer: Option C.

The number of moves are however always $m+n$ so that we can term it as $\Theta(m+n)$. But the number of comparisons varies as per the input. In the best case, the comparisons are $\text{min}(m,n)$ and in worst case they are $m+n-1$.

• edited by
24 24 votes
$\text{A : 10  20  30  90}$

$\text{B : 40  50  60  70}$

Suppose $\text{A.length = m , B.length = n}$

Here $m+n-1$ comparisons are required
So, $O(m+n)$ //this is one of  the worst case comparison

Answer : $C$
• edited by
11 11 votes

If there are 2 arrays like this

A: 10   20    60   90

B:  30   50    70  100

And store the resultent array in C[ ]

while((a[]!=NULL) && (b[]!=NULL))
{
if((a[i]<b[j])||(b[j]==NULL))
{
    c[k++]=a[i++];
}
else
{
    c[k++]=b[j++];
}
}
8 8 votes
@srestha, kindly notice that in question its written two sorted list, not array.

in this case,
best case:- all elements of 2nd list is greater than that of 1st list then minimum comparison is Min(m,n) as it will take only one iteration.for comparison rest will be inserted as it is.
and worst case:- let 2 sorted list be 1,2,3,4 and 1,2,3,4 so here total comparison for merging should be =7(last element need not be compared) so total comparison= m+n-1= O(m+n).
2 2 votes

Example:

A[] = {11,22,33}

B[] = {4,12,43}

Assuming, we are sorting in ascending order.  We compare 11 and 4, smaller is 4. So, insert 4 then 11, then 11 AND 12 , insert 11. then 12 and 22 , insert 12, then 22 and 43 , insert 22, then compare 43 and 33, insert 33 and then finally remaining one 43. So, we required 5 steps for 6 elements.

 

To sort them we will be requiring (m+n-1) comparisons in worst case.

0 0 votes

1).For merging two sorted lists of sizes m and n into a sorted list of size m+n, we require comparisons of O(m+n) in terms of asymptotic notation. This is because during the merging process, we are comparing each element from both lists once, and the total number of elements in the merged list is m+n. Therefore, the number of comparisons is directly proportional to the number of elements in the merged list, and the asymptotic notation for the number of comparisons is O(m+n).      

2).For merging two sorted lists of sizes m and n into a sorted list of size m+n, we require comparisons of m+n-1. The reason is that in order to merge the two sorted lists, we need to compare the first element of each list and select the smaller one. Then we move on to the next element of the selected list and repeat the process. We do this until one of the lists is completely merged. At this point, we only have one list remaining which is already sorted, so we don't need to make any more comparisons. Since we make one comparison for each element in the merged list, we need m+n-1 comparisons.

Answer:
Position:
Show:

Related questions

29 29 votes
2 answers 2 answers
7.5k
7.5k views
Kathleen asked Oct 8, 2014
7,490 views
Merge sort uses:Divide and conquer strategyBacktracking approachHeuristic searchGreedy approach
135 135 votes
10 answers 10 answers
42.2k
42.2k views
go_editor asked Sep 28, 2014
42,221 views
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 sequen...
23 23 votes
2 answers 2 answers
11.8k
11.8k views
Kathleen asked Oct 8, 2014
11,772 views
Consider the following sequence of numbers:$$92, 37, 52, 12, 11, 25$$ Use Bubble sort to arrange the sequence in ascending order. Give the sequence at the end of each of ...
34 34 votes
5 answers 5 answers
15.7k
15.7k views
Kathleen asked Oct 8, 2014
15,672 views
In a virtual memory system the address space specified by the address lines of the CPU must be _____ than the physical memory size and ____ than the secondary storage siz...