Question

**C++ Question** For all of the following, determine the total operation count and then the Big-O...

**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 = 0; i < n; i++)
for (int j = 0; j < i*i; j++)
for (int k = 0; k < j; k++)
sum++;


d.
for (int i = 0; i < p; i++)
for (int j = 0; j < i*i; j++)
for (int k = 0; k < i; k++)
sum++;

e.
for (int i = 0; i < n; i++)
{
Circ arr[n];
arr[i].setRadius(i);
}

f.
for (int i = 0; i < n; i++)
{
int k = i;
while (k > 1)
{
sum++;
k = k / 2;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.
for (int j = 0; j < n; j++) // It runs for n times so its complexity is O(n)
for (int k = 0; k < j; k++) // It runs for n(n+1)/2 times so its complexity is O(n2)
sum++; // It runs for n(n+1)/2 times so its complexity is O(n2)

Therefore its complexity is O(n2)

b.

for (int i = 0; i < q*q; i++) // It runs for n times so its complexity is O(n2)
for (int j = 0; j < i; j++) // It runs for n2(n2-1)/2 times so its complexity is O(n4)
sum++;    // It runs for n2(n2-1)/2 times so its complexity is O(n4)

Therefore its complexity is O(n4)

c.
for (int i = 0; i < n; i++)   // Its complexity is O(n)
for (int j = 0; j < i*i; j++) // Its complexity is O(n3)
for (int k = 0; k < j; k++) // Its complexity is O(n6)
sum++; // Its complexity is O(n6)

Therefore its complexity is O(n6)

d.
for (int i = 0; i < p; i++)   // Its complexity is O(n)
for (int j = 0; j < i*i; j++)   // Its complexity is O(n3)
for (int k = 0; k < i; k++) // Its complexity is O(n6)
sum++; // Its complexity is O(n6)

Therefore its complexity is O(n6)

According to HomeworkLib guidelines i have to solve first four bits only.

Add a comment
Know the answer?
Add Answer to:
**C++ Question** For all of the following, determine the total operation count and then the Big-O...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • 1. Determine the appropriate big-o expression for each of the following functions, and put your answer...

    1. Determine the appropriate big-o expression for each of the following functions, and put your answer in the table we have provided in section 2-1 of ps5_parti. We've included the answer for the first function. (Note: We're using the “ symbol to represent exponentiation.) a (n) = 5n + 1 b. b(n) = 5 - 10n - n^2 o c(n) = 4n + 2log (n) d. e. d(n) = 6nlog (n) + n^2 e(n) = 2n^2 + 3n^3 - 7n...

  • Show your work Count the number of operations and the big-O time complexity in the worst-case...

    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...

  • (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity...

    (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...

  • For each of the below code snippets, identify the bounding function (the big O) of the...

    For each of the below code snippets, identify the bounding function (the big O) of the number of times the indicated line is run (justify your answer briefly): int i = 1: while (i < n) { printf ("Insert difficult work here!") i = i + i: } for(i = 0: i < n: i++) { for (j = 0: j < n: j++) { for (k = 0: k < n: k++) { if(i==j && j==k) arr[i] [j] [k]...

  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • What is the big o cost of this method? int count = 0; int i =...

    What is the big o cost of this method? int count = 0; int i = 1; while(i < n){ for (j = 1; j < n*n; j *= n) { count++; } i *= 2; } System.out.println(count);

  • This is java. I need help with my computer science class. Question 11 Assume that you...

    This is java. I need help with my computer science class. Question 11 Assume that you have an array of integers named arr. The following program segment is intended to sum arr [0]through arr[n−1], where n = arr.length:          sum = 0;          i = 0;          n = arr.length;          while (i != n)          {             i++;             sum += arr[i];          } In order for this segment to perform as intended, which of the following modifications, if any, should be made? 1.No modification is necessary...

  • Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT