305 views
0 votes
0 votes
For merging two unsorted list of size m and n into sorted list of size m+n. The time complexity in terms of number of comparison for this is

1 Answer

0 votes
0 votes

In C language, we can use terms list and array interchangeably.

Approach 1 :

  1. Sort both the lists individually using merge sort, so it will take Time complexity =O (mlogm+nlogn)
  1. Then we get two sorted lists now we can apply merge algorithm (also known as combine algorithm in merge sort) and it will take time complexity of O(m+n) in worst case and min(m, n) in best case.
  1. Also, we have to take one extra array of size m+n to store all m+n elements in one array.
  1. Then we will get one sorted list of size m+n

so final T.C= mlogm+nlogn+(m+n)

             T.C=O(mlogm+nlogn)

 

Approach 2 :

  1. Take one extra array and copy all m+n elements in it, which will take time complexity = O(m+n)
  1. now apply merge sort, which will take Time complexity of (m+n)log(m+n)
  1. Then we will get one sorted list of size m+n

so final T.C= (m+n)+(m+n)log(m+n)

              T.C=O((m+n)log(m+n))

also, if the size of both lists is n/2 then

T.C= ($\frac{n}{2}$+$\frac{n}{2}$)+($\frac{n}{2}$+$\frac{n}{2}$)log($\frac{n}{2}$+$\frac{n}{2}$)

which will be asymptotically represented as

T.C=O(nlogn).

 

 

 

edited by

Related questions

240
views
0 answers
0 votes
nihal_chourasiya asked Oct 11, 2023
240 views
0.50280.26040.47920.6305
251
views
1 answers
0 votes
763
views
1 answers
0 votes
DAWID15 asked Dec 9, 2022
763 views
To justify the OPTION B they gave an example of 2*2 matrix. However we can see that row 2 is linearly dependent on row1. Even though the 2nd row looks non-zero it can be made into zero. SO am I wrong or the explanation is wrong?
648
views
0 answers
0 votes
syncronizing asked Feb 28, 2019
648 views
In the given Synchronous counter circuit, initially, all the flip-flop outputs are 0'. it is required to replace FF2 with A-B Flip-Flop. The A-B flip-flop ... answer given is A=Q1' B=Q1'This is how I approached the above question.