Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 240dfc3

Browse files
Added Recursion Topic
1 parent 4b8a3b9 commit 240dfc3

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Recursion {
2+
3+
// Used Example Of Factorial
4+
public class Factorial {
5+
public int recursiveFactorial(int n) {
6+
if (n > 0) {
7+
System.out.println("Execution of: " + n);
8+
return n * recursiveFactorial(n - 1);
9+
} else {
10+
return 1;
11+
}
12+
}
13+
14+
public void main(String[] args) {
15+
int recursiveSolution = recursiveFactorial(4);
16+
System.out.println(recursiveSolution);
17+
}
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Recursion
2+
Recursion has _two_ fundamental aspects:
3+
1. **Base Case**
4+
2. **Recursive Step**
5+
6+
When using iteration, we rely on a counting variable and a boolean condition.
7+
8+
For example, when iterating through the values in a list, we would increment the counting variable until it exceeded the length of the dataset.
9+
10+
Recursive functions have a similar concept, which we call the **base case**.
11+
12+
The _base case_ dictates whether the function will recurse, or call itself. Without a base case, it’s the iterative equivalent to writing an _infinite loop_.
13+
14+
Because we’re using a **call stack** to track the function calls, your computer will throw an error due to a _stack overflow_ if the base case is not sufficient.
15+
16+
The other fundamental aspect of a recursive function is the recursive step.
17+
18+
This portion of the function is the step that moves us closer to the base case.
19+
20+
In an iterative function, this is handled by a loop construct that decrements or increments the counting variable which moves the counter closer to a boolean condition, terminating the loop.
21+
22+
In a recursive function, the "counting variable" equivalent is the argument to the recursive call.
23+
24+
If we’re counting down to 0, for example, our base case would be the function call that receives 0 as an argument.
25+
26+
We might design a recursive step that takes the argument passed in, decrements it by one, and calls the function again with the decremented argument.
27+
28+
In this way, we would be moving towards 0 as our base case.
29+
30+
Analyzing the Big O runtime of a recursive function is very similar to analyzing an iterative function. Substitute iterations of a loop with recursive calls.
31+
32+
Also learn about _Call Stacks and Execution Frames_, it will help understand recursion even better.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /