SlideShare a Scribd company logo
1 of 14
Recursion
Recursion
• Recursion is a programming technique in which a method can call
itself to fulfill its purpose
• A recursive definition is one which uses the word or concept being
defined in the definition itself
• In some situations, a recursive definition can be an appropriate way
to express a concept
• Before applying recursion to programming, it is best to practice
thinking recursively
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 2
How Recursion works?
Infinite Recursion
• All recursive definitions must have a non-recursive part
• If they don't, there is no way to terminate the recursive path
• A definition without a non-recursive part causes infinite recursion
• This problem is similar to an infinite loop -- with the definition itself
causing the infinite “looping”
• The non-recursive part is called the base case
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 4
Recursion in Math
• Mathematical formulas are often expressed recursively
• N!, for any positive integer N, is defined to be the product of all
integers between 1 and N inclusive
• This definition can be expressed recursively:
1! = 1
N! = N * (N-1)!
• A factorial is defined in terms of another factorial until the base case
of 1! is reached
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 5
Recursive Programming
• A method in Java can invoke itself; if set up that way, it is called a
recursive method
• The code of a recursive method must handle both the base case and
the recursive case
• Each call sets up a new execution environment, with new parameters
and new local variables
• As always, when the method completes, control returns to the
method that invoked it (which may be another instance of itself)
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 6
Recursive Programming
• Consider the problem of computing the sum of all the integers
between 1 and N, inclusive
• If N is 5, the sum is
1 + 2 + 3 + 4 + 5
• This problem can be expressed recursively as:
The sum of 1 to N is N plus the sum of 1 to N-1
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 7
Recursive Programming
• The sum of the integers between 1 and N:
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 8
Recursive Programming
• A recursive method that computes the sum of 1 to N:
public int sum(int num)
{
int result;
if (num == 1)
result = 1;
else
result = num + sum(num-1);
return result;
}
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 9
Recursive Programming
• Tracing the recursive calls of the sum method
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 10
Use recursion to add all of the numbers up to 10
public class Main {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
}
public static int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
}
Recursion vs. Iteration
• Just because we can use recursion to solve a problem, doesn't mean
we should
• For instance, we usually would not use recursion to solve the sum of 1
to N
• The iterative version is easier to understand (in fact there is a formula
that computes it without a loop at all)
• You must be able to determine when recursion is the correct
technique to use
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 13
Recursion vs. Iteration
• Every recursive solution has a corresponding iterative solution
• A recursive solution may simply be less efficient
• Furthermore, recursion has the overhead of multiple method
invocations
• However, for some problems recursive solutions are often more
simple and elegant to express
Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 14

More Related Content

Similar to Recursion.pptx

Lecture_7_StackAndRecursion (1).pptx
Lecture_7_StackAndRecursion (1).pptxLecture_7_StackAndRecursion (1).pptx
Lecture_7_StackAndRecursion (1).pptx
AbuHuraira729502
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Dheeraj Kataria
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
jamvantsolanki
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
Terry Yoast
 

Similar to Recursion.pptx (20)

Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
Ppt chapter12
Ppt chapter12Ppt chapter12
Ppt chapter12
 
Lecture_7_StackAndRecursion (1).pptx
Lecture_7_StackAndRecursion (1).pptxLecture_7_StackAndRecursion (1).pptx
Lecture_7_StackAndRecursion (1).pptx
 
Recursion, debugging in c
Recursion, debugging in cRecursion, debugging in c
Recursion, debugging in c
 
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIATypes Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
algorithm_6dynamic_programming.pdf
algorithm_6dynamic_programming.pdfalgorithm_6dynamic_programming.pdf
algorithm_6dynamic_programming.pdf
 
PNP.pptx
PNP.pptxPNP.pptx
PNP.pptx
 
PNP.pptx
PNP.pptxPNP.pptx
PNP.pptx
 
UNIT-V.ppt
UNIT-V.pptUNIT-V.ppt
UNIT-V.ppt
 
Python recursion
Python recursionPython recursion
Python recursion
 
Introduction to Recursion
Introduction to RecursionIntroduction to Recursion
Introduction to Recursion
 
Python Programming unit5 (1).pdf
Python Programming unit5 (1).pdfPython Programming unit5 (1).pdf
Python Programming unit5 (1).pdf
 
9781111530532 ppt ch13
9781111530532 ppt ch139781111530532 ppt ch13
9781111530532 ppt ch13
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructure
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Pascal Programming Language
Pascal Programming LanguagePascal Programming Language
Pascal Programming Language
 
N queens using backtracking
N queens using backtrackingN queens using backtracking
N queens using backtracking
 

More from VijalJain3 (6)

CN - Lecture 2.pptx
CN - Lecture 2.pptxCN - Lecture 2.pptx
CN - Lecture 2.pptx
 
1. Introduction to Cyber Security.pptx
1. Introduction to Cyber Security.pptx1. Introduction to Cyber Security.pptx
1. Introduction to Cyber Security.pptx
 
Random Class & File Handling.pptx
Random Class & File Handling.pptxRandom Class & File Handling.pptx
Random Class & File Handling.pptx
 
RSA.pptx
RSA.pptxRSA.pptx
RSA.pptx
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
AES.pptx
AES.pptxAES.pptx
AES.pptx
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 

Recently uploaded (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

Recursion.pptx

  • 2. Recursion • Recursion is a programming technique in which a method can call itself to fulfill its purpose • A recursive definition is one which uses the word or concept being defined in the definition itself • In some situations, a recursive definition can be an appropriate way to express a concept • Before applying recursion to programming, it is best to practice thinking recursively Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 2
  • 4. Infinite Recursion • All recursive definitions must have a non-recursive part • If they don't, there is no way to terminate the recursive path • A definition without a non-recursive part causes infinite recursion • This problem is similar to an infinite loop -- with the definition itself causing the infinite “looping” • The non-recursive part is called the base case Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 4
  • 5. Recursion in Math • Mathematical formulas are often expressed recursively • N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive • This definition can be expressed recursively: 1! = 1 N! = N * (N-1)! • A factorial is defined in terms of another factorial until the base case of 1! is reached Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 5
  • 6. Recursive Programming • A method in Java can invoke itself; if set up that way, it is called a recursive method • The code of a recursive method must handle both the base case and the recursive case • Each call sets up a new execution environment, with new parameters and new local variables • As always, when the method completes, control returns to the method that invoked it (which may be another instance of itself) Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 6
  • 7. Recursive Programming • Consider the problem of computing the sum of all the integers between 1 and N, inclusive • If N is 5, the sum is 1 + 2 + 3 + 4 + 5 • This problem can be expressed recursively as: The sum of 1 to N is N plus the sum of 1 to N-1 Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 7
  • 8. Recursive Programming • The sum of the integers between 1 and N: Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 8
  • 9. Recursive Programming • A recursive method that computes the sum of 1 to N: public int sum(int num) { int result; if (num == 1) result = 1; else result = num + sum(num-1); return result; } Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 9
  • 10. Recursive Programming • Tracing the recursive calls of the sum method Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 10
  • 11. Use recursion to add all of the numbers up to 10 public class Main { public static void main(String[] args) { int result = sum(10); System.out.println(result); } public static int sum(int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; } } }
  • 12.
  • 13. Recursion vs. Iteration • Just because we can use recursion to solve a problem, doesn't mean we should • For instance, we usually would not use recursion to solve the sum of 1 to N • The iterative version is easier to understand (in fact there is a formula that computes it without a loop at all) • You must be able to determine when recursion is the correct technique to use Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 13
  • 14. Recursion vs. Iteration • Every recursive solution has a corresponding iterative solution • A recursive solution may simply be less efficient • Furthermore, recursion has the overhead of multiple method invocations • However, for some problems recursive solutions are often more simple and elegant to express Java Foundations, 4th Edition, Lewis/DePasquale/Chase 17 - 14