edited by
477 views
0 votes
0 votes

Consider the following Algorithm is to compare two lists.

compare list ( list 1, list 2)
{
       for num in list 1
           { 
                 if num not in list 2 
                   return 0 ;
                else
                   remove the first occurrence of num from list 2
           }
                 if list 2 is empty 
 return 1;
}


Let n be the length of list 1 and m be the length of list 2. What is the worst – case running time of given algorithm?

  1.   O(n2)
  2.   O(m2)
  3.   O( m log n)
  4.   O(nm)
edited by

1 Answer

0 votes
0 votes
time complexity is O(mn)

n element of list1 is searched m times

Related questions

0 votes
0 votes
2 answers
1
radha gogia asked Jul 31, 2015
11,589 views
If I have two lists of length 2 then no of comparisons in the worst case would be 2 only , since If I have say 10,20 in list A and 5,7 in list B so then on merging 10 is ...
0 votes
0 votes
0 answers
4
usdid asked Jul 2, 2022
504 views
what is the running time of the following iterative algorithm?b) It is possible to talk about the best, average and worst running times for this algorithm. Why? pseudo co...