edited by
5,468 views
4 4 votes

Given two sorted list of size $m$ and $n$ respectively. The number of comparisons needed the worst case by the merge sort algorithm will be:

  1. $m \times n$
  2. maximum of $m$ and $n$
  3. minimum of $m$ and $n$
  4. $m+n-1$

3 Answers

Best answer
5 5 votes

option d

worst case comparisons in merge sort is o(m+n-1)

selected by
2 2 votes

In the merge sort algorithm, when merging two sorted lists of sizes m and n, the worst-case number of comparisons is given by:

m+n−1

Explanation:

  • The merge operation compares elements from both lists to build a single sorted list.
  • In the worst case, every comparison places one element into the merged list until one of the two lists is empty.
  • Once one list is exhausted, the remaining elements from the other list are directly appended without further comparisons.

So, the worst-case number of comparisons required is m+n−1

Example :

List 1
: [4,10,13]
List 2: [5,7,11]

Process:

  1. Compare 4 and 5: 4 is smaller, so add 4 to the merged list.
    Merged List: [4], Remaining: [10,13] and [5,7,11]

  2. Compare 10 and 5: 5 is smaller, so add 5 to the merged list.
    Merged List: [4,5] Remaining: [10,13] and [7,11]

  3. Compare 10 and 7 : 7 is smaller, so add 7 to the merged list.
    Merged List: [4,5,7]Remaining: [10,13] and [11]

  4. Compare 10 and 11: 10 is smaller, so add 10 to the merged list.
    Merged List: [4,5,7,10] Remaining: [13] and [11]

  5. Compare 13 and 11: 11 is smaller, so add 11 to the merged list.
    Merged List: [4,5,7,10,11] Remaining: [13] and [ ]

  6. Add the remaining 13 to the merged list (no comparison needed).
    Merged List: [4,5,7,10,11,13]

Total Comparisons:

  1. 4 vs 5
  2. 10 vs 5
  3. 10 vs 7
  4. 10 vs 11
  5. 13 vs 11

Total = 5 comparisons
This matches m+n−1= 3+3−1=  5

Answer:
Position:
Show:

Related questions

4 4 votes
2 answers 2 answers
7.1k
7.1k views
Arjun asked Apr 22, 2018
7,058 views
Assume $A$ and $B$ are non-zero positive integers. The following code segment:while(A!=B){ if*(A B) A -= B; else B -= A; } cout<<A; // printing the value of AComputes the...
4 4 votes
2 answers 2 answers
8.8k
8.8k views
Arjun asked Apr 22, 2018
8,758 views
An array $A$ consists of $n$ integers in locations $A[0], A , \ldots A[n-1]$. It is required to shift the elements of the array cyclically to the left by $k$ places, wher...
7 7 votes
1 answers 1 answer
6.5k
6.5k views
Arjun asked Apr 22, 2018
6,546 views
The following paradigm can be used to find the solution of the problem in minimum time:Given a set of non-negative integer and a value $K$, determine if there is a subset...
8 8 votes
2 2 answers
6.0k
6.0k views
Arjun asked Apr 22, 2018
5,956 views
Which of the following is application of Breath First Search on the graph?Finding diameter of the graphFinding bipartite graphBoth (a) and (b)None of the above