ON Its at a position with a loop of N iterations 16 points 3 What is the Big O

On its at a position with a loop of n iterations 16

This preview shows page 1 - 3 out of 3 pages.

O(N) It’s at a position with a loop of N iterations.16 points3. What is the Big-O running time of the following code fragment?Assume lst1 has N items, and lst2 has M items.public static int Count( List<Integer> lst1, List<Integer> lst2){Iterator<Integer> itr1 = lst1.iterator();int count=0;while ( itr1.hasNext() ){Integer x = itr1.next();Iterator<Integer> itr2 = lst2.iterator();while ( itr2.hasNext() )
if ( x.equals( itr2.next()) )count++;}return count;}a.If an ArrayList is passed for lst1 and lst2. Explain your answer.O(N) Its going on a loop but has to tranverse.b.If a LinkedList is passed for lst1 and lst2. Explain your answer.O(N^2) The iterations of N are in a loop.4. What is the Big-O running time of the following code fragment?Assume lst has N items.public static int calc( List<Integer> lst ){int count = 0;int N = lst.size();for ( int i=0; i<N; i++){if (lst.get(i) > 0)sum += lst.get(i);elsesum += lst.get(i) * lst.get(i);}return sum;} 5. Suppose a Java method receives a List<Integer> and reverses the order of the items it contains by removing each item from the front of the list, adding each item to a Stack<Integer>, and then removing the items from the stack and inserting each item to the end of the list. What is the expected Big-O running time if:a.If an ArrayList is passed. Explain your answer.O(N^2) In this occasion every element has to be shifted for N iterations.b.If a LinkedList is passed. Explain your answer.
O(N) Its moving constant amount of pointer at O(1) for each item with N iterations.20 points6. Show each step of converting a+b*c+(d-e) from infix to postfix notation, using the algorithm described in the textbook that uses a stack.1.ABC Stack +*+2.ABC*+DE Stack +(-)3.ABC*+DE-+

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture