#RECURSION
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Recursion with two recursive steps means your function calls itself more than once within the same invocation. Unlike simple recursion where a function calls itself once (e.g., factorial), here you have multiple calls that branch the execution.
For example, in a function like the Fibonacci sequence:
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
This function calls itself twice: fib(n-1) and fib(n-2). Each call creates a new branch of execution that further calls the function, growing the call tree exponentially.
Key points:
- The recursion tree expands faster since you have multiple calls per frame.
- Base case(s) stop the infinite recursion.
- Each recursive call works independently, but their results often combine to form the answer.
- Understanding this helps grasp the calls' growth and potential performance issues (like overlapping subproblems).
If you draw the call tree, you’ll see how a single input expands into many function calls, each handling a smaller subproblem.
Useful links:
do you mean tree recursion?
for example in merge sort
like it calls itself for the left and and the right side
yes, so this is called tree recursion, because it creates a recursion tree of recursive calls
so after the first recursive step and recursion it goes to the second one
and so on
Yes but it will first exhaust all the "left" side recursive calls, then come up from the bottom doing the right side call.
so the left recursion will be called until the base case is met?
i understand the fibonacci somehow i just cant seem to grasp the idea around the merge sort
but for merge sort, take the algorithm and draw a tree of the recursive calls on a sheet of paper
it is the same principle
The fibonacci example here is a good example for when you need to retain intermediary results. Look at the image Spectre posted, see how many times fib(1) or fib(0) is called. Waste.
yeah the trivial recursive fibonacci implementation is very bad, (well every recurisive fib is inferior to the trivial iterative) but i think it is a nice example for showing tree recursion because it is so easy
here is one for mergesort, but i think going through that algorithm and drawing the tree on a sheet of paper, help for understanding it
yeah i understand the divide and conquer but i just cant see how that translates to code
like when the recursive steps are one under the other
when the first recursion ends is the first recursive step activated again or does it go to the second one
yes it goes down to the atomar layer of the left most branch first
there no further recursion happens becuse it just returns, then the right one gets called, then the recusion layer is finished and we are back in the layer above, there the left call is finished because we just reurnt from that and the right one gets called
#1506309939059228845 message
here like surely joe said
so it loops on the left recursion until the base case is met?
and then the right is called?
i really recomend taking the algorithm and pen and paper and drawing the tree
step by step
then you will see
Yes, inside the mergeSort method is two calls to itself then a call to some outside merge method that joins them in the correct order.
i think for recursion it takes until it makes click but then you understand it
does the merge happen seperatly for the left and right side?
public static void mergeSort(int[] a, int startIdx, int endIdx){
//You have some range over a[] when you enter this function
mergeSort(a, startIdx, midpoint);
mergeSort(a, midpoint, endIdx);
// now you merge, you have access to the original range
merge(a, start,mid,end);// something like this;
}```
The point of recursion is that it holds temporary variables like start, mid,end, in its stack frame. The alternative in non-recursive is you keep track of those variables in some sort of Stack variable instead.
do u have trouble understanding this?
void foo(int x) {
if (x != 0) bar(x - 1);
System.out.println(x);
}
void bar(int x) {
if (x != 0) baz(x - 1);
System.out.println(x);
}
void baz(int x) {
if (x != 0) System.out.println("end");
System.out.println(x);
}
what does calling foo(2) print?
there is no recursion here.
if u understand that u also understand recursion. demystify it, java has no idea what recursion is. it just executes method after method
"end"? and 2
i think
Look at this picture again. The outer most mergesort is working on the top row, and then the merge part is working on the bottom row. The second mergesort layer is 38, 27, 43, 3 on the top and 3, 27, 38, 43 on the bottom.
no. take pen and paper and execute it step by step like "a dumb java robot"
thats how java works after all. its a dumb robot running line by line
dont overcomplicate it. dont guess. run it by hand line by line
yeah i cant understand how this shit works thanks for your help and time tho
do u understand this?
void foo(int x) {
System.out.println(x);
bar(x - 1);
System.out.println(x);
}
void bar(int x) {
System.out.println("bar" + x);
}
what does it print for foo(2)?
we are stepping back until the point where its becoming clear
there are 3 prints. not two
why would it end before
yeah ok i get that
so whats the output?
2 bar1 2
yup. so next:
what about this?
void foo(int x) {
if (x > 0) bar(x - 1);
System.out.println("foo" + x);
}
void bar(int x) {
System.out.println("bar" + x);
}
what does it print for foo(2)?
bar1 foo2
great.
so back to this:
void foo(int x) {
if (x != 0) bar(x - 1);
System.out.println(x);
}
void bar(int x) {
if (x != 0) baz(x - 1);
System.out.println(x);
}
void baz(int x) {
if (x != 0) System.out.println("end");
System.out.println(x);
}
what does calling foo(2) print?
its exactly the same. just a third method
0 1 2
nice
so now we look at this:
void foo(int x) {
if (x != 0) foo(x - 1);
System.out.println(x);
}
what does calling foo(2) print?
as a reminder: java has no idea what recursion is. it works the same way as if it would be any other method
unfortunately not correct
its the exact same code as the one right before
with foo bar baz
its absolutely identical
so i guess ur still overcomplicating it
instead of seeing that there is no magic involved
🙂
2!=0 true so it call itself again and goes back to the if no?
foo(2) starts. if check, enters the if
so it calls foo(1) next
what does java do when it calls another method?
it pauses the current method
continues with the other method first
and once that is done it eventually comes back and resumes where it paused
so foo(2) is paused
foo(1) starts
enters if. foo(0). so foo(1) pauses
foo(0) starts
if check. not enter. print(0)
foo(0) is done
java resumes its caller, foo(1) where it paused
print(1)
foo(1) is done
java resumes its caller foo(2) where it paused
wait it prints and then calls the method?
print(2)
no.
go back to the old code
foo(2) calls bar(1)
foo(2) is paused. bar(1) starts
bar(1) calls baz(0). bar(1) is paused
baz(0) prints 0 and is done
java resumes bar(1). print 1
bar(1) is done. java resumes foo(2). print 2
done
its exactly the same
if u understand the previous code u also understand this
if not, ur overcomplicating
u have to understand and visualize how java moves through the code
line by line. when a method calls another method it will pause the original method and do the other method first
if foo calls bar, java needs to pause foo and do bar first
once bar is done it comes back and resumes foo
and it will go back to the other methods the waay it came?
u know how to this works already. look at this
void foo() {
System.out.println("hello");
String name = askForName();
System.out.println(name);
}
foo calls askForName. foo is paused in the middle of its execution
askForName needs to happen now
once its done java will continue foo
with the last line
where it paused
its all there and u know it already
dont rush ahead yet
ok ok
u cant visualize this yet
1 0?
nope. lets step back. walk me through every line. tell me what java executes
we start with foo(2)
what happens?
x!=0 true so it calls foo(1)
1!=0 true agian
right. so?
yes
0!=0 false so the base case is met
yeah. java has no idea what a base case it. it runs line by line. whats the next line it runs
no
look at this again
void foo() {
System.out.println("hello");
String name = askForName();
System.out.println(name);
}
what happens when askForName() is done?
the name is printed
so. java resumes the paused foo() method. resumes it at the point where it called askForName and was paused
the caller gets resumed
yes. so what happens when foo(0) is done?
resumes those paused ones
which one
in order?
who called foo(0)?
we were essentially here:
void foo(1) {
if (1 != 0) foo(0);
System.out.println(1);
}
0 1 2
see where it says foo(0)
i think i get it
thats where it paused foo(1)
so now that foo(0) came back, java resumes exactly there:
exactly the same way as with askForName()
no magic involved
so when a method calls itself it has to go back to who called it
and it starts from where it wss called
ur making this sound as if its special. its for any method
yeah lol
method A calls method B, A is paused. when B is done we go back to A
so recursion is fake
yes
its not like a real thing
java doesnt care the slightest
ok this is starting to make sense
with that having clicked u now just have to play this "dumb robot running line by line" game
so its the same with two recursive steps
so in merge sort
when it recurses its self for the left after its done it goes to the next line which is the recursion to itself for the righ
yes
https://visualgo.net/en/sorting
here is a visualisation which also shows the code. but my recommendations stays to try it line by line with pen and paper first, you are the java code executor, draw the tree. or look at this and then try to do it by yourself.
Sorting is a very classic problem of reordering items (that can be compared, e.g., integers, floating-point numbers, strings, etc) of an array (or a list) in a certain order (increasing, non-decreasing (increasing or flat), decreasing, non-increasing (decreasing or flat), lexicographical, etc).There are many different sorting algorithms, each ha...
but after doesnt it have to recurse back to itself to the left?
wait it does
from following the lines