What is an Algorithm?
An algorithm is a step-by-step set of instructions to solve a problem or complete a task. Algorithms are the foundation of all computer programs.
Real-life example: A recipe is an algorithm. It has a sequence of steps, decisions ("if the sauce is too thick, add water"), and repetition ("stir for 5 minutes").
Properties of Good Algorithms
- Clear - Unambiguous instructions that can be followed
- Finite - Must eventually end (not run forever)
- Effective - Each step can actually be performed
- Input - Takes zero or more inputs
- Output - Produces at least one result
Control Structures
All algorithms are built using three fundamental control structures:
Sequence
Steps executed one after another, in order.
Step 2
Step 3
Selection
Making decisions based on conditions.
do this
ELSE
do that
Iteration
Repeating steps (loops).
do something
ENDWHILE
Flowcharts
A flowchart is a visual representation of an algorithm using standard symbols.
Flowchart Symbols
| Symbol | Name | Purpose |
|---|---|---|
| ○ | Terminator | Start or End of the algorithm |
| □ | Process | An action or calculation |
| ◇ | Decision | Yes/No question (branching) |
| ▱ | Input/Output | Getting input or showing output |
| ● | Connector | Links parts of flowchart |
| → | Flow Line | Shows direction of flow |
Flowchart Example
Algorithm to check if a number is positive, negative, or zero:
Pseudocode
Pseudocode is a way to describe an algorithm using structured English. It's not a real programming language but is easy to translate into code.
Pseudocode Conventions
- Keywords in CAPITALS: IF, THEN, ELSE, WHILE, FOR, INPUT, OUTPUT
- Use indentation to show structure
- One instruction per line
- Use meaningful variable names
Selection in Pseudocode
// Simple IF
IF age >= 18 THEN
OUTPUT "You can vote"
ENDIF
// IF-ELSE
IF temperature > 30 THEN
OUTPUT "It's hot!"
ELSE
OUTPUT "It's comfortable"
ENDIF
// IF-ELSEIF-ELSE
IF score >= 80 THEN
grade = "A"
ELSEIF score >= 60 THEN
grade = "B"
ELSEIF score >= 40 THEN
grade = "C"
ELSE
grade = "D"
ENDIF Iteration in Pseudocode
// WHILE loop (condition checked first)
count = 0
WHILE count < 5
OUTPUT count
count = count + 1
ENDWHILE
// FOR loop (known number of iterations)
FOR i = 1 TO 10
OUTPUT i
NEXT i
// FOR EACH (iterate through a collection)
FOR EACH item IN shoppingList
OUTPUT item
NEXT item Example: Finding the Maximum
Algorithm to find the largest number in a list:
BEGIN FindMaximum
INPUT numbers[]
max = numbers[0]
FOR EACH num IN numbers
IF num > max THEN
max = num
ENDIF
NEXT num
OUTPUT "The maximum is: " + max
END Trace Tables
A trace table tracks how variable values change as an algorithm executes. It helps verify that an algorithm works correctly.
Trace table for counting from 1 to 3:
| Step | count | count < 4 | Output |
|---|---|---|---|
| 1 | 1 | TRUE | 1 |
| 2 | 2 | TRUE | 2 |
| 3 | 3 | TRUE | 3 |
| 4 | 4 | FALSE | - |
Key Terminology
- Algorithm - Step-by-step instructions to solve a problem
- Flowchart - Visual representation of an algorithm
- Pseudocode - Structured English description of an algorithm
- Sequence - Steps executed in order
- Selection - Making decisions (IF statements)
- Iteration - Repeating steps (loops)
- Trace table - Tracking variable values through execution