-1

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

jrok
  • 54,456
  • 9
  • 109
  • 141
  • This is not C++. Don't just add random language tags. – jrok Oct 15 '14 at 06:20
  • You need to solve it like you solve any mathematical problem. There is no universal recipe. It is hard to answer your specific question because of non-standard notation you are using. – n. m. could be an AI Oct 15 '14 at 06:26

1 Answers1

0

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

user2550754
  • 884
  • 8
  • 15