Algorithm Design Flashcards
What are algorithms?
They are step-by-step procedures or methods used to solve specific problems or perform tasks.
It’s a problem solution that can be easily translated into a
programming language and then run on a computer
This requires careful planning and consideration
to ensure efficiency and accuracy
What are the 9 steps involved in constructing an algorithm?
- DEFINE THE PROBLEM:
- understand what you want to solve
- identify inputs, outputs & constraints/limitations - ANALYZE THE PROBLEM:
- examine the problems requirements/constraints in detail
- determine spec goals & objectives of the algorithm
- consider existing knowledge or solutions - DESIGN THE ALGORITHM:
- develop high-level plan to solve problem
- choose appropriate structures & algorithms - BREAK DOWN THE ALGORITHM:
- divide it into smaller, interconnected problems
- identify main steps required to solve each sub problem
- ensure each step is well-defined - SPECIFY INPUTS & OUTPUTS:
- define types and formats of inputs
- specify the expected outputs and formats
- consider exceptional scenarios - WRITE PSEUDOCODE OR FLOWCHART:
- visual representation (flowchart = show logical flow of a program)
- language-like representation (pseudocode = natural language and programming, simplified)
- express steps in a language-independent matter
- clearly communicate sequence - TEST & REFINE:
- implement the program in language of choice
- debug & test each stage
- test it with different inputs
- add print statements to track problems
- identify any issues/bugs - ANALYZER COMPLEXITY & OPTIMIZE:
- analyze the time & space complexity
- identify bottlenecks/inefficiencies
- optimize algorithm by revising/rethinking - DOCUMENT & MAINTAIN:
- document the algorithms design, implementation and usage instructions
- maintain clear/concise documentation for future reference
- consider version & collab tools to maintain algorithm updates
Give an example for designing and writing a simple program for converting decimal numbers to binary…
- UNDERSTAND THE PROBLEM:
- first divide the decimal number by 2 & record the remainder (0 or 1)
- second continue dividing quotient by 2 and recording remainders until quotient becomes 0
- lastly reverse order of the remainders to obtain binary number
DO EXAMPLE OF DECIMAL NUMBER 19 TO BINARY
- INPUTS, OUTPUTS, CONSTRAINTS?
- INPUTS: user inputs number to be converted to binary
- do we need to check if the input is valid? Input could be non-numeric.
- is there a valid range of input that the program is supposed to handle? Negative numbers?
= In our case, let’s keep it SIMPLE
- OUTPUTS: print a message & the resulting string to the screen
- what if the string is empty, “0” or negative? Do we need to consider this?
= In our case, let’s keep it SIMPLE - BREAK IT DOWN:
- break down complex into smaller, manageable components
= In our case = the algorithm is SIMPLE - WRITE PSEUDOCODE OR FLOWCHART:
- can be used as in-code documentation
Ex.
Input decimal from from user
Set binary to an empty string
while decimal is greater than 0:
Get the remainder of decimal divided by 2
Convert the remainder to a string
Prepend the remainder string to the left of binary
Divide decimal by 2 and assign the result back to decimal
Print binary as the binary representation
N