Question

1.) Answer the following questions based on the following pseudocode for the function “foo”: Algorithm 1:...

1.) Answer the following questions based on the following pseudocode for the function “foo”:

Algorithm 1: foo

mystery()

foreach i ←1 to n do

mystery()

foreach j ←1 to i do

(A) Determine the exact number of times mystery() is called in terms of n.

(B) Assume the mystery function is in O(log(n) ∗ n ^2 ). Determine the complexity of “foo” in O-notation.

if i ≤ j then

mystery()

Homework Answers

Answer #1
A)
Algorithm and number of times each line runs:
Algorithm 1: foo
mystery()       -> 1 time
foreach i ←1 to n do    -> n+1 times
mystery()               -> n times
foreach j ←1 to i do    -> n*(i+1) times

Number of times mystery() is called = 1 + n


----------------------------
B)
Time complexity 
= 1 + n+1 + n + n*(i+1)
Max value for i is n
>= 1 + n+1 + n + n*(n+1)
>= 2 + 2n + n*(n+1)
>= 2 + 2n + n^2 + n
>= 2 + 3n + n^2
>= n^2
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Consider the following recursive algorithm. Algorithm Mystery(n) if n=1 then Execute Task A; // Requires Θ(1)...
Consider the following recursive algorithm. Algorithm Mystery(n) if n=1 then Execute Task A; // Requires Θ(1) operations else Mystery(n/3); Mystery(n/3); Mystery(n/3); Execute Task B;  //Requires 2n operations end if Let C(n) be the complexity of Mystery(n). Use the method of backward substitution to determine C(n) in three steps. a) Write the recurrence relation for C(n) including the initial condition. b) Write at least two substitution steps for C(n) and identify the pattern. c) Determine the complexity class of the algorithm in...
Below is C code and Python code for an algorithm. C code: void foo( int n,...
Below is C code and Python code for an algorithm. C code: void foo( int n, int A, int B, int C ) { if( n==1 ) { printf("%d to %d\n",A,B); return; } foo( n-1, A, C, B ); printf("%d to %d\n",A,B); foo( n-1, B, C, A ); Python code: def foo(n , A, B, C): if n==1: print A, "to", B return foo(n-1, A, C, B) print A, "to", B foo(n-1, B, C, A) Let Hn be the number...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
Logic and Design Programming In Syntax Pseudocode Algorithm Workbench 1. Design a simple function named timesDozen...
Logic and Design Programming In Syntax Pseudocode Algorithm Workbench 1. Design a simple function named timesDozen that accepts an Integer argument into a parameter named nbr. When the function is called, it should return the value of its argument multiplied times 12. 2. Assume that a program has two String variables named alpha and bravo. Write - a pseudocode statement that assigns an all uppercase version of alpha to the bravo variable. Book name is Starting Out with Programming Logic...
This is python questions 1.An algorithm to solve this computation problem must be written using a...
This is python questions 1.An algorithm to solve this computation problem must be written using a programming language. a.True b.False 2.O(N) is called __________ complexity. a.Constant b.Linear c.Quadraic d.Exponential 3. A fast sorting algorithm is a sorting algorithm that has an average runtime complexity of __________ or better. a.O(N2) b.O(N1.5) c.O(NlogN) d.None of these 4.A(n)_________ describes a sequence of steps to solve a computational problem or perform a calculation. a.permutation b.statement c,algorithm d.formula
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n...
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n 1)What does this algorithm compute? 2) Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed. 3) How does this algorithm compare with the non-recusive algorithm for computing thius function in terms of time efficeincy and space effeciency?
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() /...
public int linearSearch2(ArrayList<Integer> pList, Integer pKey) { for (int i = 0; i <= pList.size() / 2; ++i) { if (pList.get(i).equals(pKey)) { return i; } else if (pList.get(pList.size() - 1 - i).equals(pKey)) { return pList.size() - 1 - i; } } return -1; } ----- Q1)Which function f(n) counts how many times the key operation(s) is(are) performed as a function of the list size n when pKey is not in pList (the worst case behavior of linearSearch2() occurs when pKey...
Answer the questions based on the program given below. a) Write the function prototype of fnFunny...
Answer the questions based on the program given below. a) Write the function prototype of fnFunny function. b) Write the output produced by the above program #include <stdio.h> int main (void){       unsigned short i =4;       unsigned long j = 6 ;       short k = 2 ;       k = fnFunny ( i , j );       printf("i = %hu, j = %lu, k = %hi\n", i, j, k);       return 0; } short fnFunny (unsigned short i,...