0% found this document useful (0 votes)
24 views7 pages

Lec 6 - Recursion

Uploaded by

als000ninja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Lec 6 - Recursion

Uploaded by

als000ninja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Recursion

Recursion
• Recursion is a fundamental concept in computer science
and programming where a function calls itself (in its
body) to solve smaller instances of the same problem
until it reaches a base case.
• It's an approach that can simplify the implementation of
certain algorithms, particularly those that naturally fit a
divide-and-conquer strategy (i.e., problems that can be
divided into similar subproblems like binary search,
mergesort, finding factorial, etc.).
Recursion

This Photo by Unknown Author is licensed under


CC BY-NC
Key Components of Recursion
• Base Case: The condition under which the recursion
stops. It's the simplest instance of the problem which
can be solved directly without further recursion.
• Recursive Case: The part of the function where the
function calls itself with a modified argument, bringing it
closer to the base case.
How Recursion Works
• A recursive function breaks down complex problems
into simpler, self-referential subproblems.
• Each subproblem is solved by calling the same function
recursively.
• The process continues until a base case is reached,
preventing infinite recursion.
Advantages of Recursion
• Simplicity: Recursive solutions are often simpler
(require fewer lines of code) and more intuitive than
iterative ones.
• Ease of Use in Divide-and-Conquer: Problems that
can be divided into similar subproblems, such as sorting
algorithms (e.g., quicksort, mergesort), benefit from
recursive solutions.
• Natural Fit for Certain Data Structures: Recursion
is particularly useful for working with data structures
like trees and graphs.
Disadvantages of Recursion
• Stack/Memory Overhead: Each recursive call
consumes memory on the call stack. Deep recursion can
lead to stack overflow errors.
• Performance: Recursion can be slower due to function
calls and stack management.
• Complexity: Recursive code can be harder to debug.

You might also like