Question

2. Write the big-O expression to describe the number of operations required for the following two...

2. Write the big-O expression to describe the number of operations required for the following two pieces of code. You need to explain your solution and show your work. (3 points)

code 1:

counter = 0

      for (i = 1; i < n; ++i) {

          for (j = 1; j < i; j++) {

counter++;

}

   }

code 2:

counter = 0;

for (i = 1; i < n; ++i) {

for (j = 1; j < n^3; j++){

                counter++;

           }

}

Homework Answers

Answer #1

CODE 1 :

CODE 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
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code...
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code fragment. Algorithm test int counter, i, j; counter := 0; for (i:= 1; i < n; i*=2) { for (j:= 0; j < i; j++) { counter := counter +1 } } 3)Let f(n) = 2lg 8n + 3 be a function.
Find the big-O, big-Omega of the following functions (show steps please) a) f(n) = 5n^2 +...
Find the big-O, big-Omega of the following functions (show steps please) a) f(n) = 5n^2 + 1 b) f(n)= (nlogn+1)*(n+1)
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the...
APPLIED STATISTICS 2 USE R CODE ! SHOW R CODE! Write a function to calculate the sum of cubes from 1 to n, but skip the multiple of 5. Say, if n=10, the result is 1^3+2^3+3^3+4^3+6^3+7^3+8^3+9^3. The input is the value of n, the output is the summation. (you need if statement to check whether a number is a multiple of 5.In R, a%%b is the remainder for a divided by b. So, we have 10%%5=0) APPLIED STATISTICS 2 USE...
#2. Write a function Triangle which has one parameter n, it will print a Triangle of...
#2. Write a function Triangle which has one parameter n, it will print a Triangle of stars with nxn. By default, n is set to 3. For example, calling Triangle(3) or Triangle() will print the following: # * # * * # * * * #my code down below is not working and I have no idea why. Could someone help? Triangle <- function(n){ for(i in 0:(n-1)){ s <- "" for(k in 0:(n-1)) s <- paste(s, " ", sep =...
Find Big-O Notation 1. def upper_triangle(A, N): # Assume A is a square matrix (stack of...
Find Big-O Notation 1. def upper_triangle(A, N): # Assume A is a square matrix (stack of lists) for i in range(N): A[i][:i] = [0 for _ in range(i)] return A 2. def running_avg(vec, N): sum = 0 avg_list = [] for i in range(N): sum += vec[i] avg = sum / (i + 1) avg_list.append(avg) return avg_list 3. def similarity(vec1, vec2, N): # Assume vec1 and vec2 have length of N s1 = 0 s2 = 0 for i in...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward...
Divide-and-Conquer technique is famous for providing O(n log n) solutions for problems with O(n2) straight forward solutions. One of those problems is the “Maximum Subarray Sum” or “Maximum Value Contiguous Subsequence": Given a sequence of n numbers A[0 ... n-1], give an algorithm for finding a contiguous subsequence A[i ... j], for which the sum of elements in this subsequence is the maximum we can get by choosing different i or j.   For example: the maximum subarray sum for the...
find the: (a) delay required to complete them assuming the operands are n bits long (...
find the: (a) delay required to complete them assuming the operands are n bits long ( write the expression for the delay in terms of n using big O notation). Assume the delay of a standard 2 input NAND/NOR is 1 unit and a 2-input MUX and/or XOR/XNOR is 1 unit. (b) Area required (each gate occupies roughly 1 unit of area) The operation is below: 2. determine the parity of an operand(i.e. determine if the number of "1"'s in...
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
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...
Why it cannot display flop? you need to explain rather than just show me another code...
Why it cannot display flop? you need to explain rather than just show me another code MatLab function b=Matrixvector(A,x) n=length(x); b=zeros(n,1); flop=0; for i=1:n for j=1:n b(i)=b(i)+A(i,j)*x(j); flop=flop+2; end end disp(b); disp(flop);