Topic 9 · Notes

Algorithms and programs

These notes are part of a subscription

15 sections covering the whole specification for this topic, with worked examples.

Preview

Representing algorithms

An algorithm is a sequence of steps that solves a problem. It can be written in several ways before it is coded.

Pseudo-code looks like a program but isn't in any real language, so it can be read by anyone and turned into any language. The style used here:

total = 0
for i = 1 to 10
   input mark
   if mark >= 50 then
      total = total + 1
   end if
next i
output total

Exam papers use their own pseudo-code and explain any unusual parts. What matters is that each step is clear and unambiguous.

Flowcharts show an algorithm as a diagram:

SymbolShapeUsed for
TerminatorRounded rectangleStart and Stop
ProcessRectangleA calculation or assignment
DecisionDiamondA yes/no question, with two exits
Input/outputParallelogramInput or output
SubroutineRectangle with double sidesCalling a procedure or function
Flow lineArrowThe order of the steps

Structured English describes the steps in plain English, using a small set of words such as IF, THEN, ELSE, REPEAT, UNTIL, FOR EACH, with indentation to show structure. It is easiest for non-programmers to follow.

All three use the same three building blocks: sequence (steps in order), selection (if, case) and iteration (loops).

Also in these notes

  • Variables and constantsAS
  • Scope and lifetimeAS
  • Subroutines and parametersAS
  • Bubble sortAS
  • Insertion sortAS
  • Linear and binary searchAS
  • Dry-running and test dataAS
  • Compression algorithmsAS
  • RecursionA2
  • Validation and verification algorithmsA2
  • QuicksortA2
  • Comparing algorithms: Big O notationA2
  • Shortest path: Dijkstra's algorithmA2
  • Huffman coding and compression ratiosA2