Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 26A

CSE110 Lecture 26A

Principles of Programming with Java
Recursion
(202007)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

June 27, 2017
Tweet

Transcript

  1. CSE110 Principles of Programming with Java Lecture 26: Recursion Javier

    Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 class

    Previously main()
  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 class

    Previously main() class class class
  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 One

    last thing Recursion is a programming technique in which a method can call itself.
  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6 Case

    A public static void main(String[]args) { sayHello(); } public static void sayHello() { System.out.println("Hello"); sayHello(); // recursive }
  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Case

    B public static void main(String[]args) { sayHello(5); } // a recursive method will always have a condition public static void sayHello(int num) { if (num == 0) System.out.println("Hello and Bye!"); else { System.out.println("Hello"); sayHello(num-1); // recursive } }
  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Case

    C public static void main(String[]args) { printNumbers(10); } // a recursive method will always have a condition public static void printNumbers(int num) { if (num == 0) System.out.println(num); else { System.out.println(num); printNumbers(num-1); // recursive System.out.println(num); } }
  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Recursion

    • Each time a recursive method is called, a new copy of the method runs, with new instances of parameters and local variables being created • As each copy finishes executing, it returns to the copy of the method that called it • When the initial copy finishes executing, it returns to the part of the program that made the initial call to the method
  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Types

    of Recursion • Direct recursion: a method calls itself • Indirect recursion: method A calls method B, and method B calls methodA. Or, method A calls method B, which calls ..., which calls method A
  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Recursive

    Factorial Method The natural definition of some problems leads to a recursive solution • The factorial of a nonnegative integer n is the product of all positive integers less or equal to n • Factorial of n is n * (n-1) * ... * 2 x 1 • The factorial of 0 is 1
  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12 Recursive

    Factorial Method // Factorial of n can be expressed in terms of // the factorial of n-1 // 0 ! =1 // n ! = n x (n-1) ! public static int factorial(int n) { if (n == 0) return 1; else return n*factorial(n-1); }
  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 Recursive

    Fibonacci Numbers The natural definition of some problems leads to a recursive solution • The Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... • After the starting 0, 1, each term is the sum of the two preceding terms • Recursive solution: fib(n) = fib(n – 1) + fib(n – 2); • Base cases: fib(0) =0, fib(1) =1
  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Recursive

    Fibonacci Numbers public static int fib (int n) { if (n <= 0) return 0; else if (n == 1) return 1; else return fib(n – 1) + fib(n – 2); }
  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Reference

    Chapter 5, pages 228 - 232
  15. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2020

    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.