Algorithms - EduqasTesting and evaluating algorithms

Algorithms are step-by-step plans for solving problems. They are a starting point when writing a program. Algorithms can be designed using pseudo-code and flowcharts.

Part ofComputer ScienceUnderstanding Computer Science

Testing and evaluating algorithms

Testing an algorithm

One way to test short is to do what is known as a using paper. A dry run involves creating a , containing all the variables a program contains. Whenever the value of a changes, the change is indicated in the trace table.

Consider this simple program:

1 total is integer
2 number is integer
3 set total = 0
4 for count = 1 to 3
5 input “Enter number”, number
6 total = total + number
7 next count
8 output total

Each instruction has been given a line number (1-8). The program has three variables - total, count and number. These variables will be put into a trace table.

Instructiontotalcountnumber
Instruction
total
count
number

Next, the program is tested using test data. If the numbers 5, 7 and 9 are input, the resulting total should be 21.

The instruction number is entered in the table. If a variable changes with that instruction, the new variable value is written in the appropriate box, as follows:

Instructiontotalcountnumber
1
2
30
41
55
65
7
42
57
612
7
43
59
621
7
821
Instruction1
total
count
number
Instruction2
total
count
number
Instruction3
total0
count
number
Instruction4
total
count1
number
Instruction5
total
count
number5
Instruction6
total5
count
number
Instruction7
total
count
number
Instruction4
total
count2
number
Instruction5
total
count
number7
Instruction6
total12
count
number
Instruction7
total
count
number
Instruction4
total
count3
number
Instruction5
total
count
number9
Instruction6
total21
count
number
Instruction7
total
count
number
Instruction8
total21
count
number

At each step, the programmer is able to see if, and how, a variable is affected.

Trace tables are extremely useful because they enable a programmer to compare what the value of each variable should be against what a program actually produces. Where the two differ is the point in the program where a logic error has occurred.

Evaluating an algorithm

When developing an it is useful to formalise exactly what that algorithm is designed to do. This makes it possible to check whether it meets all of those requirements. In addition, there might be several possible algorithms for the same problem. A good algorithm will not only do what it is supposed to do but will also do it efficiently. Inefficient algorithms may slow down the speed of the program execution. For example, a is less efficient than a .

Ways that algorithms may be faulty

Algorithms may fail because:

  • the original problem may not be fully understood
  • the algorithm is incomplete
  • the algorithm is inefficient and may be too complicated or too long
  • the algorithm does not meet the original design criteria, so it is not fit for purpose