Algorithms and Flowcharts
Learn how to describe a solution step by step, represent its logic visually, and evaluate whether it solves a problem effectively.
1. What Is an Algorithm?
An algorithm is a finite, ordered set of clear instructions used to solve a problem or complete a task.
Consider the everyday task of making a cup of tea. Its steps form a simple algorithm:
- Fill a kettle with water.
- Heat the water.
- Place a tea bag in a cup.
- Pour hot water into the cup.
- Wait for the tea to brew, then serve.
Characteristics of a good algorithm
- Input: It may accept zero or more inputs.
- Output: It produces at least one result.
- Definiteness: Every step is clear and unambiguous.
- Finiteness: It eventually terminates.
- Effectiveness: Its steps can actually be carried out.
2. Writing an Algorithm
A useful way to design an algorithm is to identify the input, decide the processing steps, and determine the expected output.
Example: Find the sum of two numbers
Algorithm: AddTwoNumbers 1. Start 2. Read A and B 3. Sum = A + B 4. Display Sum 5. Stop
Example input: A = 7, B = 5
Processing: Sum = 7 + 5 = 12
Output: 12
Example: Find the larger of two numbers
1. Start 2. Read A and B 3. If A > B, display A 4. Otherwise, if B > A, display B 5. Otherwise, display "Both are equal" 6. Stop
3. What Is Pseudocode?
Pseudocode expresses an algorithm in a structured, readable form that resembles programming code without following the exact syntax of any particular language.
INPUT marks
IF marks >= 40 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
END IF
Pseudocode helps you plan the logic before writing actual code.
Pseudocode versus a programming language
| Pseudocode | Programming language |
|---|---|
| Uses flexible, readable conventions. | Follows defined syntax and rules. |
| Usually cannot be executed directly. | Can be executed or compiled using appropriate tools. |
| Focuses on planning the solution. | Turns the solution into an executable program. |
4. What Is a Flowchart?
A flowchart is a visual representation of an algorithm. It uses standard symbols connected by arrows to show the order of operations and the direction of control.
Sum = A + B.
5. A Simple Flowchart in Text
This example checks whether a number is positive. The following text diagram shows the flow; it is a simplified representation rather than a drawing of the standard shapes.
[START]
|
[Input number]
|
[Number > 0?]
/ \
Yes No
| |
[Display Positive] [Display Not Positive]
\ /
|
[END]
A decision creates different possible paths. Both paths can later join again before the algorithm ends.
6. Three Fundamental Control Structures
Sequence
Instructions run one after another in a specified order.
Read length Read width area = length * width Display area
Selection
A condition determines which path the algorithm follows. Common
constructs include IF, ELSE, and
SWITCH.
IF temperature > 30 THEN
OUTPUT "Hot"
ELSE
OUTPUT "Not hot"
END IF
Iteration
A set of instructions repeats while a condition holds or for a
specified number of times. Loops include FOR,
WHILE, and REPEAT-UNTIL.
FOR number = 1 TO 5
OUTPUT number
END FOR
The output is 1, 2, 3, 4, 5.
7. Tracing an Algorithm
Tracing means following an algorithm step by step while keeping track of how its variables and output change.
total = 0
FOR i = 1 TO 4
total = total + i
END FOR
OUTPUT total
| Iteration | i | total after addition |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
Final output: 10
Tracing is especially useful when checking loops, conditions, and unexpected results.
8. Correctness and Efficiency
An algorithm should solve the intended problem correctly and use resources sensibly.
- Correctness: Does it produce the expected result for valid inputs?
- Time complexity: How does the amount of work grow as the input gets larger?
- Space complexity: How does its memory requirement grow?
Two common examples of time complexity are:
O(1): constant time; the number of operations does not grow with input size in the stated model.O(n): linear time; the work grows proportionally to the input size in the stated model.
9. Knowledge Check
Try answering each question before revealing the answer.
1. What is an algorithm?
2. Which flowchart symbol represents a decision?
3. Which control structure repeats instructions?
4. What does pseudocode help a programmer do?
5. What is the final value of total?
total = 2 total = total * 3 total = total + 4
Key Takeaways
- An algorithm describes a solution as clear, finite steps.
- Pseudocode expresses the logic in a readable, code-like form.
- Flowcharts represent the sequence and branching of operations visually.
- Sequence, selection, and iteration are fundamental control structures.
- Tracing helps verify logic; complexity helps describe resource growth.