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

1. State and explain the definition of big-O. 2. Explain why we use big-O to compare...
1. State and explain the definition of big-O. 2. Explain why we use big-O to compare algorithms. 3. Explain why binary search runs in O(log n) time. 4. Under what conditions is it possible to sort a list in less than O(nlog n) time? 5. List and explain the worst-case and average-case running times for each Vector method below: (a) insert(iterator here, Object item) (b) insertAtHead (c) insertAtTail (aka push back) (d) get(iterator here) (e) get(index i) (f) remove(iterator here)...
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.
Prove the following problems. 1 .Use the definition of big O to explain why or why...
Prove the following problems. 1 .Use the definition of big O to explain why or why not 3/(x2 + 3x) = O(3). Prove your answer. 2 .Use the definition of  Θ to explain why or why not sqrt(2 + sqrt(3x)) =  Θ(x1/4). Prove your answer. 3 .Explain why 5x2 =  Θ(2x2) is true and  5x2 ~ 2x2 is not true.
Could you please explain this step by step? I do not understand how to calculate long...
Could you please explain this step by step? I do not understand how to calculate long end duration with the different prices? What is long end duration? Calculate the long end duration for a bond with a current price of £109.5, if its price drops to £108.5 when the yield curve steepens at the long end by 50bps; and if its price increases to £112.2 when the yield curve flattens at the long end by 50bps. Interpret the value of...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT