For merging two sorted lists of sizes m and n into a sorted list of size m +…

2018

For merging two sorted lists of sizes m and n into a sorted list of size m + n, how many comparisons are required?

  1. A.

    O(m)

  2. B.

    O(n)

  3. C.

    O(log m + log n)

  4. D.

    O(m + n)

Attempted by 635 students.

Show answer & explanation

Correct answer: D

To merge two sorted lists of sizes m and n, we use a two-pointer technique where each element is compared at most once. Step 1: Initialize two pointers, one for each list, starting at the first element. Step 2: Compare the elements at the current pointers and select the smaller one to add to the merged list. Step 3: Move the pointer of the list from which the element was selected. Step 4: Repeat until all elements from both lists are processed. Since each element is compared at most once, the total number of comparisons is at most m + n. Therefore, the time complexity of merging is O(m + n).

Explore the full course: Up Lt Grade Assistant Teacher 2025