2. If can’t find the number, look for the number on page 2 3. If can’t find the number, look for the number on page 3 4. And keep repeating this until the number is found
• Recursion breaks a problem into smaller subproblems until a stopping case is reached • In computer programs, we can do recursion with method calls and if statements
page) If the name is before this page, start the algorithm again using the 1st part of the phonebook If the name is after this page, start the algorithm again using the 2nd part of the phonebook
in the phone book, one page at a time • O(n), because we must flip (loop) through each page • Recursive algorithm of dividing the phone book in half, and then half again, etc. to search for the number • O(log n), because we keep dividing the problem in half
• Recursive algorithm 1.Base case: if length of string equals one (1), display string 2.Recursive case: display the last character in the string, then call method with shorter string with the last letter cut off
string to display it backwards • O(n), because we must loop though each letter • Recursive algorithm of displaying the last character, deleting the last character, and keep calling the method • O(n), because have a method call for each letter
unit stores running programs • Divides the running program into 3 basic parts with 2 subdivisions Stack Segment Data Segment Code Segment runtime stack heap
in program code with letters A, B, C, D, … • Each letter represents the return address of each method • The return address is where program control returns to after a method ends execution
– one (1) box for each time a method is called by the executing program • This diagram shows 3 recursive method calls (ARIs) method1’s ARI (1st method call) method1’s ARI (2nd method call) method1’s ARI (3rd method call)
• When we draw the runtime stack of ARI (activation record instances) of a running program, this is also called a box trace • Basically, we are drawing boxes filled with data to help us understand how recursion works
a return value, so that the method can return the result of a calculation For example, in the program Backwards.java, the strings are written backwards by all 3 methods, and the string is returned to the main() method, which prints the strings
recursive method recursiveBack() 1. We already have return address labels B and D for the method calls 2. As the program runs, we will draw a box (ARI) for each method call • The commandline input to the program is the string "bat"
line #20 with return address B • Once this method ends, the return value is assigned to variable backwards • Runtime stack No ARIs (activation record instances), boxes that store the data for the local environment of a method, on the runtime stack yet
#48 assigns length of string parameter to local variable length • Since if statement is false, program jumps to line #56 • Runtime stack return address=B string="bat" length=3
return statement adds the letter "t" to another method call with argument "ba" • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba")
line #58, pushes return address D and argument "ba" to the runtime stack • Program control jumps to line #47 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba"
line #48 • A different string parameter’s length value is assigned to a different length local variable • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2
at line #50 is still false, program control continues to the else statement on line #56 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2
assigned 1, so the if statement is true • On line #51, the parameter string is returned with value "b" return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b"
line #51 is a base case, so no more recursive method calls return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b"
return value to the calling method located at the return address, and pop the bottommost ARI off the runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b"
line #51, returns return value "b" to return address D at line #57, and pops the bottommost ARI off runtime stack • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+"b"
the runtime stack has changed from "a"+m("b") to "a"+"b", which is the string "ab" • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="ab"
line #57 to line #58 • The recursive method call returns the value "ab" to the return address D at line #57 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="ab"
finished • At the return statement at line #57, return value "tab" is returned to the address B at line #20 in main() • Runtime stack return address=B string="bat" length=3 return value="tab"
to line #20, the return value "tab" is assigned to local variable backwards • Runtime stack Since all of the ARIs have been popped off the runtime stack, we have no boxes of data for the method’s local environment anymore…
code with a red dot breakpoint, press ladybug button, and press the “Step in for selected thread” button (top left blue arrow curving down to the right) • Also check out the call stack window to see the ARI for each method
which add integers from 1 to 5 using loops and recursion • Also has example of infinite recursion • If the method never reaches the base case, then the runtime stack will fill up with method calls (ARIs), and run out of memory (StackOverflowError)
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Go outside and get some exercise!