5.2 - Writing Clear Algorithms
What algorithms are and their key features
Algorithms are step-by-step sets of instructions designed to solve a specific problem or perform a task.
Characteristics of effective algorithms
- Clarity and ease of understanding - Algorithms must be straightforward so that another person can follow the steps in the correct order without confusion.
- Versatility in application - They can be used for various purposes, such as guiding safe evacuation from a building or operating complex machinery.
- Efficiency variations - Different algorithms might solve the same problem, but some are more efficient, completing the task in less time or with fewer resources.
These features ensure algorithms are practical tools for problem-solving across different contexts.
Representing computer algorithms with pseudocode
Pseudocode is a way to describe an algorithm using simple, structured language that resembles programming code but is not tied to any specific programming language. It focuses on the logic of the steps rather than strict syntax rules, making it accessible even without programming experience.
Benefits and uses of pseudocode
- Simplicity over syntax - Pseudocode outlines the algorithm's steps without the need for perfect code formatting, allowing focus on the process itself.
- Quick to create - It can be written rapidly and is easy to understand, which helps in planning before actual coding.
- Flexible conversion - Pseudocode can be translated into any programming language, serving as a bridge between an idea and a working program.
- Personal style - You can write it in your own way, as long as it remains clear and comprehensible to others.
Pseudocode is particularly useful in computational thinking, where the goal is to design solutions that computers can execute efficiently.
Examples of algorithms in pseudocode
To illustrate how algorithms work, consider the task of calculating the area of a triangle. The formula for this is area = (base × height) ÷ 2. Algorithms can be written in plain English or pseudocode, but pseudocode makes it easier to convert into a program.
Simple algorithm in plain English
- Take the base of the triangle.
- Take the height of the triangle.
- Multiply the base by the height.
- Divide the result by two.
- Show the answer.
This version is easy to follow but less structured for programming.
More structured algorithm in pseudocode
This pseudocode version reads like a basic program, using keywords like INPUT for receiving data and OUTPUT for displaying results. It maintains the same logic but is more adaptable for coding.
Both examples solve the problem effectively, but the pseudocode one is better suited for turning into actual code because it mimics programming structure.
Adapting algorithms for general problems
Algorithms designed for a specific problem can often be modified to handle a broader range of similar tasks. This process involves identifying patterns or common elements in the problems, allowing the algorithm to become more versatile.
To generalise algorithms, identify the core pattern or common elements in the problems or their solutions.
Examples of adapting algorithms
- Recipe adaptation - An algorithm for making a strawberry trifle can be generalised to create any fruit trifle. By recognising that the layers (fruit, custard, and whipped cream) are consistent except for the fruit type, you can replace "strawberry" with a variable for any fruit.
- Programming adaptation - An algorithm that adds up a fixed list of 5 numbers can be extended to sum any number of items. This is done by incorporating an iteration statement (a loop) to handle lists of varying sizes, making it more efficient for general use.