Produce an equation that asymptotically describes the following algorithms runtime: define algorithm_1(input): x = 2 y = 2 x = ((y + z) * 80)/4 print x, y, z define algorithm_2(input): x = 1 y = 5 loop from x to size(input) * 4: y = y +5 print x, y define algorithm_3(input): x = user_input() y = user_input() z = 0 loop i = 0 to size(input): if x
Please solve and show all steps. This is for C++ problem.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Use previous solution to Derive Big Oh for the
- We need to provide the asymptotic equation that will describe the complexity of the provided code snippets.
1. First code ::
-> In this we have 3 operations and all the operation take constant time.
We are considering (input = n).
Lets take time taken for ::
- x = 2 , y = 2 -> a (Constant time)
- x = ((y + z) * 80) /4 -> b (Constant time)
- print x ,y, z -> c (Constant time)
Here all are constant operations. So,
T(n) = a + b + c , which can also be written as ::
T(n) = k (Constant)
2. Second code ::
-> In this code we have a loop and so the operation will take linear time.
We are considering (input = n).
Lets take time taken for ::
- x = 1 , y = 5 -> a (Constant time)
- loop, (y = y + 5) , (print x, y) -> 4*n (Linear time)
T(n) = a + 4*n or,
T(n) = a + b*n ( a, b = constant)
3. Third code ::
-> In this code we have a loop and so the operation will take linear time.
We are considering (input = n).
Lets take time taken for ::
- x and y input -> e (Constant time)
- z = 0 -> f (constant time)
- loop -> g* n (linear time)
- if x < y -> h* n (linear time)
- Other operations -> k (linear time)
T(n) = (e + f) + (g+h) *n + k
- We assume :: (e + f) = a , (g+h) = b
T(n) = a + bn + k ( a, b, n are constants)