0

Recursion and iteration who is better

Considering the speed, number of steps and the complexity which system or writing code is ideal or usually suitable in most cases

16th Apr 2017, 9:12 AM
Samuel Akosa
Samuel Akosa - avatar
3 ответов
+ 11
if you can avoid recursion - do, however as @merkafter said, trees and graphs are mostly handled by recursion and there is no escaping that. One tip - the moment you start writing a recursion, write your exit condition, then do everything else.
16th Apr 2017, 11:41 AM
Nikolay Nachev
Nikolay Nachev - avatar
+ 5
Iteration-based solutions are always faster but there are problems which are much easier to solve with recursion.
16th Apr 2017, 11:49 AM
Tamás Barta
Tamás Barta - avatar
0
It depends on the problem. To calculate the fibonacci numbers for example an iterative technique is better while recursion if often better in terms of graphs and trees.
16th Apr 2017, 9:16 AM
merkrafter