Logical reasoningComparing algorithms

There is almost always more than one solution to a problem. Logical reasoning is used to predict the outcomes of the algorithms that are designed to solve a problem, to help select the best solution.

Part ofComputer ScienceAlgorithms

Comparing algorithms

can be used to compare how effective and efficient different solutions are.

Look at this simple for a cinema’s ticketing system. It works out if you are young enough to get a discount:

OUTPUT 'How old are you?'
INPUT User inputs their age
STORE the user's input in the age variable
IF age < 17 THEN OUTPUT 'You qualify for the student discount!'
ELSE IF age > 17 THEN OUTPUT 'You're too old for the student discount!'
ELSE IF age = 17 THEN OUTPUT 'You qualify for the student discount!'

There are three in this algorithm. The first checks if age is less than 17. The second checks if age is greater than 17. The third checks if age is equal to 17.

Logically, it is clear that a person’s age could be greater than 17, less than 17 or could actually be 17. From this we use these values in our algorithm to make predictions:

  • if age is less than 17, the algorithm will output ‘You qualify for the student discount!’
  • if age is greater than 17, the algorithm will output ‘You’re too old for the student discount!’
  • if age is equal to 17, the algorithm will output ‘You qualify for the student discount!’

The predictions show that this algorithm will work, because logically all possibilities have been covered.

The following is a different solution:

OUTPUT 'How old are you?'
INPUT user inputs their age
STORE the user's input in the age variable
IF age <= 17 THEN OUTPUT 'You qualify for the student discount!'
ELSE OUTPUT 'You are too old for the student discount.'

There is just one in this algorithm. It checks if age is less than or equal to 17.

Logically, it is clear that a person’s age could be greater than 17, less than 17 or could actually be 17. From this we use these values in our algorithm to make predictions:

  • if age is less than or equal to 17, the algorithm will output ‘You qualify for the student discount!’
  • otherwise (ie if age is greater than 17) it will output ‘You are too old for the student discount!’

Both algorithms work well. However, if we look at them both we can see that the first algorithm contains three rules (conditions), but the second contains just one.

The second algorithm is shorter, more efficient and easier to program and, therefore, logically would be a better choice.