Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are recursive methods always better than iterative methods in Java? [closed]

Tags:

java

recursion

Are recursive methods always better than iterative methods in Java?

Also can they always be used in place of iteration and vice-versa?

like image 404
Hoon Avatar asked Mar 11 '13 19:03

Hoon


People also ask

Is recursion or iteration more efficient Java?

Iteration is generally going to be more efficient. Recursion requires more memory (to set up stack frames) and time (for the same).

Are recursive functions more efficient than iterative algorithms?

Iterative algorithms and methods are generally more efficient than recursive algorithms. Recursion is based on two key problem solving concepts: divide and conquer and self-similarity. A recursive solution solves a problem by solving a smaller instance of the same problem.

Why iteration is better than recursion in Java?

An iteration does not use the stack so it's faster than recursion. Iteration consumes less memory. Iteration makes the code longer.


2 Answers

Are recursive methods always better than iterative methods in java?

No

Also can they always be used in place of iteration and vice-versa?

You can always make an iterative function from a recursive one (if memory can handle it, see interesting link here).

For some cases, it's better to use recursion (like when dealing with trees.. traveling on a binary tree, etc.). For me, if using loops isn't more complicated and much more difficult than a recursion, I prefer to use loops.

Recursion uses more memory but is sometimes clearer and more readable. Using loops increases the performance, but recursion can sometimes be better for the programmer (and their performance).

So, for the conclusion, deciding what to use - recursion or iteration, depends on what you want to implement, and what's more important for you (readability, performance...), and asking recursion or iteration is like asking for elegance or performance.


Example

Consider these two implementation for the factorial:

Iterative:

private int Factorial(int num) {     int result = 1;     if (num <= 1)         return result;     while (num > 1)     {         result * = num;         num--;     }     return result; } 

Recursion:

private int Factorial(int num) {     if (num <= 1)          return 1;     return num * Factorial(num - 1); } 

Which method is more readable?

Obviously the recursive one, it is straight forward and can be written and successfully run from the first try - It is simply translating math definition into Java!

Which method is more efficient?

Take for example num = 40, here is a time comparison:

long start = System.nanoTime(); int res = Factorial(40); long end = System.nanoTime(); long elapsed = end - start;          System.out.println("Time: " + elapsed);  

2993 for the recursive

2138 for the iterative

of course, the difference will be larger when num is larger.

like image 82
Maroun Avatar answered Oct 20 '22 20:10

Maroun


Recursion is good for programmers to understand a program, but many times they cause stackoverflows hence always prefer iteration over them.

The fact is that recursion is rarely the most efficient approach to solving a problem, and iteration is almost always more efficient. This is because there is usually more overhead associated with making recursive calls due to the fact that the call stack is so heavily used during recursion.
This means that many computer programming languages will spend more time maintaining the call stack then they will actually performing the necessary calculations.

Does recursion use more memory than iteration? Generally speaking, yes it does. This is because of the extensive use of the call stack.

Should I use recursion or iteration?

Recursion is generally used because of the fact that it is simpler to implement, and it is usually more ‘elegant’ than iterative solutions. Remember that anything that’s done in recursion can also be done iteratively, but with recursion there is generally a performance drawback. But, depending on the problem that you are trying to solve, that performance drawback can be very insignificant – in which case it makes sense to use recursion. With recursion, you also get the added benefit that other programmers can more easily understand your code – which is always a good thing to have.

like image 39
amod Avatar answered Oct 20 '22 20:10

amod