Unit 8 - Recursion
Unit 8 - Recursion
UNIT 8: RECURSION
TEXT
RECURSION
▸ A recursion function is defined in terms of itself
RECURSIVE FUNCTIONS
▸ To write recursive functions we break up the problem into two cases:
▸ #2 A “base case” of the problem where the answer is known ahead of time, or easily computed
▸ factorial(n):
FACTORIAL EXAMPLE
unsigned long fact(unsigned long n)
//base case
if(n == 1) return 1;
return n * r
}
TEXT
return 3 * 2 return 2 * 1
TEXT
▸ Head Recursion: immediately make the recursive call then do some work
{ {
if(n==1) { if(n==1) {
cout << “Stop.” << endl; cout << “Stop.” << endl;
return;
return;
} }
} }
TEXT
int main()
{
RECURSIVE FUNCTIONS int data[4] = {8, 6, 7, 9};
int sum1 = isum_it(data, 4);
int sum2 = rsum_it(data, 4);
▸ Recursive functions usually take the place of }
loops int isum_it(int data[], int len)
{
sum = data[0];
▸ Example: summing up an array of integers for(int i=1; i < len; i++){
sum += data[i];
}
}
data[4] =
{8,6,7,9};
rsum_it(data, 4) rsum_it(data, 3) rsum_it(data, 2) rsum_it(data, 1)
each
STACK
y storage MAKES
runningfor
RECURSION POSSIBLE
int rsum_it(int data[], int len)
the local return data[0];
instance
if(len == 1)
{
if(len == 1)
es
on ▸
of How
each does this
running work: we
instance
else call
int sum =
the *same*
return function over and over?
data[0];
else
rsum_it(data, len-1);
unction
▸ Because of the stack! We can call the function as many times as need be,
int sum =
return sum + data[len-1];
} rsum_it(data, len-1);
each gets its *own* stack
Code for all functions
frame: hence
return sum + each has it’s
data[len-1]; own stack-local variables
}
Code
forfor all functions 800
ata rsum_it
Code(data=800,
for all functions int main()
=1, sum=??) and return link 8 6 7 9
{
ata for rsum_it (data=800, data[4]: 0 1 2 3
n=2,
=2, sum=??)
sum=8) Code
Data for
and
and for
returnall
rsum_it
return link
linkfunctions
(data=800, 800 int data[4] = {8, 6, 7, 9};
ata forlen=1,
rsum_itsum=??) and return link
(data=800, 8 6 int
7 9 sum2 = rsum_it(data, 4);
=3, sum=14)
sum=??) and return link }
Data for rsum_it (data=800, data[4]: 0 1 2 3
ata forlen=2,
rsum_it (data=800,
len=2, sum=??)
sum=8) and
and return
return link
link
=4, sum=21)
sum=??) and return link int rsum_it(int data[], int len)
for main Data for rsum_itand(data=800,
(data=800,sum2=??) {
len=3, sum=??) and return link
sum=14)
return link
if(len == 1)
Data
System forarea
stack rsum_it (data=800,
len=4, sum=21)
sum=??) and return link
return data[0];
Data for main (data=800,sum2=??) and
else
return link int sum = rsum_it(data, len-1);
return sum + data[len-1];
System stack area
}
TEXT
IN CLASS EXERCISE
▸ count-up
▸ count-down
TEXT
▸ This case must terminate the recursion, i.e it will not make a further recursive
call
▸ The recursive case must “make progress” towards the base case
▸ Recursive cases and base cases must have return statements to propagate the
answer “up” the recursive call stack
TEXT
▸ Recursion pros:
▸ Power of recursion comes from making multiple recursive calls - hard to implement with iterative solutions
▸ How to choose?
k = 11
• Assume
RECURSIVE remaining items = [start, end)
EXAMPLES List 2 3 4 6 9 11 13 15 19
– start is inclusive index of start item in remaining list index 0 1 2 3 4 5 6 7 8
Sorting
TEXT
▸ Recursive•Bubble
If we Sort
will perform a lot of searches it may List
index
3 7 6 5 1 8
0 1 2 3 4 5
be beneficial to sort the list, then use After Pass 1
▸ List with indexes 0 -search
binary (n-2)
List 3 6 5 1 7 8
• Many
▸ Scan list and swap sorting algorithms
to “bubble” largest of differing
value to the right
index 0 1 2 3 4 5
After Pass 2
complexity (i.e. faster or slower)
▸ Recurse on list indexes 0 - (n-2), then 0 - (n - 3), etc. List 3 5 1 6 7 8
• Bubble Sort (simple though not terribly index 0 1 2 3 4 5
After Pass 3
efficient)
List 3 1 5 6 7 8
– On each pass through thru the list, pick up the index 0 1 2 3 4 5
maximum element and place it at the end of After Pass 4
the list. Then repeat using a list of size n-1 (i.e. List 1 3 5 6 7 8
w/o the newly placed maximum value) index 0 1 2 3 4 5
After Pass 5
TEXT
IN CLASS EXERCISE
▸ Text Fractal
TEXT
CODING TIME!
▸ Examples to code: ▸ Examples to code:
▸ recursive sum ▸ Recursive Insertion sort
▸ index version
▸ Fibonacci
▸ pointer version
▸ index version
▸ pointer version