Unit 1 · Basics of Computer Programming

1.1.1What is an algorithm?

What is an algorithm?

An algorithm is a finite, ordered set of clear steps that solves a problem or performs a task. A recipe, directions to a friend's house and the steps to withdraw money from an ATM are all algorithms.

A program is an algorithm written in a language a computer understands. Good programmers design the algorithm first and code afterwards.

Algorithm, program and code

These three words are related but they are not the same:

TermWhat it isExample
AlgorithmThe plan: the steps, written in any form a person can follow"Add the three marks and divide by 3"
ProgramThe algorithm written in a programming languageA Java class that reads three marks and prints the average
CodeThe actual text of the programdouble avg = (a + b + c) / 3.0;

One algorithm can be turned into many programs: the same steps can be written in Java, Python or C++. That is why programmers design the algorithm first. Once the logic is right, translating it into a language is the easy part.

Real-life example: a cake recipe is an algorithm. The ingredients are the input, the mixing and baking steps are the process, and the cake is the output. If the recipe says "bake until ready" without a time or a test, a beginner cannot follow it. That step is not definite.

Properties of a good algorithm

PropertyMeaning
InputZero or more values are supplied
OutputAt least one result is produced
DefinitenessEvery step is clear and unambiguous
FinitenessIt ends after a limited number of steps
EffectivenessEvery step is simple enough to be carried out
CorrectnessIt gives the right answer for all valid inputs

Try it: write the steps for making a cup of tea, then ask a friend to follow them exactly as written. Every step they cannot follow, or follow wrongly, is a missing or ambiguous step: the property of definiteness at work.

Each property explained

A list of steps is only a good algorithm when it has all six properties. Here is what each one means in practice:

A poor algorithm and a better one

Problem: work out the price to pay for a shop item after a discount.

Poor versionBetter version
1. Get the price.1. Start.
2. Take off the discount.2. Read price and discountPercent.
3. Show it.3. Set discount = price × discountPercent / 100.
4. Set finalPrice = price − discount.
5. Display finalPrice.
6. Stop.

The poor version is not definite: "take off the discount" does not say how the discount is worked out, and the discount is never read as input. The better version names every value, gives the formula and has a clear start and end.

Ways of writing an algorithm

The same algorithm can be written down in several forms. You will use all of them in this unit:

Remember

  • An algorithm is a plan; a program is that plan written in a programming language.
  • A good algorithm has input, output, definiteness, finiteness, effectiveness and correctness.
  • Always design and check the algorithm before you start coding.