Analyze the following code fragments and write down the Big-O estimates of the following code fragments. Provide a concise explanation how you got your answer.
c. for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
cout << (j + k) << endl;
}
d. while (n > 1)
{
k += n *3;
n = n / 2;
}
e. int temp = n;
for (int j = 0; j < n; j++)
{
while (temp > 1)
temp = temp / 2;
}
c.
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++;
cout << (j + k) << endl;
}
Answer: O(n2)
Explanation:
1. The given method has nested for loop.
2. The inner loop executes n times and the outer loop also executes n times, Hence the total complexity isO(n2).
d.
while (n > 1)
{
k += n *3;
n = n / 2;
}
Answer: O(n/2)
Explanation:
Complexity is considered to be the worst case implementation, Let us consider the n = 2; Then the while loop is executed 1 time;
Let the n value is 8; then the loop is executed 3 times; As the value of n increses the number times the loop execution decreases, but when compared to the worst case scenario, the maximum times the statements executed is n/2;
So the complexity is O(n/2).
e.
int temp = n;
for (int j = 0; j < n; j++)
{
while (temp > 1)
temp = temp / 2;
}
Answer: O(n)
Explanation:
The inner executes n/2 as explained in the part e. similarly the outer loop executes n times. So the complexity is O(n, n/2). The maximum of two is n. Therefore the complexity is O(n).
Analyze the following code fragments and write down the Big-O estimates of the following code fragments....
Analyze the following code fragments and write down the Big-O estimates of the following code fragments. Provide a concise explanation how you got your answer. c. for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) cout << (j + k) << endl; } d. while (n > 1) { k += n *3; n = n / 2; } e. int temp = n; for (int j...
(10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...
Show how to get the big-Oh for the following code: void CountSort (int A[N], int range) { // assume 0 <= A[i] < range for any element A[i] int *pi = new int[range]; for ( int i = 0; i < N; i++ ) pi[A[i]]++; for ( int j = 0; j < range; j++ ) for ( int k = 1; k <= pi[j]; k++ ) cout << j << endl; }
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
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 25; j > 16; j -= 3) cout << setw(5) << j; Answer: 2. int sum = 0; for (int k = -2; k <= 2; k++) sum = 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...
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 ++)...
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 } }
**C++ Question** For all of the following, determine the total operation count and then the Big-O of the given code segments: a. for (int j = 0; j < n; j++) for (int k = 0; k < j; k++) sum++; b. for (int i = 0; i < q*q; i++) for (int j = 0; j < i; j++) sum++; For all of the following, just determine the Big-O of the given code segments: c. for (int i =...
I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...