Calculate the running time for the following code fragment. Show your work.
for (i:= 0; i < n; i++)
{
for (j:=1; j < n; j*=2)
{
counter := counter +1
}
}
Generally one loop nested in another will have O(n²).
in this cae j=j*2 is the pattern for j for firs for loop wil have to expcute n time where as inner for loop will be executed half than n as j =j*2.
So i will be (n*n)/2
Time complexity will be O(n^2/2) .
Let me know if you have doubts.
Calculate the running time for the following code fragment. Show your work. for (i:= 0; i...
Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for i in range(n): for j in range(n): test= test + i *j Q-2: Given3 the following code fragment what is its Big-O running time? for i in range(n): test=test+1 for j in range(n): test= test - 2 Q-3: Given the following code fragment what is its Big-O running time? i = n while i > 0: k=2+2 i...
c++ What is the worst-case complexity of the following code fragment: for(i = 0; i < N; i++) { for(j = 0; j < N; j++) { sequence of constant time statements } } for(i = N; i > 0; i--) { sequence of constant time statements }
For the following program fragment give a Big-O analysis of the running time. Briefly explain your answer: int t = 0; for(int i=1; i <= n; i++) for(int j=1; j <= i*i; j++) if(j % i == 0) t++; What I have so far, O(1) + O(n) + O(n2) + O(1) + O(1) Drop Low order terms: O(n) + O(n2) And I believe the final answer to be O(n3), but not sure if just drop the O(n) or...
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) int *a = new int [10]; // new is O(1) int size = 10; for (int i = 0; i < n; i ++) { if (i == size) { int newsize = 3*size/2; int *b = new int [newsize]; // new is O(1) for (int j = 0; j < size; j ++)...
1. Give the running time of the following procedures, using -notation. Show your work. procedure P. (n) S=0; for i n to 2n do for j = 5i to 5i+12 do ses+i-j. procedure ps(n) 560; for i« 5n to 6n do for 15 to i do for k j to i do ses+i-j. procedure pe(n) S=0; for i1 to 5n do begin j4i ; while j<i' do begin 5 5 +i-j; je 5; end end
Exercises • Determine running time for the following code fragments: (a) a = b + c; d = a + e; (b) sum = 0; for (i=0; i<3; i++) for (j=0; j<n; j++) sum++; (c) sum=0; for (i=0; i<n<n; i++) sum++; (d) for (i=0; i < n-1; i++) for (j=i+1; j <n; j++) { tmp = A[i][j]; A[i][j] = A[j] [i]; A[j][i] = tmp; (e) sum = 0; for (i=1; i<=n; i++) for (j=1; j<=n; j+=2) sum++;
Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. Assume k is a constant given by the problem. for (i=1; i<=n; i++) p = pow(i,k); // p = i to the power of k for (j=1; j<=p; j++) Some O(1) work end for end for
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) for(int i=n-1; i >=0; i--){ for(int k=0; k < i*n; k++){ // do something that takes O(1) time } }
What is the Big-Oh order of the following code fragment? The fragment is parametrized on the variable N. Assume that you are measuring the number of times j is decremented. public static void sort(Comparable[] a) { int N-a.length; for (int i = 1; i < N;i++) { for (int j = i; j > && less(a[5], a[j-1]); j--) //measure j -- exch(a, j, j-1); O(nlogn) O O(n^2) Q(n) Does not exist.