Information Technology Topic
What an Algorithm Is
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- An algorithm is a clearly specified, step-by-step set of instructions that, if followed, gives the result you want: it takes input, processes it and gives output
- The characteristics to learn are: a finite number of steps, precise steps, unambiguous steps, a clear flow of control from one process to the next, and it terminates
- To test an algorithm against those characteristics, hunt for vague words, steps no one could carry out, unclear next steps and loops that can never stop
Section 1What an algorithm is
An algorithm is a set of steps that, if you follow them exactly, gets you a result you want. The steps are written clearly enough that a person or a computer can carry them out without guessing. You already use algorithms. A recipe for sorrel drink, the steps for topping up phone credit, and directions for getting from Half Way Tree to Cross Roads are all lists of steps that end in a result. A computer cannot guess what you mean, so before anyone writes a program, the steps are worked out first as an algorithm. The algorithm is the plan. The program is that plan written in a language the computer can run.
Section 2Input, process and output
Most algorithms take in some data (the input), work on it (the process) and give back a result (the output). Making change in a Mandeville shop shows the pattern. Input: the price and the money handed over. Process: subtract the price from the money. Output: the change. Here's the trick: use input, process, output as a quick completeness check. Does the algorithm get the data it needs? Does it do the work? Does it show the answer? An algorithm that works out the change and never displays it has no output, so nobody sees a result.
Section 3A finite number of steps, and terminating
Finite means the list of steps is limited. You can count the instructions and there is a last one. 'Keep adding numbers for ever' has no last step. Terminate means that when the algorithm is run, it stops. A short list of steps can still fail to stop if a step sends you round a loop and the loop never ends. Easy to mix up: finite is about the written list. Terminates is about what happens when it runs. This algorithm is only four lines long, yet it never terminates, because count never changes and so the test is always true: Store 1 in count While count <= 3 Display "Hello" End While Here While means 'repeat the lines below while the test is true' and End While marks the end of the loop (the loops topic explains it fully). Add one line inside the loop, Store count + 1 in count, and the loop shows Hello three times and then stops. Here's the trick: whenever there is a loop, ask what changes inside it to make the test fail.
Section 4Precise and unambiguous
Precise means each step says exactly what to do, with exact values and exact conditions. 'Add some sugar' is not precise. 'Add 2 cups of sugar' is. Unambiguous means a step can only be understood one way. 'Divide the total by the number and the cost' could mean two different things. 'Divide the total by the number of items' cannot. If two people could follow the same step and get different results, the step is ambiguous. A step must also be something that can actually be done as written. 'Think of a good answer' is not an instruction a computer can carry out. Watch out: vague words such as some, enough, high, soon and a bit make a step imprecise. Swap them for a value or a test, for example 'if the total is more than 5000'.
Section 5Flow of control
Flow of control is the order in which the steps are carried out, and how control passes from one step (one process) to the next. Usually the steps run one after the other. Some algorithms test a condition and take one path or another, and some go back and repeat steps. A clear algorithm makes the flow obvious. You always know which step comes next, and you never reach a point with no next step, or with two different next steps at once. In a flowchart the flow is the arrows. In pseudocode it is the order of the lines, and how If and loop statements are laid out. For example, a bus-fare algorithm reads the age, then reaches a decision, then takes one of two paths to display a fare. Control passes from one process to the next, and the decision chooses which one.
Section 6Algorithm, program, and ways to show one
An algorithm says what to do and in what order. It does not belong to any language. A program is one version of an algorithm written in a programming language such as Pascal, and the same algorithm can be written as programs in different languages. Three usual ways to write an algorithm down are: numbered steps in plain English, a flowchart (steps drawn as shapes joined by arrows) and pseudocode (English-like statements such as Read, Store and Display). The syllabus asks for flowcharts and pseudocode, and the next three topics teach them. Easy to mix up: pseudocode is not a program. No computer can run it. Its job is to plan the steps and to explain them to other people.
Section 7Testing an algorithm against the characteristics
Ask five questions. Is there a last step (finite)? Will it always stop (terminates)? Is every step exact (precise)? Can any step be read two ways (unambiguous)? Can I always tell which step is next (flow of control)? Try it on: Read the mark. If the mark is high, display Pass, otherwise display Fail. The word 'high' fails the precise test, because nobody knows how high. Write 'If the mark is 50 or more'. Now try this one: Store 5 in n While n > 0 Display n Store n - 1 in n End While It displays 5, 4, 3, 2, 1 and stops, because n falls by 1 each time and eventually n > 0 is false. Every step is exact, the flow is clear, the list is finite and it terminates.
Examples
A shop in Mandeville uses this algorithm to work out change: Read price Read paid Store paid - price in change Display change (a) A customer's bill is 680 and the customer pays with 1000. State what is displayed. (b) Show that the algorithm has each of the characteristics: a finite number of steps, precise, unambiguous, a clear flow of control, and it terminates. (c) A colleague rewrites step 3 as 'Work out what the customer should get back'. State which characteristics this step breaks.
(a) Read price stores 680 and Read paid stores 1000. Then change = 1000 - 680 = 320, so 320 is displayed. (b) Finite: there are 4 steps and a last one. Precise: each step names exact values and an exact calculation. Unambiguous: paid - price can only be read one way. Flow of control: the steps run in order, one after another, with no choices or loops. Terminates: after Display change there is nothing left to do, so it stops. (c) 'Work out what the customer should get back' is not precise, because it does not say what to calculate, and it is ambiguous, because two people could work it out in different ways. It is not something a computer can carry out as written. The original step, Store paid - price in change, is exact.
An algorithm is meant to display the odd numbers below 10: Store 1 in n While n <> 10 Display n Store n + 2 in n End While (a) Write the first six values displayed. (b) State which characteristic of an algorithm this one fails and explain why. (c) Change the test so that it terminates, and state the output. (d) Give a different fix that keeps the test n <> 10 and state the output.
(a) n starts at 1 and goes up by 2 each pass. The values displayed are 1, 3, 5, 7, 9 and 11. (b) It does not terminate. n takes only odd values, so it jumps from 9 to 11 and never equals 10. The test n <> 10 is therefore always true and the loop never ends, even though the list of steps is finite. (c) Use While n < 10. When n reaches 11 the test is false and the loop stops. The output is 1, 3, 5, 7, 9. (d) Start n at an even number, for example Store 2 in n. Then n goes 2, 4, 6, 8, 10. When n is 10 the test n <> 10 is false and the loop stops, so the output is 2, 4, 6, 8. The lesson: the update must be able to make the test fail.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 2, Specific Objective 5 · Syllabus scope
- Dictionary of Algorithms and Data Structures (P. E. Black, ed.), entries algorithm and linear search · National Institute of Standards and Technology (NIST) · Page algorithm, Dictionary of Algorithms and Data Structures, checked against (https://xlinux.nist.gov/dads/HTML/algorithm.html) · Facts
- NIST CSRC Glossary entries: information, database · National Institute of Standards and Technology (NIST) · Page algorithm, NIST glossary entry, checked against (https://csrc.nist.gov/glossary/term/algorithm) · Facts
- The Python Tutorial (sections 3, 4, 8) and The Python Standard Library, Built-in Types (version 3.15 pages) · Python Software Foundation · Section 3.2, First steps towards programming (a loop ends when its condition becomes false), checked against (https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming) · Facts