Introduction to Algorithms
What is an algorithm?
- An algorithm is a set of precise instructions, that if strictly followed, will result in the solution to a problem
- Algorithms are particularly useful for programming computers
- computers can process huge amounts of data and perform millions of calculations in very short time frames
- Robots would be programmed to follow an algorithm in order to complete a task
- e.g. a robot vacuum cleaner or lawn mower
- Algorithms can be performed by human beings too!
- e.g. the recipe and cooking instructions for a cake, the instructions to build a Lego set
- Algorithms are particularly useful for programming computers
What does an algorithm look like?
- Algorithms may be presented in a variety of ways
- A list of instructions, in order, written in words/text
- Pseudocode - this is a mixture of text and very basic computer commands/code
- let statements, if statements and loops are common but easy enough to follow without any knowledge of a formal computer programming language
- Flow charts are a visual way of presenting the steps of an algorithm
- they clearly show the order of instructions and any parts of an algorithm that may need repetition
What else do I need to know about algorithms?
- Some algorithms do not always provide an optimal (best) solution
- but will give a solution that is sufficient for the purpose
- For example, a band touring the UK may use an algorithm to find the shortest route between the different cities/venues to minimise their travelling expenses
- if the algorithm is inaccurate by say, 200 miles, it is not going to make much difference overall - the band will be travelling thousands of miles in total so 200 miles is relatively little
- In modern, complicated real-life situations a compromise between the speed and efficiency of an algorithm and the accuracy of the solution it provides is often needed
- When using an algorithm with a small amount of data - as exam questions will do due to time restraints - it is tempting to use common sense, intuition and basic mathematical skills to 'see' the solution
- to show understanding of how an algorithm works it is crucial to stay in 'robot mode'
- i.e. follow the algorithm precisely and accurately without taking any 'shortcuts' (however obvious they may seem)
- to show understanding of how an algorithm works it is crucial to stay in 'robot mode'
Examiner Tip
- Showing that you have followed an algorithm precisely includes
- knowing, or using checks built into the algorithm to know, that an algorithm is complete
- stating (or 'printing') any 'output' at the end if that is what an algorithm instruction requires
- do not 'assume' the output is the last number in your working