Question

In: Computer Science

Please calculate the Big-O cost and explain how: 1) void recurX(long x) {           ...

Please calculate the Big-O cost and explain how:

1)

void recurX(long x) {
           if(x==0) System.out.print("*");  
else {
recurX(x/2);
recurX(x/2);
}

}

2)

void recurY(long y) {
           if(n==0) return;
           for(long i=0;i<y;i++)
System.out.print("*");
recurY(n/2);
       }

Solutions

Expert Solution

1)

void recurX(long x) {
if(x==0) System.out.print("*");
else {
recurX(x/2);
recurX(x/2);
}
}

Answer: O(n)
explanation:

Recurrence relation for running time :
T(n) = 2T(n/2)+1 // 2 recursive calls of size n/2, 1 for if condition
T(1) = 1//base case
solving:
T(n) = 2T(n/2)+1
= 2^2T(n/2^2)+2 +1
= 2^3T(n/2^3)+2^2 +2 +1
,
,
,
= 2^kT(n/2^k)+2^k-1 + ,,,,+2^2 + 2+1
let 2^k = n, k= logn
T(n) = 2^kT(n/n) +2^k-1 + ,,,,+2^2 + 2+1 = 2^k +2^k-1 + ,,,,+2^2 + 2+1 = 2^k+1 = 2*2^k = 2n = O(n)


2)

void recurY(long y) {
if(n==0) return;
for(long i=0;i<y;i++)
System.out.print("*");
recurY(n/2);
}
Answer: O(n)
Recurrence relation for running time :
T(n) = T(n/2) + n //recursive call of size n/2 , n for loop
T(1)=1//base case
solving:
T(n) = T(n/2) + n
=T(n/2^2) + n/2 + n
=T(n/2^3) +n/2^2 + n/2 + n
,
,
,
=T(n/2^k)+n/2^k-1+,,,, +n/2^2 + n/2 + n
let 2^k =n, k = logn
=T(n/n)+n/2^k-1+,,,, +n/2^2 + n/2 + n
=1 + n[ 1/2^k-1 + ,,,, + 1/2 + 1]
= O(n)


Related Solutions

Can someone please explain the big O [ Data Structures ]. Also, is there a shortcut...
Can someone please explain the big O [ Data Structures ]. Also, is there a shortcut (a hint) on how I can know which one is it? Like O(1), O(n)... Thank you!
Please explain what the Big O Notation is in plain English. Please include the logic behind...
Please explain what the Big O Notation is in plain English. Please include the logic behind it and calculations if necessary. I have trouble understanding it, I will be taking a test soon so I will need an easier way to remember it. I use Python btw.
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f(x, y) is O(g(x, y)) means that there exist constants C, k1, and k2 such that|f(x, y)|≤C|g(x, y)|whenever x > k1 and y > k2] 2. Find a div b and a mod b when: (a) a = 30303, b = 333 (b) a = −765432, b = 3827 3. Convert...
how to calculate the cost of death for a country please explain with an example clearly
how to calculate the cost of death for a country please explain with an example clearly
(a) Explain the difference between big-O and big-theta, and give examples of each to show the...
(a) Explain the difference between big-O and big-theta, and give examples of each to show the difference. (b) How can we say that two functions have the same asymptotic complexity, using big-theta notation? (c) Rank the following functions in order of increasing complexity (rate of growth), and indicate which functions have the same asymptotic complexity. x2; x log(x); 2x; log(x) + 7; 92x2 + 57x + 3921; 4x; 27x2 + 8x3; 22x+5; log(x42); 3x + 12.
Please explain Cost of Capital and what is the formula to calculate.
Please explain Cost of Capital and what is the formula to calculate.
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
Section 1031 Exchanges- Please explain o What is like kind property in tax accounting? o How...
Section 1031 Exchanges- Please explain o What is like kind property in tax accounting? o How long does one have to complete a like-kind exchange? o Requirements for it to be a tax-free exchange?
1. How is big data analytics applied in accounting? You must explain in detail how big...
1. How is big data analytics applied in accounting? You must explain in detail how big data analytics applied are used. Give and elaborate three (3) examples. (3 Different accounting examples.) [600 words] 2. Why is big data analytics applied in those examples? What are the benefits to the company or organisation? [100 words] * Please give in-depth answers with supporting references (journal articles, company reports etc.), appropriate with the word limits.*
For the differential equation x′′ + (o.1)(1 − x2)x′ + x = 0; x(0) = 1,...
For the differential equation x′′ + (o.1)(1 − x2)x′ + x = 0; x(0) = 1, x′(0) = 0. (a) Rewrite it as a system of first order differential equations in preparation to solve with the vectorized version of a numerical approximation technique. (b) Use the vectorized Euler method with h = 0.2 to plot out an approximate solution for t = 0 to t = 10. (c) Plot the points to the approximated solution (make a scatter plot), make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT