One Pager Cheat Sheet
- Both iteration and
recursion
can be used to solve programming problems, but an important thing to keep in mind is that for any problem that can be solved via iteration, there is a corresponding recursive solution and vice versa. - Iteration is the
repetition
of a block of code usingcontrol variables
or a stopping criterion, typically in the form of for, while or do-while loop constructs. - A recursive function is one that calls itself, such as the
printList
function which uses thedivide and conquer
principle to print the numbers1
to5
. - The
printList
function implementsrecursion
by defining a base case (the non-recursive part) and a general / recursive case (defining how to solve the problem in terms of a smaller version of itself). - The execution of a recursive function
without a base case
will not raise an exception, but can lead tostack overflow
if the maximum number of recursive calls is exceeded. - Recursion is a technique that enables a function to save its
local variables
andparameters
on anactivation stack
, which is thenpopped off
when the function exits or thebase case
is reached. - The
printList
function will print the values in reverse,5, 4, 3, 2, 1
, when the statements are reversed, which happens during the "unwind recursion" phase after the base case is invoked. - We can calculate the factorial of a number using either an
iterative
orrecursive
implementation in C++. - The
iterative
solution is more efficient for programming languages such asC++
orJava
as it runs in constant memory, compared to therecursive
solutions which generates 4 records on theactivation stack
for each call. - The recursive version of the factorial function takes up more
memory
due to increasedactivation stack
records, while being less optimal in languages likeC++
andJava
. - Quick Sort is a naturally
recursive
sorting algorithm that divides the entire array into smaller pieces, and is conceptually easier to implement via recursion. - The clear concept behind
Quick Sort
is easily understood from the elegant and simple recursive version of the algorithm, which adjusts elements around a pivot before callingquickSort
recursively on both subarrays. - We need to
implement our own stack
to maintain theindices
of the left and right subarrays for the iterative version ofquickSort
. - Of course, the main question now is: which solution to go for,
iterative
orrecursive
, and I would still advise you to go for theiterative
solution to avoid segmentation faults while therecursive
version is easier to write and understand. - The
Towers of Hanoi
game is an example of a naturally recursive problem in which you move disks from one shelf to another while following a rule that a smaller disk cannot be placed on top of a larger disk. - Break down the problem into a smaller version of itself to solve it recursively using an intermediate location when moving disks.
- The figure shows the Recursion Tree for the 3 disk Towers of Hanoi problem, with the final solution being derived from a direct translation of the pseudo-code in
C++
. - We can solve the
Towers of Hanoi
problem using an iterative approach by pushing step 3, then 2 and then 1 onto a temporary stack. - The number of recursive calls required to check if the number 37 is prime using recursion is three, due to the
base case
and subsequent checks fordivisibility
. Using an iterative technique instead of recursion can be more efficient when solving problems like Towers of Hanoi since there would be no overhead associated with the activation stack.
- Replace recursion with iteration by initializing an empty array, iterating through it with a loop, removing each element for processing, making the relevant calculations, and finally, adding it back to the array.