Transcript L1

Recursion
What is Recursion
It is the idea that a method can call itself
void method()
{
method();
}
Recursion is best used to break a problem into smaller parts with each part
being solved recursively. This approach to problem solving is called Divide
and Conquer.
Recursion body  Needs X Condition
void method()
{
if(condition)
// end recursion
else
method();
}
void method()
{
if(condition)
method();
else
// end recursion
}
Avoid infinite recursion
void method()
{
method();
}
Loop Similarities?
Iterative Solution
Recursive Solution
for(int k = 0; k < 10; k++)
print(“Hello World);
void method(int a)
{
if(a > 0)
method(a-1);
print(“Hello World”);
}
void main()
{
method(10);
}
Recursion Stack
void method(int a)
{
if(a > 0)
method(a-1);
print(“Hello World”);
}
void main()
{
method(10);
}
The first method called
is the last method
finished.
Recursion always keeps
a stack to keep track of
methods and their data
The methods on the top
of the stack finishes fist
Method 5
Method 4
Method 3
Method 2
Method 1
Recursion Stack
• In terms of computer memory, the stack is an area of random-access
memory (RAM) allocated to hold all of the parameters and local variables
associated with any method used by a currently running program. The
stack is also responsible for remembering the order in which methods are
called so they can be returned correctly. Whenever a method is called, the
parameters and local variables associated with it are added to the stack.
When the method returns, the parameters and the variables are removed
("popped") from the stack. A stack is a LIFO (Last-In-First-Out) data
structure. This means that the last method data that was added to the
stack is the first method data that is removed ("popped") from the stack
and the method data that was added first to the stack is the last data
removed. A program's stack size changes continuously while the program is
running.
Recursion Stack Continued
int method(int a)
{
if(a == 100)
{
return 100;
}
else
{
print(a);
return method(a + 10);
}
}
void main()
{
System.out.print(method(10));
}
Recursion Stack Continued
void method(int a)
{
if(a == 50)
{
a = 50;
}
else
{
method(a + 10);
}
System.out.print(a);
}
void main()
{
method(0);
}
Recursion
• Break up a problem into smaller
parts, solve them individually and
then combine the “parts.” This is
often known as “Divide and
Conquer.” Recursive methods are
great for problems that can be
broken down into smaller,
identical pieces.
• Often much cleaner code
• Generally preferred by
mathematicians due to its abstract
nature and shorter code
Iteration
• Keep looping until task is “done”
• Has less memory complexity since
it does not have to keep track of a
recursive stack.
• Generally preferred by
programmers due to its more
formal control structure.
package filename
public class filename
{
(put your methods here)
public static void main(String[ ] args)
{
}
}
Exercise 1: Stack overflow
void method(int a)
{
method(a++);
}
Exercise 2
Using recursion, count down from 50 to 0.
50
49
48
47
46
0
Exercise 3
Given a number, find its factorial. Be sure we use recursion.
For example, 5! = 5*4*3*2*1 = 120
Exercise 4
Given a number, find the Fibonacci sequence value.
Fibonacci sequence rule: any number is the sum of its past two
numbers.
1
1
2
3
5
8
13
21
34
55
89
So Fibonacci(10) should return 55. This solution exists online, please
don’t just copy and paste, that does you no good.
Exercise 5
Create an integer array of size 20 and use recursion to assign random
values between 0-100 to all fields.
Now use recursion to display this array.
No loops of any kind please.