Question

In: Computer Science

3. Given Pseudocode: Algorithm2(n): y = 0 for i = 10 to n + 10 do...

3. Given Pseudocode:

Algorithm2(n):

y = 0

for i = 10 to n + 10 do

for k = i to n + 10 do

y = y + k

return y

a. What's the output of Algorithm2(n) as a function of n?

b. What's the running time of Algorithm2(n) as a function of n?

c. Using Theta notation, describe the running time of Algorithm2(n) in the form of Θ(nC) for some C.

d. Using Theta notation, describe the output of Algorithm2(n) in the form of Θ(nC) for some C.

Solutions

Expert Solution

a) the output is as follows

y= y+k

it will do

[10+11+12+13+14+....+n+10] + [11+12+13+14+.....+n+10]+ [12+13+14+..+n+10] +...+n+10

which is

n/2[10+n+10] + (n-1)/2[11 + n+10] + (n-2)/2[12+n+10]+....+n+10 { becasue sum of AP is total terms/2[first term+last term}

this is the output in terms of n

b) the runtime complexity is

the k loops is runnning from i to n+10

and i is from 10 to n+10

so k going 10 to n+10

then 11 to n+10

then 12 to n+10

so number of iterations of k are

n+10-10 + n+10-11 +.....+n+10-n-10

which n+n-1+n-2+n-3+n-4+...+1

which is the sum of first n natural numbers which is equal to n(n+1)/2

c) since running time is n*(n+1)/2

we get time complexity is O(n*n) { because we neglect the constants and small terms}

d) we represented the output in solution a

we know in theta complexity we take only highest degree term and neglect the constants

so the output will be represented as O(n^2)

Hope you like the solution

please do thumbs up!


Related Solutions

4. Given Pseudocode: Algorithm3(n): y = 0 for i = 1 to n do y =...
4. Given Pseudocode: Algorithm3(n): y = 0 for i = 1 to n do y = 1 while y < i2 do y=2y return y a. What's the output of Algorithm3(n) as a function of n? b. What's the running time of Algorithm3(n) as a function of n? Represent this as a summation. c. Prove the running time to be O(n log n).
Solve the following recurrence relation for the given initial conditions. y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10 y(0) = 2; y(1) = 0
Solve the following recurrence relation for the given initial conditions.y(n+2) - 0.3y(n + 1) + 0.02y(n) = 10        y(0) = 2;    y(1) = 0
Solve the given initial-value problem. y'' + y = 0,    y(π/3) = 0,    y'(π/3) = 4
Solve the given initial-value problem. y'' + y = 0,    y(π/3) = 0,    y'(π/3) = 4
Find the solution of the given initial value problem: y′′′+y′=sec(t), y(0)=6, y′(0)=7, y′′(0)=−3
Find the solution of the given initial value problem: y′′′+y′=sec(t), y(0)=6, y′(0)=7, y′′(0)=−3
solve for the second order initial value problem= y"-6y+8y=0 y(0)=3, y'(0)=10
solve for the second order initial value problem= y"-6y+8y=0 y(0)=3, y'(0)=10
in the problem given (3 - x)y" + x2y' + (1 + x)y = 0 ,...
in the problem given (3 - x)y" + x2y' + (1 + x)y = 0 , x0=0. find the recurrence relation and the series solution given y(0) = -6 and y'(0)= 2
For the following causal difference equation, given that y[-1] = 0, y[-2] = 1, and x[n]...
For the following causal difference equation, given that y[-1] = 0, y[-2] = 1, and x[n] = u[n], solve using z-Transforms. (Hint: convert to delay operator form, find the z-Transform, use PFE to find the inverse z-Transform) 4y[n + 2] + 4y[n + 1] + 2y[n] = x[n + 1]
How do you translate this pseudocode go regular code in C++ int iMin = 0,i =...
How do you translate this pseudocode go regular code in C++ int iMin = 0,i = 0; for(j = 0; j < n - 1; j++) int iMin = j; for(i = j + 1; i < n; i++) if(a[i] < a[iMin]) iMin = i; if(iMin != j) swap(a[i], a[iMin]);
y''-5y'+4y=et y(0)=3 y'(0)=1/3
y''-5y'+4y=et y(0)=3 y'(0)=1/3
Given the differential equation y''+y'+2y=0,  y(0)=−1,  y'(0)=2y′′+y′+2y=0,  y(0)=-1,  y′(0)=2 Apply the Laplace Transform and solve for Y(s)=L{y}Y(s)=L{y}. You do not...
Given the differential equation y''+y'+2y=0,  y(0)=−1,  y'(0)=2y′′+y′+2y=0,  y(0)=-1,  y′(0)=2 Apply the Laplace Transform and solve for Y(s)=L{y}Y(s)=L{y}. You do not need to actually find the solution to the differential equation.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT