Java TOPIC 33 – RECURSION LESSON NOTE DEFINITION Recursion is a problem solving technique that deals with expressing a problem in terms of one or more simpler versions of itself until a base case is reached. REAL WORLD EXAMPLE Let’s
consider the job of sorting a stack of papers. A person generally starts by taking the top
paper, dealing with it or placing it in a proper location. The stack is now smaller (simpler
case). This continues until the stack
is completely gone (base case). We
could write the instructions for the above situation as such: Sorting a Stack of Papers (n pages): If pages remain in stack } Notice
how the instructions on sorting a stack of papers actually references itself
(red text) but with less pages. That
is a simpler case. And that is
recursion. RECURSION IN PROGRAMMING The
most important and challenging step of understanding the use of recursion as
a programming technique is to be able to redefine a problem using a recursive
definition. This is a challenging
concept to master and practice will help a lot! In
programming, recursion occurs whenever a function calls itself. We call this a recursive function call, or
simply, a recursive call.
In
the example above, the recursive call only occurs while n > 1. The situation when the recursive call is
not used is referred to as the escape case or the base case. So, in the above example, the escape case
occurs when n equals 1. For
recursion to work, an escape case is required in order to stop the looping
process. STACK OVERFLOW ERROR Students
trying to use recursion for the first time often forget to code in an escape
case. This leads to an endless looping
execution with continuous recursive calls to the recursive function. However, this will not go on infinitely
because one can only do so many function calls inside one another before Java
throws a StackOverflowError. To
explain what a Stack Overflow Error is, we need to first look at what happens
when a function is called. When a
function call occurs, Java needs to store certain details about where the
function was called from so that it can return to that location after the
function is done. All this information
is placed inside a memory structure called a stack. If you call many functions one inside the
other, all of this data starts to fill the stack. And at one point, the stack is completely
filled up and adding more information causes an error.
FACTORIAL OF A NUMBER The
factorial of a number n is defined as the product of n times all over
positive numbers smaller than n. We
use the exclamation mark for the factorial symbol. So,
four factorial (usually written as 4!) is equal to: 4! = 4 x 3 x 2 x1 = 24
n! = n x (n-1) x (n-2) x … x
2 x 1 RECURSIVE DEFINITION OF FACTORIAL We
can also define the factorial of a number n recursively. We need to be able
to express the value of factorial by a simpler case. The continual application of simplification
needs to eventually lead to a base case. We
can recursively define n! in the following way: n! = n * (n-1)! for all n > 1 1! = 1 Using
the above definition we can find 4! by doing the following steps: 4! = 4 * (4-1)!
= 4 * 3! We
reapply the rule for 3!
= 4 * 3 * (3-1)!
= 4 * 3 * 2! We
reapply the rule for 2!
= 4 * 3 * 2 * (2-1)!
= 4 * 3 * 2 * 1! We
use base case for 1!
= 4 * 3 * 2 * 1
= 24
|
||
|