SlideShare a Scribd company logo
1 of 12
Basics
&7Cipher
• Any function which call it self called a Recursive function.
• A recursive function solve a problem by calling a copy of itself to work on a
smaller problem called Recursive function.
• In Recursion it is important to insure the Recursion terminates.
• Recursive code are generally shorter and easier to write than Iterative function.
• Recursion is most useful for a task which can be define in terms of similar
subtask.
&7Ciph
er
• A Recursive function perform a task by calling itself to perform a subtask and
at some point the function encounter a subtask that can be performed without
calling itself.
• The case where the function do not recur is called BASE CASE.
Example:
Program to find factorial of any number n
n! = 1 if n == 0
n! = n * (n-a)! if n > 0
&7Ciph
er
• Iterative solution :
int iterativeFactorialMethod(int n){
int factorial = 1;
if (n <= 0)
{
cout << "Factorial of number is : " << factorial << endl;
return factorial;
}
else
{
for (int i = 1; i <= n; i++)
{
factorial = factorial * i;
}
&7Ciph
er
• Recursive solution :
int recursiveFactorialMethod(int n){
int factorial = 1;
if (n <= 0)
{
cout << "Factorial of number is : " << factorial << endl;
return factorial;
}
else
{
factorial = n * recursiveFactorialMethod(n-1);
}
cout << "Factorial of number is : " << factorial << endl;
&7Ciph
er
• Iteration terminates when a condition is proven to be false.
• Each iteration do not require extra spaces.
• Ab infinite loop could loop forever since there is no extra spaces being
created.
• Iterative solution to a problem may not the obvious as recursive solution.
&7Ciph
er
• A recursive approach mirror the problem which we are trying to solve.
• A Recursive approach make it simpler to solve a problem that may not have
the obvious answer or solution but recursion adds overheads for each
recursive call.
• Which means it needs more space on stack.
• Every recursive solution gets memory in stack which means if we will solve it
with a large input and we have a low memory in our stack than at run time we
will encounter stack-over-flow situation.
&7Ciph
er
• Recursion get terminated when it reach to the base case.
• Each recursion call need extra space on stack frame.
• If we encounter infinite recursion the program may run out of memory and that
result stack over flow.
• Solution of some problem can easier to formulate with recursion.
&7Ciph
er
• Recursive algorithm have two type of case.
1. Recursive case
2. Base case
• Every recursive algorithm must terminates on his Base case.
• Generally Iterative solution are more efficient than Recursive solution.
• For some problem there are no obvious Iterative solution possible.
• Some problems are best suited for Recursive algorithm and vice-versa.
&7Ciph
er
• Fibonacci series
• Merge sort
• Quick sort
• Tower of Hanoi
• Divide and Conquer Algorithm
• Dynamic Programming
• Graph traversal (DFS and BFS)
• Backtracking Algorithm
&7Ciph
er
&7Cipher

More Related Content

Similar to Recursion DS made easy - 2

Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptxVijalJain3
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructurePrasanna996462
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and loopingxcoolanurag
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15Terry Yoast
 
Chapter 7 recursion handouts with notes
Chapter 7   recursion handouts with notesChapter 7   recursion handouts with notes
Chapter 7 recursion handouts with notesmailund
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSASumit Pandey
 
Recursion
RecursionRecursion
RecursionNikxjon
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structurecogaxor346
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2Roy Thomas
 
Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Tim Bunce
 
Benchmarking and PHPBench
Benchmarking and PHPBenchBenchmarking and PHPBench
Benchmarking and PHPBenchdantleech
 
Derivative Free Optimization and Robust Optimization
Derivative Free Optimization and Robust OptimizationDerivative Free Optimization and Robust Optimization
Derivative Free Optimization and Robust OptimizationSSA KPI
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)BoneyGawande
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardAnimesh Chaturvedi
 

Similar to Recursion DS made easy - 2 (20)

14. Recursion.pdf
14. Recursion.pdf14. Recursion.pdf
14. Recursion.pdf
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
Introduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructureIntroduction to Algorithms And DataStructure
Introduction to Algorithms And DataStructure
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
Chapter 7 recursion handouts with notes
Chapter 7   recursion handouts with notesChapter 7   recursion handouts with notes
Chapter 7 recursion handouts with notes
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSA
 
Recursion
RecursionRecursion
Recursion
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2
 
Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
CPP10 - Debugging
CPP10 - DebuggingCPP10 - Debugging
CPP10 - Debugging
 
RecursionWeek8.ppt
RecursionWeek8.pptRecursionWeek8.ppt
RecursionWeek8.ppt
 
Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406Devel::NYTProf v5 at YAPC::NA 201406
Devel::NYTProf v5 at YAPC::NA 201406
 
Savitch ch 14
Savitch ch 14Savitch ch 14
Savitch ch 14
 
Benchmarking and PHPBench
Benchmarking and PHPBenchBenchmarking and PHPBench
Benchmarking and PHPBench
 
Savitch Ch 14
Savitch Ch 14Savitch Ch 14
Savitch Ch 14
 
Derivative Free Optimization and Robust Optimization
Derivative Free Optimization and Robust OptimizationDerivative Free Optimization and Robust Optimization
Derivative Free Optimization and Robust Optimization
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 

Recursion DS made easy - 2

  • 2. • Any function which call it self called a Recursive function. • A recursive function solve a problem by calling a copy of itself to work on a smaller problem called Recursive function. • In Recursion it is important to insure the Recursion terminates. • Recursive code are generally shorter and easier to write than Iterative function. • Recursion is most useful for a task which can be define in terms of similar subtask. &7Ciph er
  • 3. • A Recursive function perform a task by calling itself to perform a subtask and at some point the function encounter a subtask that can be performed without calling itself. • The case where the function do not recur is called BASE CASE. Example: Program to find factorial of any number n n! = 1 if n == 0 n! = n * (n-a)! if n > 0 &7Ciph er
  • 4. • Iterative solution : int iterativeFactorialMethod(int n){ int factorial = 1; if (n <= 0) { cout << "Factorial of number is : " << factorial << endl; return factorial; } else { for (int i = 1; i <= n; i++) { factorial = factorial * i; } &7Ciph er
  • 5. • Recursive solution : int recursiveFactorialMethod(int n){ int factorial = 1; if (n <= 0) { cout << "Factorial of number is : " << factorial << endl; return factorial; } else { factorial = n * recursiveFactorialMethod(n-1); } cout << "Factorial of number is : " << factorial << endl; &7Ciph er
  • 6.
  • 7. • Iteration terminates when a condition is proven to be false. • Each iteration do not require extra spaces. • Ab infinite loop could loop forever since there is no extra spaces being created. • Iterative solution to a problem may not the obvious as recursive solution. &7Ciph er
  • 8. • A recursive approach mirror the problem which we are trying to solve. • A Recursive approach make it simpler to solve a problem that may not have the obvious answer or solution but recursion adds overheads for each recursive call. • Which means it needs more space on stack. • Every recursive solution gets memory in stack which means if we will solve it with a large input and we have a low memory in our stack than at run time we will encounter stack-over-flow situation. &7Ciph er
  • 9. • Recursion get terminated when it reach to the base case. • Each recursion call need extra space on stack frame. • If we encounter infinite recursion the program may run out of memory and that result stack over flow. • Solution of some problem can easier to formulate with recursion. &7Ciph er
  • 10. • Recursive algorithm have two type of case. 1. Recursive case 2. Base case • Every recursive algorithm must terminates on his Base case. • Generally Iterative solution are more efficient than Recursive solution. • For some problem there are no obvious Iterative solution possible. • Some problems are best suited for Recursive algorithm and vice-versa. &7Ciph er
  • 11. • Fibonacci series • Merge sort • Quick sort • Tower of Hanoi • Divide and Conquer Algorithm • Dynamic Programming • Graph traversal (DFS and BFS) • Backtracking Algorithm &7Ciph er