Home / GCSE · AQA / Computer Science / Algorithms

CS1Algorithms, Decomposition and Abstraction

3.1.1 Representing algorithms

What an algorithm is

AQA’s definition, word for word: an algorithm is a sequence of steps that can be followed to complete a task.

  • Sequence: the steps come in a set order, and the order matters. Pouring the milk before you have a cup doesn’t work.
  • Complete a task: an algorithm solves a problem and then finishes.
  • Not just for computers. A recipe, directions to a friend’s house and the rules for long division are all algorithms. A person can follow them without a computer.
  • Clear steps. Each step must mean one thing, so that anyone (or any computer) following the algorithm gets the same result.
Worked exampleWrite an algorithm, in numbered English steps, to find the larger of two numbers.
  1. 1. Get the first number. 2. Get the second number.the inputs come first
  2. 3. If the first number is bigger than the second, the answer is the first number.a decision
  3. 4. Otherwise, the answer is the second number. 5. Say the answer.the other outcome, then the output
Five steps in a fixed order. It works for any two numbers (if they are equal, either is the answer).
Exam tipLearn the definition exactly. Examiners want “a sequence of steps” and “to complete a task”. “A set of instructions” is too vague, and an algorithm isn’t “a program”.
Algorithm
A sequence of steps that can be followed to complete a task.
3.1.1 Representing algorithms

Algorithms and programs

AQA: a computer program is an implementation of an algorithm. An algorithm is not a computer program.

  • The algorithm is the method: the steps. The program is that method written in a programming language so that a computer can run it.
  • One algorithm can be written in many forms: English steps, a flowchart, pseudo-code, or program code in Python, C# or VB.NET. It is still the same algorithm.
  • Programmers usually design the algorithm first, then write the program.
AQA pseudo-code (the algorithm)Python (a program that implements it)
first ← USERINPUT
second ← USERINPUT
IF first > second THEN
   OUTPUT first
ELSE
   OUTPUT second
ENDIF
first = int(input("First number: "))
second = int(input("Second number: "))
if first > second:
    print(first)
else:
    print(second)
RememberThe pseudo-code and the Python above are the same algorithm. Only the Python is a program.
Computer program
An implementation of an algorithm in a programming language, so that a computer can carry it out.
3.1.1 Representing algorithms

Decomposition

AQA’s definition: decomposition means breaking a problem into a number of sub-problems, so that each sub-problem accomplishes an identifiable task, which might itself be further subdivided.

  • Start with the whole problem at the top. Split it into the main jobs. Split any job that is still too big.
  • Name each sub-problem by what it does: “Check balance”, “Take payment”. A box that says “Other stuff” isn’t an identifiable task.
  • Why decompose?
  • Small problems are easier to understand and solve than one big one.
  • Different programmers can work on different sub-problems at the same time.
  • Each part can be written and tested on its own.
  • Parts can be reused. “Log in” could be used by another app. Later, each sub-problem often becomes a subroutine.

A decomposition diagram: each box below is a sub-problem of the box above it

Exam tipIf you are asked to complete a decomposition diagram, each new box must be a task that belongs under the box it hangs from. Use a verb: “Check allergens”, not “Allergens”.
Decomposition
Breaking a problem into a number of sub-problems, so that each sub-problem accomplishes an identifiable task, which might itself be further subdivided.
Sub-problem
A smaller part of a problem that does one identifiable task.
3.1.1 Representing algorithms

Abstraction

AQA’s definition: abstraction is the process of removing unnecessary detail from a problem.

  • You keep only the details you need to solve the problem and leave the rest out.
  • A railway line map keeps the stations, their order and where lines meet. It removes streets, rivers, real distances and bends, because a passenger planning a journey doesn’t need them.
  • A football video game models the ball, the players and the score. It leaves out the length of each blade of grass and the faces of every fan.
  • A school’s program for lunch accounts stores each pupil’s name, account number and balance. It doesn’t store their favourite colour or shoe size.
  • Which details are “unnecessary” depends on the problem. A gardening app might need the length of the grass.
DecompositionAbstraction
What you doBreak the problem into smaller sub-problemsRemove unnecessary detail from the problem
ResultSeveral smaller problemsA simpler version of the problem

A line map is an abstraction of the real area

Exam tipSay “removing unnecessary detail from a problem”. Examiners have rejected “removing unnecessary code from a program” and “removing detail” on its own.
Abstraction
The process of removing unnecessary detail from a problem.
3.1.1 Representing algorithms

Writing algorithms in AQA pseudo-code

Pseudo-code is a way of writing an algorithm that looks like code but isn’t a real programming language. Every Paper 1 prints algorithms in AQA’s own style, so you must be able to read it.

AQA pseudo-codePythonWhat it does
total ← 0total = 0Assignment: store a value in a variable
name ← USERINPUTname = input("Name: ")Input from the user
OUTPUT nameprint(name)Output
IF … THEN … ELSE … ENDIFif …: else:Selection (a decision)
WHILE … ENDWHILEwhile …:Repeat while a condition is True
REPEAT … UNTIL …No direct equivalentRepeat until a condition is True
FOR i ← 1 TO 5 … ENDFORfor i in range(1, 6):Repeat a set number of times
= ≠ ≤ ≥== != <= >=Equal, not equal, at most, at least
DIV MOD// %Whole-number division, remainder
Worked exampleWrite an algorithm in pseudo-code that inputs five temperatures and outputs how many are below 0. Then write it in Python.
  1. count ← 0
    FOR i ← 1 TO 5
       temp ← USERINPUT
       IF temp < 0 THEN
          count ← count + 1
       ENDIF
    ENDFOR
    OUTPUT count
    AQA pseudo-code: ← for assignment, ENDIF and ENDFOR close each block
  2. count = 0
    for i in range(1, 6):
        temp = float(input("Temperature: "))
        if temp < 0:
            count = count + 1
    print(count)
    Python: range(1, 6) gives 1 to 5, and input() must be converted to a number
For the inputs 3, −1, 0, −4, 2 both output 2.
Exam tipWhen you answer in pseudo-code, any clear, consistent pseudo-code is accepted: it doesn’t have to be AQA’s style. When you answer in Python, write print(). OUTPUT in a Python answer can’t get the mark.
Pseudo-code
A way of writing an algorithm in structured, code-like statements that isn’t a real programming language.
3.1.1 Representing algorithms

Flowcharts

A flowchart shows an algorithm as a diagram. Each kind of step has its own symbol, and arrows show the order.

  • Every flowchart begins with one Start and ends with a Stop (terminators).
  • A decision diamond asks a Yes/No question. Label its two arrows Yes and No.
  • A loop is an arrow that goes back to an earlier step. There is no special loop symbol.
  • Inputs and outputs go in parallelograms. Calculations and assignments go in rectangles.

The flowchart symbols you need

This flowchart counts how many numbers are entered before a 0

Worked exampleTurn this description into a flowchart: “Input a test mark. If it is 40 or more, output Pass, otherwise output Retake.”
  1. Start (terminator) → Input mark (parallelogram)the input comes first
  2. Decision diamond: mark ≥ 40?a Yes/No question
  3. Yes → Output Pass. No → Output Retake (parallelograms)one output on each branch
  4. Both arrows join → Stopone Stop at the end
Five symbols plus the arrows: Start, one input, one decision, two outputs, Stop.
Exam tipIf a question gives you statements to place in a flowchart, use exactly those statements in the boxes. Don’t write your own pseudo-code instead.
Flowchart
A diagram that shows an algorithm using standard symbols joined by arrows.
Terminator
The rounded symbol that marks the Start or Stop of a flowchart.
Decision
The diamond symbol for a Yes/No question. Two arrows leave it.
3.1.1 Representing algorithms

Inputs, processing and outputs

You must be able to explain a simple algorithm in terms of its inputs, processing and outputs, and point to where each happens.

  • Input: data the algorithm receives. In pseudo-code, USERINPUT; in Python, input(). Data can also come from a sensor or be passed in as a parameter.
  • Processing: what the algorithm does with the data: calculations, comparisons, decisions and loops.
  • Output: the result it gives back. In pseudo-code, OUTPUT; in Python, print(). A value returned from a subroutine is also an output of that subroutine.

Input, processing and output in a car park charge algorithm

Worked exampleIdentify the inputs, processing and outputs in this algorithm.
hours ← USERINPUT
IF hours ≤ 2 THEN
   charge ← 3
ELSE
   charge ← 3 + (hours - 2) * 2
ENDIF
OUTPUT charge
  1. Input: hours ← USERINPUTthe only data that comes in
  2. Processing: the comparison hours ≤ 2 and the calculation 3 + (hours - 2) * 2work done on the data
  3. Output: OUTPUT chargethe result goes out
For 5 hours the output is 3 + 3 × 2 = 9.
Input
Data that an algorithm receives, e.g. typed by the user.
Processing
What an algorithm does with its data: calculations, comparisons, decisions and loops.
Output
A result that an algorithm gives, e.g. displayed on screen.
3.1.2 Efficiency of algorithms

Comparing the efficiency of algorithms

More than one algorithm can solve the same problem. AQA asks only about time efficiency: the algorithm that needs fewer steps for the same input takes less time, so it is more time efficient.

  • To compare, count the steps each algorithm needs: comparisons, calculations or loop passes.
  • You won’t be asked for formal measures of efficiency.
  • Having a loop doesn’t make an algorithm more efficient. Often it’s the other way round.
  • The count can depend on the input. Say which input you used.
Worked exampleTwo algorithms check whether a whole number n is a multiple of 10. A repeatedly subtracts 10 until n is below 10, then checks for 0. B checks whether n MOD 10 = 0. Compare them for n = 470.
  1. A: 470 → 460 → … → 0 is 47 subtractions, each with a check, then one final checkone loop pass per subtraction
  2. B: one calculation (470 MOD 10 = 0) and one comparisonno loop
  3. B needs far fewer steps for the same resultfewer steps, so less time
B is more time efficient. A’s steps also grow as n gets bigger; B’s don’t.
Exam tip“B has fewer steps” is only half an answer. Finish the thought: “…so B takes less time / is more time efficient.” Don’t just say “faster” or “more efficient” without a reason.
Time efficiency
How much time an algorithm takes to complete a task; fewer steps for the same input means more time efficient.
3.1.1 Representing algorithms

A systematic approach, and answering in the right form

AQA wants you to solve problems in a systematic way and to write algorithms as pseudo-code, program code or flowcharts. Every question tells you which form to use.

  • 1. Understand the problem: what are the inputs and the outputs?
  • 2. Decompose it into sub-problems.
  • 3. Abstract: keep only the details that matter.
  • 4. Design an algorithm for each part, as pseudo-code or a flowchart.
  • 5. Write the program code.
  • 6. Test it, e.g. with a trace table and test data.
The question says…Write…
“Your answer must be written in Python”Python only. Pseudo-code or another language won’t get full marks.
“using pseudo-code”Any clear, consistent pseudo-code (AQA’s style is fine, but not required)
“using either pseudo-code or a flowchart”Your choice of the two. Don’t do both.
“Complete the flowchart / program”Only the missing parts, using the statements given if there are any
Exam tipLong code questions come with a bullet list of what the program must do. Tick each bullet off as you write it: AQA’s examiners give that advice every year.
Systematic approach
Solving a problem in planned stages: understand, decompose, abstract, design, code, test.