This code will terminate as the function is calld with k-1
, meaning that the condition k>1
will eventually evaluate to False
.
Imagine recursion is like a continually growing tree and a squirrel is the interpreter. When the isort()
function is called, the tree branches off and the squirrel runs to the end of that branch. However, the tree uses has a finite supply of nutrients (k
) each time, and a bit is used (k-1
) each time it grows a new branch. The tree will stop branching off when it runs out of nutrients (which is the k>1
condition). When the tree stops growing, the squirrel will reach the end of the last branch and get the nut (return
value/ next line(s) of code). The squirrel will now run back to the roots (the code (if any) after the call to the recursive function) by going back through the branches (leaving each recursion depth). When the squirrel arrives back at the roots, the program is finished.
(Hope this analogy helps 🙂 )
4
solved recursion code should go infinitely?