+ 12
When should you use recursion instead of a loop within a function?
12 Antworten
+ 15
You would typically use recursion as a part of the loop, when you are dealing with arrays inside arrays. Example:
You have a for loop, which goes through every element of array. Inside the array you have... smaller arrays as elements! And you want to print every smaller array. You can do this in two ways. Without recursion:
write a loop to process an array, and inside it write another loop to perform the same action.
With recursion:
write a loop to process array element. If it happens to be an array itself, call this same function to process inner array.
Why bother:
If you have array inside array inside array inside array and they all share lots of properties, you can use the same function to process all this data. Its much more efficient than writing a separate function for each inner array.
Hopefully its easy to understand xD
+ 7
YOU CAN USE RECURSIVE WHEN THE ALGORITHM HAS TREE STRUCTURE AND YOU GIVE RETURN VALUE LEVEL BY LEVEL IN THE TREE TRAVERSAL
+ 5
NavatejaReddy I have to disagree, a function that using recursion can also be programmed using loops. It might not be as elegant as recursion for a certain problems as mohammad pointed out (btw great example) but it still can be written.
This has been scientifically proven. The problem you have to solve in a recursion -> loop conversion is how to have memory like the local variables generated by the recursion in the while loop. There is a very simple approach presented for this on stackoverflow.
+ 5
The other guys described it already very well. Just one additional note: be careful with recursion and use it only when you cannot solve the problem as efficiently with loops because recursive functions can lead to problems very quickly. Not only when you forget the base case but if the function calls itself too many times this can crash the program because of a stack overflow. Especially in Java DON'T DO RECURSION IN JAVA!! xD
+ 4
If we can divide a problem into smaller ones of similar type, we should employ recursion. For example, factorial of 5 needs to implicitly calculate factorial of 4, which in turn needs 3! and so on. In such cases, usage of recursion is efficient.
+ 4
A great example for recursion would be finding the Greatest Common Factor of two numbers:
int gcf(int a, int b) {
if (b == 0)
return a;
return gcf(b, a%b)
}
you could also use it for factorials. Usually though, loops require less cpu processing power when it comes to doing really large factorials, or even, fibonacci numbers, which could also use recursion
int fact(int n) {
if (n < 1)
return 1;
return n * fact(n - 1);
}
int fibonacci(int n) {//returns the nth fib number
if (n <= 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
+ 4
The solutions to some problems are more naturally expressed using recursion.
For example, assume that you have a tree data structure with two kinds of nodes: leaves, which store an integer value; and branches, which have a left and right subtree in their fields. Assume that the leaves are ordered, so that the lowest value is in the leftmost leaf.
Suppose the task is to print out the values of the tree in order. A recursive algorithm for doing this is quite natural:
class Node {
abstract void traverse();
}
class Leaf extends Node {
int val;
void traverse() {
print(val);
}
}
class Branch extends Node {
Node left, right;
void traverse() {
left.traverse();
right.traverse();
}
}
Writing equivalent code without recursion would be much more difficult. Try it!
More generally, recursion works well for algorithms on recursive data structures like trees, or for problems that can naturally be broken into sub-problems. Check out, for instance, divide and conquer algorithms.
+ 2
Recursion is best when you need to do code reusability and when you need to reduce time of execution of statements.
Looping is 'usually' best when you know exactly what amount times your code should execute
+ 1
@Yuri Predborski for example, finding in a json document?
- 8
You can use recursive function when the logic cant be implemented by the loops.
- 9
Hi
- 10
hi