0% found this document useful (0 votes)
21 views

#3 Recursion, Loops, Stack

Uploaded by

Gian Dizon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

#3 Recursion, Loops, Stack

Uploaded by

Gian Dizon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Recursion,

Loops, Stack
Data Structures and Algorithms
Objectives:

• Explain the concept of recursion.


• Differentiate recursion from
loops.
• Describe the concept of a call
stack.
Recursion
Recursion

Recursion is a
programming
technique where a
function calls itself
within its own
definition.
Recursion

It is usually used to
solve problems that
can be broken down
into smaller instances
of the same problem
(divide and conquer).
Suppose there is a locked
suitcase. To open the
suitcase, you need to find the
key for its lock.
You have been informed that
the key is inside a box.
The box where the key is found,
however, contains many
smaller boxes. The key is inside
one of the smaller boxes.
How would you look for the
key?
You might try doing
something like this, a step-
by-step procedure to look
through each box.
You might also try something
like this.
Which approach seems
easier for you, the left or
the right?
This approach uses a
While Loop.

While the pile isn’t


empty, grab a box and
look through it.
If you write the
pseudocode for this
approach, it might look
like this…

Pseudocode is a high-level description of the


problem you’re trying to solve. It is written like
programming language code but it is closer to
human speech.
This one uses
recursion.

It is where a function
calls itself.
The pseudocode for this
approach might look
like this…

The function named look_for_key()


is called within itself.
Loops
Loops

In programming, loops allow us


to execute a statement or group
of statements multiple times.

A loop statement usually


requires a condition to be true
in order to execute.
Loops in Python
Loops in Python
Loop vs. Recursion

Both approaches
accomplish the same
thing, but recursion
may seem to be
clearer.
Loop vs. Recursion

Leigh Caldwell on Stack Overflow once said…


“Loops may achieve a performance gain
for your program. Recursion may achieve a
performance gain for your programmer.
Choose which is more important in your
situation!”
Loop vs. Recursion
Infinite Loops,
Base Case, and
Recursive Case
Infinite Loop

Because a recursive
function calls itself, it’s
easy to write a function
incorrectly that ends up
in an infinite loop.
An infinite loop is a
function that will keep If you execute an infinite loop in Python,
executing forever. you can kill the script with the keyboard
shortcut Ctrl - C
Base Case and Recursive Case

When writing a Every recursive function


recursive function, the (or even a loop) must have
program must be told two parts: a base case
when to stop and the recursive case.
executing or recursing.
Base Case and Recursive Case

The base case is The recursive case


when the function is when the function
doesn’t call itself calls itself.
again, so it does not
go into an infinite
loop.
Base Case and Recursive Case
Call Stack
Call Stack

A call stack (or just Under the LIFO


stack) is a linear data principle, the last
structure. element inserted to the
The elements are stack would be the first
to be deleted or
stored in a LIFO (last
removed.
in, first out) principle.
Working with a stack
usually involves two
LIFO actions: push and pop.

Push is the act of Pop is the act of


inserting a new item on removing the topmost
top of the stack. item.
Call Stack
Stack

A stack can be A stack can have either a


implemented in fixed size (usually in an
coding usually array) or have a sense of
through an array or dynamic resizing.
list.
Stack Data Structure
(tutorialspoint.com)

• Basic Operations on
Stacks
o Insertion: push()
o Deletion: pop()
Hands-On Demo
Recursion and Stacks

When working with This part of the


multiple containers, lesson will revisit
stacks are usually the pile of boxes
used. Stacks play a scenario to find the
big part in recursion. key.
Recursion and Stacks

Using this approach, you make a pile of boxes to


search through, so you always know what boxes
you still need to search.
Recursion and Stacks

In the recursive approach, there is no pile of boxes. If


there is no pile, how do you know what boxes you need
to look through?
Using the stack is
The “pile of boxes” convenient because
is saved on the you don’t have to keep
stack. track of the pile of
boxes yourself – the
stack does it for you (at
the cost of memory).
Each function call takes up memory. When the stack is too
tall, that means the computer is saving information for many
function calls.
References

Bhargava, A. (2016). Grokking Algorithms – An illustrated guide for


programmers and other curious people.
GeeksforGeeks. (2024). Learn Data Structures and Algorithms DSA
Tutorial. https://www.geeksforgeeks.org/learn-data-structures-and-
algorithms-dsa-tutorial/#dsa-full-form
TutorialsPoint. (nd). Data Structures and Algorithms (DSA) Tutorial.
https://www.tutorialspoint.com/data_structures_algorithms/index.htm

You might also like