Question

What is the tilde time complexity for the following code fragments: 1) int sum = 0;...

What is the tilde time complexity for the following code fragments:

1)

int sum = 0;
for (i = n; i > 0; i /= 2)
for (j = 0; j < i; j++)
sum++;

2)

int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j+=2)
sum++;

Homework Answers

Answer #1
1)
Number of times each line run at max is
int sum = 0;                -> 1
for (i = n; i > 0; i /= 2)  -> log(n)
for (j = 0; j < i; j++)     -> (i+1)log(n)
sum++;                      -> ilog(n)

Max value for i is n
Time complexity
= Theta(1 + log(n)(i+1+i))
= Theta(1 + log(n)(n+1+n))
= Theta(1 + log(n)(2n+1))
= Theta(nlog(n))

=================================

2)
int sum = 0;                    -> 1
for (int i = 0; i < n; i++)     -> n+1
for (int j = 0; j < i; j+=2)    -> (1+(i/2))(n+1)
sum++;                          -> (i/2)(n+1)

Max value for i is n
Time complexity
= Theta(1 + n+1 + (1+(i/2))(n+1) + (i/2)(n+1))
= Theta(1 + n+1 + (1+(n/2))(n+1) + (n/2)(n+1))
= Theta(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
What is the time complexity of the following code? (in big-O notaion) sum = 0 count...
What is the time complexity of the following code? (in big-O notaion) sum = 0 count = 0 var1 = 0 for: i=1 to n{ sum = sum+i for j=1 to [(3i+2i)(5n)(2i)]{ var1 = sum+count} } for m = i to j*j{ sum = sum*sum var2 = sum - (count+3)}
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum...
1. public int add100(int[] array) { if (array.length < 100) { return 0; } int sum = 0; for (int i = 0; i < 100; i++) { sum += array[i]; } return sum; } 2. int sum = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) { sum++; sum++; } } for (int i = 0; i < n; i += 2) { sum++; } 3. nt...
Answer the following questions: 1. Given the code segments below with n as the problem size,...
Answer the following questions: 1. Given the code segments below with n as the problem size, answer the following questions: //Code Segment 1 (Consider n as a power of 3) int sum = 0; for(int i = 1; i <= n; i = i*3) sum++;​​​​​​​// statement1 //Code Segment 2: (Consider both possibilities for x) if(x < 5){ for(int i = 1; i <= n; i++)    for(int k = 1; k <= i; k++)    sum++;​​​​​// statement2 } else{ for(int...
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 ------...
Given the following code snippet, what is this program calculating? for (int i = 0; i...
Given the following code snippet, what is this program calculating? for (int i = 0; i < n; i++) { sum += arr[i]; } sum /= n;
(Java) For the following code segment, match the different values of the empty line with the...
(Java) For the following code segment, match the different values of the empty line with the resulting Big O complexity of the algorithm. int n = int k = 0; for (int i=0; i <= n; i++){ int j = i; while (j > 0) { // ___MISSING CODE___ k++; } } j = 0; j--; j /= 2; j = j * 2; Options for each are: O(n^3)            O(n * log n)            an...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of...
Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. for ( int i = n; i > 0; i /= 2 ) { for ( int j = 1; j < n; j += 2 ) { for ( int k = 0; k < n; k += 2 ) { ... // constant number of operations } } }
int main() { int i, j, sum = 0; for(i=0;i<3;i++) { for(j=0;j<4;j++) { if(j > 2)...
int main() { int i, j, sum = 0; for(i=0;i<3;i++) { for(j=0;j<4;j++) { if(j > 2) break; sum++; } } } What is value of sum, explain.
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return...
What is the time complexity ? int fact(int n) { if( n==1) {return 1;} n=n*fact(n-1); return n; }
   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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT