Comparing algorithms
logical reasoningThe application of rules to problem solving. can be used to compare how effective and efficient different solutions are.
Look at this simple pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. 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 conditionIn computing, this is a statement or sum that is either true or false. A computation depends on whether a condition equates to true or false. 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 conditionIn computing, this is a statement or sum that is either true or false. A computation depends on whether a condition equates to true or false. 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.