๐ฏ By the end of this lesson you canโฆ
- Define an algorithm in simple words
- List the features of a good algorithm
- Recognise algorithms in everyday life
- Write a simple algorithm as ordered steps
A method you can follow
An algorithm is a step-by-step method for solving a problem. If youโve ever followed directions to a friendโs house, youโve followed an algorithm!
A clear, step-by-step method for solving a problem that always finishes.
1BEGIN2 TAKE A, B3 SET C = A + B4 DISPLAY C5END๐ A recipe: the same steps, in the same order, give the same cake every time.
What makes a good algorithm?
- โ Clear โ every step means exactly one thing.
- โ Ordered โ the steps happen in a definite sequence.
- โ Finite โ it always finishes; it doesnโt go on forever.
- โ Correct โ it actually solves the problem.
- โ Has inputs and outputs โ it takes something in and gives a result.
Program vs algorithm
An algorithm is the idea โ the method. A program is that method written for a computer. The same algorithm can be written as pseudocode, PHP or JavaScript โ itโs still the same algorithm.
1BEGIN2 TAKE A, B3 SET SUM = A + B4 SET AVERAGE = SUM / 25 DISPLAY AVERAGE6END
Run the program in your head, one line at a time. Fill every empty box, then press Check.
1BEGIN2 SET A = 83 SET B = 44 SET SUM = A + B5 SET AVERAGE = SUM / 26 DISPLAY AVERAGE7END
One problem, many algorithms
Say you want to find a word in a dictionary. Algorithm 1: start at page 1 and check every page. Algorithm 2: open near the middle, see if your word is before or after, and keep halving. Both work โ but algorithm 2 is much faster. Later in the course youโll learn to compare algorithms like this.
Check your understanding
1BEGIN2 SET A = 73 SET B = 54 SET SUM = A + B5 SET AVERAGE = SUM / 26 DISPLAY AVERAGE7END
1BEGIN2 SET A = 103 SET B = 204 SET SUM = A + B5 SET AVERAGE = SUM / 36 DISPLAY AVERAGE7END
Write an algorithm that TAKEs the WIDTH and HEIGHT of a rectangle and DISPLAYs its perimeter (all four sides added together).
๐ Key takeaways
- An algorithm is a precise, step-by-step method for solving a problem.
- Good algorithms are clear, ordered, finite and give the right result.
- The same problem can have several algorithms โ some better than others.
- A program is an algorithm written so a computer can run it.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.