Do iterative and recursive versions of an algorithm have the same time complexity?

后端 未结 5 1836
感情败类
感情败类 2020-12-05 20:06

Say, for example, the iterative and recursive versions of the Fibonacci series. Do they have the same time complexity?

5条回答
  •  天涯浪人
    2020-12-05 20:28

    If you take some recursive algorithm you can convert it to iterative by storing all function local variables in an array, effectively simulating stack on heap. If done like this there's no difference between iterative and recursive.

    Note that there are (at least) two recursive Fibonacci algorithms, so for the example to be exact you need to specify which recursive algorithm you're talking about.

提交回复
热议问题