SlideShare a Scribd company logo
ALGORITHM DESIGN:
OPTIMIZATION USING RECURSION
COMPUTER SCIENCE HL
NANJING FOREIGN LANGUAGE
FERRY KEMPERMAN
APRIK 2019
ALGORITHM DESIGN: THE THINKING PROCESS
• Algorithm design is designing a solution for a complex problem that is
……
• Non-ambiguous
• Executable
• Stepwise based
• Flow controlled
• Optimized for efficiency!
• What does this mean?
OPTIMIZED ALGORITHM DESIGN → EFFICIENCY
• An algorithm is considered designed well if
• 1. The algorithm works according to specs
• 2. The algorithm works as efficient as possible.
• Optimization is the process needed to design an
efficient algorithm.
WHAT IS AN EFFICIENT ALGORITHM EXACTLY?
• An efficient algorithm need to meet these three requirements:
• 1. A minimal number of instructions used to execute the algorithm
• 2. The minimal amount of RAM used during execution of the algorithm
• 3. The fastest retrieval of data and instructions from RAM to the CPU
possible.
• An optimized algorithm = an algorithm that is designed in such a way that
it meets these requirements.
• To put it simply: a non-optimized algorithm works correctly, but is slow
and resources heavy (CPU+RAM!).
TECHNIQUES TO DESIGN EFFICIENT ALGORITHMS
• So how to design an efficient algorithm?
• You create a flowchart or pseudocode as a design.
• How do you know this design is efficient and optimized?
• Well, how many instructions did you use? Can it be less?
• How does your algorithm store and retrieve data? Variables and
their datatypes? Can you make smarter choices here?
• Is the flow of your algorithm smartly designed? Do you make use
of repetitions (loops) and selection (if/case statements) in such a
way that the number of executed statements is minimized
LET’S COMPARE TWO DESIGNS………
Design 1: Using FOR loop
• DECLARE Factorial : INTEGER
• DECLARE Terms : INTEGER
• Factorial  0
• Terms  0
• INPUT “Specify N to calculate N!” Terms
• FOR Counter  1 TO Terms
• Factorial  Factorial * Counter
• ENDFOR
• OUTPUT “The factorial “ + Terms + “! = “ + Factorial
Design 2: Using WHILE loop
• DECLARE Factorial : INTEGER
• DECLARE Terms : INTEGER
• Factorial  0
• Terms  0
• Counter  0
• INPUT “Specify N to calculate N!” Terms
• WHILE Counter <= Terms
• Factorial  Factorial * Terms
• Counter  Counter + 1
• ENDWHILE
• OUTPUT “The factorial “ + Terms + “! = “ + Factorial
OR AN EVEN MORE EFFICIENT DESIGN…..USING
RECURSION!
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
LET’S BREAK IT DOWN
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
• On the right I have defined a function.
• A function is a top-down design concept that I am
going to explain in SL later on.
• A function is technically speaking a reusable code
and it follows the mathematical definition of a
function:
• Input → Function (process) → Output
• A program calls a function by its name and the
code inside the function is then executed.
• A function is comprised of four parts:
• Name → A unique identifier call the function
• Body → The indented code inside the function
that will be executed on function call
• Input parameter(s) → What values does the
function need to process?
• Return value → What is the output value of the
function?
• IMPORTANT: Parameters and return values have
FIXED datatypes.
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
Datatype
of return
value
Function
name
Input parameter
with datatype
integer: it expects
an integer!
Entire first
line: function
header
Function
body
HOW TO USE THE FUNCTION? THE FUNCTION CALL
• A function is simply used by referencing his name with the correct input.
• This is called a function call.
• Let’s take look at my earlier code:
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
The function is
called here by
this statement!
Let’s have a look
on how this
works….
Main program
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
Function definition
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS
INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
Execute the main.
User enters 3 to calculate 3!, variable Terms will contain 3.
Factorial  CalculateFactorial(3) // the function call
How to evaluate CalculateFactorial(3)?
Execute the body of the function with input parameter 3, so N=3 inside the function!
IF N=1, nope, so ELSE part executed….RETURN N * CalculateFactorial(N-1), but N=3 here.
So…..RETURN 3 * CalculateFactorial(3-1)……but the function is called again, by itself!!!! This is called recursion!
Let’s continue……how to evaluate CalculateFactorial(3-1)? Well, do the same. CalculateFactorial(2)…..
N=1, nope, so ELSE part….RETURN N+CalculateFactorial(N-1), with N=2 this time. RETURN 2*CalculateFactorial(1)….
And again…….CalculateFactorial(1), so N=1…then RETURN 1…….Which make 3*2*1 = 6, which is 3!, stored in
Factorial variable.
Algorithm Design: Optimization using recursion.
RECURSION, AN DESIGN STRATEGY
• Recursion is a design technique in which you design a function that calls itself.
• Every time a function calls itself we call this an incarnation.
• Recursive design is not easy! You need to recognize patterns that are recurring
to implement recursion.
• Recursion can optimize your algorithm’s efficiency significantly.
• No need for repetitive code.
• Recursion is not looping! What is the difference?
• Incarnation depth for recursion is sometimes limited depending on the
platform.
APPLICATIONS OF RECURSIVE DESIGN IN CS AND MATH:
FRACTALS
• FRACTALS: Patterns that repeat
• themselves on a smaller scale each
• time the function calls itself….
• A very famous one in math is
• The Mandelbrot Set in the picture.
• Koch curves, binary trees etc.
• Even….CS Art!!!

More Related Content

PPTX
Daa unit 1
PPTX
Recursion for GCE AS Computing
PPTX
Lecture2a algorithm
PPTX
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
PPT
User defined functions
PDF
Iteration, induction, and recursion
PPT
chapter1.ppt
PPT
Lec-6 Recursion of Data Structures & Algorithms
Daa unit 1
Recursion for GCE AS Computing
Lecture2a algorithm
ADA_Module 1_MN.pptx- Analysis and design of Algorithms
User defined functions
Iteration, induction, and recursion
chapter1.ppt
Lec-6 Recursion of Data Structures & Algorithms

Similar to Algorithm Design: Optimization using recursion. (20)

PPTX
DS Unit-1.pptx very easy to understand..
PPTX
Design and Analysis of Algorithms.pptx
PPTX
DAA-Unit1.pptx
PPTX
Lecture 7.pptx
PDF
Chapter VII RECURSION.pdf algor and data structure
PPTX
Introduction to Design and Analysis of Algorithms
PPT
Lecture9 recursion
PPT
M251_Meeting 9 (Recursion_AdvancedJava).ppt
PDF
062636636366363773737373733+73737733+7.pdf
PPT
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
PPT
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
PDF
BCS401 ADA First IA Test Question Bank.pdf
PPTX
Chp-1 DAA (2).pptx design analysis and algoritham presentation
PPT
17recursion
PPTX
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
PPT
data unit notes from department of computer science
PPT
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
1_Introduction.pptx
PPT
Pengenalan kepada pengaturcaraan berstruktur
DS Unit-1.pptx very easy to understand..
Design and Analysis of Algorithms.pptx
DAA-Unit1.pptx
Lecture 7.pptx
Chapter VII RECURSION.pdf algor and data structure
Introduction to Design and Analysis of Algorithms
Lecture9 recursion
M251_Meeting 9 (Recursion_AdvancedJava).ppt
062636636366363773737373733+73737733+7.pdf
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
daa_unit THIS IS GNDFJG SDGSGS SFDF .ppt
BCS401 ADA First IA Test Question Bank.pdf
Chp-1 DAA (2).pptx design analysis and algoritham presentation
17recursion
DESIGN AND ALGORITHM.pptx BCA BANGALORECITY UNIVERSITY
data unit notes from department of computer science
daaadafrhdncxfbfbgdngfmfhmhagshh_unit_i.ppt
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
1_Introduction.pptx
Pengenalan kepada pengaturcaraan berstruktur
Ad

Recently uploaded (20)

PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
history of c programming in notes for students .pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
L1 - Introduction to python Backend.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Migrate SBCGlobal Email to Yahoo Easily
Designing Intelligence for the Shop Floor.pdf
history of c programming in notes for students .pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
Computer Software and OS of computer science of grade 11.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Digital Systems & Binary Numbers (comprehensive )
Operating system designcfffgfgggggggvggggggggg
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Nekopoi APK 2025 free lastest update
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
VVF-Customer-Presentation2025-Ver1.9.pptx
L1 - Introduction to python Backend.pptx
Transform Your Business with a Software ERP System
PTS Company Brochure 2025 (1).pdf.......
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Ad

Algorithm Design: Optimization using recursion.

  • 1. ALGORITHM DESIGN: OPTIMIZATION USING RECURSION COMPUTER SCIENCE HL NANJING FOREIGN LANGUAGE FERRY KEMPERMAN APRIK 2019
  • 2. ALGORITHM DESIGN: THE THINKING PROCESS • Algorithm design is designing a solution for a complex problem that is …… • Non-ambiguous • Executable • Stepwise based • Flow controlled • Optimized for efficiency! • What does this mean?
  • 3. OPTIMIZED ALGORITHM DESIGN → EFFICIENCY • An algorithm is considered designed well if • 1. The algorithm works according to specs • 2. The algorithm works as efficient as possible. • Optimization is the process needed to design an efficient algorithm.
  • 4. WHAT IS AN EFFICIENT ALGORITHM EXACTLY? • An efficient algorithm need to meet these three requirements: • 1. A minimal number of instructions used to execute the algorithm • 2. The minimal amount of RAM used during execution of the algorithm • 3. The fastest retrieval of data and instructions from RAM to the CPU possible. • An optimized algorithm = an algorithm that is designed in such a way that it meets these requirements. • To put it simply: a non-optimized algorithm works correctly, but is slow and resources heavy (CPU+RAM!).
  • 5. TECHNIQUES TO DESIGN EFFICIENT ALGORITHMS • So how to design an efficient algorithm? • You create a flowchart or pseudocode as a design. • How do you know this design is efficient and optimized? • Well, how many instructions did you use? Can it be less? • How does your algorithm store and retrieve data? Variables and their datatypes? Can you make smarter choices here? • Is the flow of your algorithm smartly designed? Do you make use of repetitions (loops) and selection (if/case statements) in such a way that the number of executed statements is minimized
  • 6. LET’S COMPARE TWO DESIGNS……… Design 1: Using FOR loop • DECLARE Factorial : INTEGER • DECLARE Terms : INTEGER • Factorial  0 • Terms  0 • INPUT “Specify N to calculate N!” Terms • FOR Counter  1 TO Terms • Factorial  Factorial * Counter • ENDFOR • OUTPUT “The factorial “ + Terms + “! = “ + Factorial Design 2: Using WHILE loop • DECLARE Factorial : INTEGER • DECLARE Terms : INTEGER • Factorial  0 • Terms  0 • Counter  0 • INPUT “Specify N to calculate N!” Terms • WHILE Counter <= Terms • Factorial  Factorial * Terms • Counter  Counter + 1 • ENDWHILE • OUTPUT “The factorial “ + Terms + “! = “ + Factorial
  • 7. OR AN EVEN MORE EFFICIENT DESIGN…..USING RECURSION! • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION • DECLARE Terms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
  • 8. LET’S BREAK IT DOWN • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION • On the right I have defined a function. • A function is a top-down design concept that I am going to explain in SL later on. • A function is technically speaking a reusable code and it follows the mathematical definition of a function: • Input → Function (process) → Output • A program calls a function by its name and the code inside the function is then executed. • A function is comprised of four parts: • Name → A unique identifier call the function • Body → The indented code inside the function that will be executed on function call • Input parameter(s) → What values does the function need to process? • Return value → What is the output value of the function? • IMPORTANT: Parameters and return values have FIXED datatypes.
  • 9. • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION Datatype of return value Function name Input parameter with datatype integer: it expects an integer! Entire first line: function header Function body
  • 10. HOW TO USE THE FUNCTION? THE FUNCTION CALL • A function is simply used by referencing his name with the correct input. • This is called a function call. • Let’s take look at my earlier code: • DECLARE Terms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial The function is called here by this statement! Let’s have a look on how this works….
  • 11. Main program • DECLARE Terms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial Function definition • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION Execute the main. User enters 3 to calculate 3!, variable Terms will contain 3. Factorial  CalculateFactorial(3) // the function call How to evaluate CalculateFactorial(3)? Execute the body of the function with input parameter 3, so N=3 inside the function! IF N=1, nope, so ELSE part executed….RETURN N * CalculateFactorial(N-1), but N=3 here. So…..RETURN 3 * CalculateFactorial(3-1)……but the function is called again, by itself!!!! This is called recursion! Let’s continue……how to evaluate CalculateFactorial(3-1)? Well, do the same. CalculateFactorial(2)….. N=1, nope, so ELSE part….RETURN N+CalculateFactorial(N-1), with N=2 this time. RETURN 2*CalculateFactorial(1)…. And again…….CalculateFactorial(1), so N=1…then RETURN 1…….Which make 3*2*1 = 6, which is 3!, stored in Factorial variable.
  • 13. RECURSION, AN DESIGN STRATEGY • Recursion is a design technique in which you design a function that calls itself. • Every time a function calls itself we call this an incarnation. • Recursive design is not easy! You need to recognize patterns that are recurring to implement recursion. • Recursion can optimize your algorithm’s efficiency significantly. • No need for repetitive code. • Recursion is not looping! What is the difference? • Incarnation depth for recursion is sometimes limited depending on the platform.
  • 14. APPLICATIONS OF RECURSIVE DESIGN IN CS AND MATH: FRACTALS • FRACTALS: Patterns that repeat • themselves on a smaller scale each • time the function calls itself…. • A very famous one in math is • The Mandelbrot Set in the picture. • Koch curves, binary trees etc. • Even….CS Art!!!