I have an algorithm, I am not sure about time and space complexity. ,
for (i = 1 to n )
do
begin
R(xi) = random(o, xi, r)
i++
end
What is the time and space complexity in above algorithm and why?
Thanks
I have an algorithm, I am not sure about time and space complexity. ,
for (i = 1 to n )
do
begin
R(xi) = random(o, xi, r)
i++
end
What is the time and space complexity in above algorithm and why?
Thanks
Space and time complexity is in proportional to the size of input. Suppose there is an input array of size n
and you are processing each element once, then time complexity is O(n)
. If you use any data structure to store and process intermediate results, say another array of size n
, then space complexity will be O(n)
. There are many notations for complexity and different complexities such as log n, n, n^2, n^3, n!
.
For your code time complexity is O(n)
and space complexity depends on whether you have defined(O(n)
) R
or it is passed(O(1)
) as parameter.
Refer here for more Analysis of algorithms