Algorithms - Computing and ICT in a Nutshell

Download Report

Transcript Algorithms - Computing and ICT in a Nutshell

Algorithms
Today we will look at:
• what the word algorithm means
• some example algorithms for simple tasks
• different ways of writing down algorithms
• why it’s useful to think about this!
What is an Algorithm?
• Algorithm is just the name we give to a
step-by-step procedure for doing something
– like a recipe or a set of instructions.
• It takes its name from Abū ʿAbdallāh
Muḥammad ibn Mūsā al-Khwārizmī, a
Persian mathematician who also gave use
the words algorism (using place value) and
algebra.
• But don’t panic – it’s easier than algebra!
In fact, an algorithm is just words.
Algorithm Example
• We can create an algorithm for anything. The algorithm
for making a cup of tea, for example, might look like this:
1. Fill kettle with water
2. Switch on kettle
3. Put tea in teapot
4. Wait
5. If kettle has boiled pour water into teapot, else go back to step 4
6. Put milk in cup
7. Pour tea into cup
• All you need to do is follow the steps in order.
• Notice that step 5 involves making a decision.
Start
Flowcharts
Fill the
kettle with
water
• Algorithms can also be described in
the form of a diagram called a
flowchart.
• Different stages have different shaped
boxes:
– a rounded one for the beginning and
end
– a square one for a simple instruction
– a parallelogram (slanted box) for input
and output
– a diamond for a decision.
• The flowchart for making a cup of tea
would look like the one on the right:
Switch on the
kettle
Put the
tea in the
teapot
Wait
No
Has the
kettle
boiled?
Yes
Put milk in
the cups
Pour the
tea!
Stop
Planning a Task
• There isn’t necessarily only one correct algorithm for a
particular task...
– for example, it might not matter if you add the sugar or the
milk first when you make the tea. Or you could put the tea in
the teapot before you switch on the kettle – it wouldn’t really
matter.
• But... some of the steps might need to be done before
others...
– for example, your tea wouldn’t be very nice if you poured the
water into the tea pot before you boiled it, and it probably
wouldn’t be a good idea to switch on the kettle before you
put the water in.
• For some tasks – e.g. sorting things - there might be some
ways of doing them that is quicker (or more efficient)
Why Are We Doing This?
Nearly everything you do requires an algorithm:
• everyday tasks, such as cooking and cleaning
• mathematical calculations
• Internet searches
• sorting
• navigation (we have already looked at an
algorithm for doing this)
The main reason we are thinking about this, though,
is because it helps us to...
• write a computer program
Computer Programming
• A computer program is a sequence of
commands used to control the computer; to tell
it what to do.
• For the program to run efficiently, it needs to
be planned carefully.
• The Golden Rule of programming is "don't rush
straight to the computer"; you need to first think
about what you are going to do.
• The first stage in planning a computer program
is to break job into small steps and write them
out in plain English – i.e. we need an algorithm.
Planning a Program
• You need to decide on the steps that your program
will perform before you start to create it
• You need to decide how the program will start and
end, and also whether anything will need repeating:
– a fixed number of times
– until a particular event happens – e.g. until a key is
pressed, or for a fixed amount of time
– while a particular variable has a particular value – e.g.
keep asking for a password until a password has been
entered
– forever – e.g. traffic lights
• Your program might need to make a decision
Example
Imagine you were going to write the program to
do the National Lottery “lucky dip” – what would
it need to do?
1. Pick a whole number from 1 – 49
2. Check that the number hasn’t been drawn
already – if it has, then go back to step 1
3. Repeat steps 1 and 2 until six different
numbers have been drawn
4. Sort the numbers into order