1 - Problem Solving
1 - Problem Solving
net
1 – Problem Solving
Specification Coverage
Chapter 6 (Programming) will cover the following specification content from topic
1:
• 1.1.2 benefits of subprograms
• 1.2.2 variables, constants, strings, records and arrays
• 1.2.3 arithmetic, relational and logical operators
• 1.2.5 programming errors
Python Files
Where Python has been used in examples, there will be two icons which are both
buttons that link to the original Python code. One icon is for the Python code in
Repl.it and the other is for the Python (.py) file:
Some activities will also include these buttons. Not all activities include the buttons
as running the Python code for those activities would make answering the question
too easy. Where answers are provided using Python, many of the answer
documents (both activity answers and exam style question answers) will include
links to Python files that include answers in Python code, and often a step-by-step
run through of the code. The icons used for answers are:
These answer files can be used by the teacher to explain the answers to students
after they have completed the tasks, or by students once they have completed
the tasks.
PRIMM
Flowcharts
Where flowcharts have been used there will be an icon which will open
the file created in draw.io
Visualising Variables
Use the trace variables visualisation tool at http://tiny.cc/tracevariables
to watch what happens to the value of the variables as each line is
executed. You can input any code into the tool. If inputs are required,
then add these, including if enter needs to be input.
Contents
1) Representing algorithms
Algorithms
An algorithm is a sequence of clear instructions that are used to solve a problem
step by step.
Video
Watch the video about algorithms at http://tiny.cc/bbcalgorithms from BBC
Teach. Write down 4 things you have found out about algorithms. Share these
with your class.
Instructions must be unambiguous which means they must not be open to more
than one interpretation. The instructions must be clear and easily understood. For
example, instruction number 5 in the example above clearly states that the bread
must be placed vertically into the toaster. If the word vertically had not been
used, then somebody following the algorithm could try to squash the bread in
horizontally.
Video
Watch the video about evaluating algorithms at http://tiny.cc/evalalgorithms
from BBC Teach. Identify aspects of the video that demonstrate how an algorithm
may be ambiguous, incomplete, inaccurate, inconsistent or inefficient.
Instructions must be accurate. If there are any errors, then the algorithm will not be
successful. In the example above, instruction number 4 specifies one slice of
bread and instruction number 2 refers to a bag of bread. These are specific
instructions that ensure the algorithm is run correctly. They also help to make sure
the algorithm is unambiguous.
Algorithms should always be consistent. This means that they will be run the same
every time and produce the same result every time for the same input data. The
algorithm above should work for any piece of bread being used to make toast,
but may not work for a different type of toaster.
Video
Watch how these algorithms for making a jam sandwich at
http://tiny.cc/ambiguous from Philip Bagge go wrong due to ambiguous,
incomplete or inaccurate instructions. Identify the errors that the children make
with their instructions.
You can see that there are plenty of opportunities for ambiguity where the
instructions are not clear and may be interpreted differently by different people.
Extension: Read the example of Euclid’s Algorithm for finding the greatest
(largest) common factor at http://tiny.cc/euclids
Decomposition
Whole Solution
Video
Watch the decomposition video http://tiny.cc/decompose from BBC Teach.
Make a list of the smaller problems involved in going shopping that you see during
the video.
Next think about the smaller solutions that need to take place:
Getting equipment
Get toothbrush
Get toothpaste
Find water
Putting toothpaste on toothbrush
Clean toothbrush
Squeeze toothpaste
Cleaning teeth
Scrubbing teeth
Spitting out toothpaste
Swilling mouth with water
Spitting out water
Putting equipment away
Put toothbrush away
Put toothpaste away
Clean Teeth
Swill mouth
Find water
with water
Some of these solutions can be re-used. Spitting out toothpaste and spitting out
water will include the same actions. Finding water could be used to solve part of
another problem too.
✓ each smaller problem is a smaller problem that is easier to solve than the
whole problem.
✓ each smaller problem can be solved independently of the whole problem.
✓ each smaller algorithm can be tested independently.
✓ the algorithms for the smaller problems can be combined to solve the whole
problem.
✓ some smaller algorithms could be repeated within the solution for the whole
problem.
✓ some smaller algorithms could be re-used to help solve other problems.
✓ different people can work on solving different smaller problems.
Activity – decomposition
1) Use Google Maps to find cycling directions from Dragonfly Pool in
Birmingham to Little Aston Golf Club. Notice how you are presented with
different options. Click on Details for the option that goes through Sutton
Coldfield and Sutton Park. Notice how the problem has been decomposed.
2) Using a map, but without using a route finder, write down the directions to
drive, walk or cycle from your school to home.
Abstraction
Abstraction is the process of making a complex problem easier to understand by
focusing only on necessary information. It is important to identify which information
is necessary and ignore the information that is not necessary. Edexcel describe
abstraction as:
Video
Watch this video http://tiny.cc/abstraction about abstraction from Robotics
Academy.
Abstraction involves:
If the information above was included then the map would be far too
complicated to read and understand. The image below is an original
underground map from 1908.
In this map, the distances between stations are represented to scale and you can
see how difficult it is to fit in all the detail for stations in the centre of London. As a
result, some of the stations at the ends of the lines have been omitted.
In 1933, Harry Beck designed the modern tube map which ignored detail such as
the distances between stations, streets, places of interest and the true direction of
travel. This was a method of abstraction to make the map easier to understand.
Have a look at the animation between the abstracted tube map and the physical
tube map at http://tiny.cc/tubeanimation or use the file London Underground
Map Extraction.mp4
Video
Watch this video http://tiny.cc/tubemap about how Harry Beck designed the
modern underground map from Jill Britton.
4) Zoom back in and add Pegman (the little yellow man used for Streetview) to
Borough Road outside the destination.
a) What additional features are now shown?
b) Which of these could help in finding the destination?
c) Why wouldn’t Street View be useful for the whole journey?
5) Look at the underground map in the example above. The Piccadilly Line
(dark blue) is closed. An able-bodied person needs to travel from Holburn to
Green Park.
a) What information is needed?
b) What information can be ignored?
Video
Watch http://tiny.cc/abstraction2 about how abstraction relates to computer
algorithms from BBC Teach.
What is the answer to the problem? How did you work it out?
Six frogs are sitting on rocks. Frogs can jump from one rock to another adjacent
empty rock, or they can jump over one frog to an empty rock, but they can’t jump
over more than one frog. The frogs can only jump forwards. Send the ABC frogs to
the right-hand side of the pond and send the xyz frogs to the left-hand side of the
pond. How many moves does it take?
What is the answer to the problem? You can use the interactive puzzle at
http://tiny.cc/frogs to help solve the problem.
Extension: find a rule (algorithm) to work out the minimum number of moves
needed for any number of frogs on each side.
Pattern recognition
Pattern recognition involves following the process of abstraction and
decomposition to try to identify patterns that will help to solve problems.
Video
Watch http://tiny.cc/patternrec about pattern recognition from BBC Teach.
Example – jigsaw
To complete this jigsaw, a child will look for patterns.
15 26 7 7 2 18
i) ii)
iii) iv)
How about entering this year’s Bebras Challenge? Your school can register at
www.bebras.uk
Problem solving
Pseudocode
Pseudocode looks a bit like the English language but has some structure to it.
Pseudo means ‘fake’ and so pseudocode is ‘fake code’. Pseudocode is one way
of writing instructions for an algorithm. There is no single correct solution for writing
pseudocode – it just needs to make sense, be clear and unambiguous. Edexcel
define pseudocode as:
Video
Watch http://tiny.cc/pseudo-code about pseudocode from TED-Ed. You will
notice there is also some pattern recognition involved.
Activity – pseudocode
Using your own structured English or pseudocode, write algorithms for the
following:
1) Hang a picture on the wall.
2) Charge a mobile phone fully.
3) Cross a road.
4) Calculate entrance price to a theme park - £45 for adults, £30 for children
under 16, £5 for children under 4 and £15 for OAPs. There is a discount of
10% if purchased online.
Note: In an exam, you can use your own version of pseudocode as long as it
makes sense to another person. Edexcel do not have a standard way of
presenting pseudocode to you in an examination. However, you should be
familiar with all the commands used in Edexcel’s Programming Language
Subset (PLS) of Python which will be used in programming questions for
Paper 2. The PLS is not pseudocode but a subset of the Python
programming language.
Example – pseudocode
This example of pseudocode calculates the body mass index (BMI) of a person.
This pseudocode is structured and looks similar to a programming language. It is
not necessary for pseudocode to be structured with syntax like this, as long as it is
clear and unambiguous.
Video
Watch this video http://tiny.cc/pseudokhan which shows a different approach to
using pseudocode from Khan Academy.
Flowcharts
So far, we have only written instructions for algorithms. Flowcharts can represent
algorithms graphically using a set of symbols.
These are some of the symbols that can be used within a flowchart:
Symbol Meaning
Terminator used at the start and end of a flowchart to
indicate the start and end of an algorithm. The
terminator must always include the word START at the
beginning and STOP or END at the end. The only
exception is a loop that continues forever which does not
need a STOP terminator.
Note: the colours used above are just for illustrative purposes. Colours are not
necessary in a flowchart.
There are lots of tools that you can use to draw flowcharts, including a pencil and
ruler or a pencil and stencil. If you are looking for an online tool then
https://www.draw.io/ is simple to use and moves symbols and arrows around
together. You can also save flowcharts to the cloud or locally. The symbols used
in the table above can be found in basic flowchart symbols.drawio which can be
opened in draw.io using either the online or installed version. In this
chapter, draw.io has been used to create most flowcharts and all files
are in the examples folder, but to show that other tools can also be
used, Microsoft Visio has been used to create some flowcharts too.
The algorithm above has a start and an end but there are no decisions. This is
known as a sequence of instructions.
Iterative algorithms also require a decision to be made but the outcome of the
decision will enable processes to repeat.
Example – countdown
Flowcharts are used for programming concepts
such as sequence, selection and iteration in
chapter 2. In this chapter, the focus is just on
the use of flowcharts for everyday algorithms,
but this is an example of an algorithm that
could be represented as a computer program.
Pseudocode
Algorithms may just include sequence, or they may include selection and may also
include iteration. When lots of decisions need to be made, the algorithm can
become quite complex. Looking at the algorithm visually as a flowchart can help
to understand the purpose of the algorithm and how it works.
Example – snap
This algorithm for
a game of snap
uses all of
sequence,
selection and
iteration.
Player 1 and
Player 2 playing
their cards are
processes in
sequence.
Deciding whether
the cards match
or not are
selection but the
‘no’ route enables
iteration to take
place of the
players playing
cards.
Activity – flowcharts
Draw flowcharts to represent algorithms for the following:
1) Hang a picture on the wall.
2) Charge a mobile phone fully.
3) Cross a road.
4) Calculate entrance price to a theme park - £45 for adults, £30 for children
under 16, £5 for children under 4 and £15 for OAPs.
You may find it useful to refer to the pseudocode you wrote form the previous
activity.
Questions – follow me
1) a) Define the term algorithm. [2]
b) Explain why an algorithm must be accurate and consistent? [2]
2) Understanding algorithms
STORAGE
The input, process and output for one set of input and output data for the
thermostat could be shown as:
Current
Decide if it is Turn heating on
temperature in
too hot or cold or off
room
Temperature
set by user
The process could be broken down into a flow chart like in the central heating
example seen earlier.
c) Car
2) For each device above, draw one input, process and output diagram to
show how one set of input data is processed into an output.
Image file on
hard disk
(STORAGE)
Formula or
Variable Data Answer
Function
(INPUT) (OUTPUT)
(PROCESS)
Example – spreadsheet
The input data here are 5 for the
width and 3 for the length. The
output is 15 for the area.
B1 = 5
B1*B2 15
B2 = 3
(PROCESS) (OUTPUT)
INPUT
Example – countdown
This is an example you have seen before in both
flowchart and pseudocode form.
Pseudocode
2) Describe the algorithms below in terms of their inputs, processes and outputs:
a) def subname(s, t):
if s > t:
result = True
else:
result = False
#endif
return result
#end subname
print(subname(5, 4))
b) authenticated = False
while not authenticated:
username = input("Enter username: ")
password = input("Enter password: ")
if username == "LongPA" and password == "letmein":
authenticated = True
print("Welcome.")
else:
print("Username and password wrong. Try again.")
#endif
#endwhile
The example above is quite simple and straightforward. However, look at some of
the other algorithms you have seen as examples earlier in this chapter such as
changing gear in a car, the snap game, countdown and central heating
algorithms and it is not so obvious what the purpose of the algorithm is.
import random
y = int(input("Please enter largest number: "))
z = random.randint(0, y)
i = -1
while i != z:
i = int(input("Please enter number: "))
if i == z:
print("Well done")
elif i < z:
print("Higher")
else:
print("Lower")
#endif
#endwhile
You need to decompose the problem by looking at each stage and the inputs,
outputs and processing. Work through the algorithm from beginning to end and
replace variables with descriptions of what data the variable holds.
The user is asked to enter another number which is assigned to <i>. The whole
process repeats while the new number chosen by the user <i> does not match
the random number <z>.
i = -1
while i != z:
i = int(input("Please enter number: "))
.....
#endwhile
If the new number chosen by the user matches the random number then the user
is told “Well done” and the loop ends.
if i == z:
print("Well done")
If the new number chosen by the user is less than the random number then the
user is told “Higher” and the loop continues until the user chooses a number that
matches the random number.
elif i < z:
print("Higher")
If the new number chosen by the user is bigger than the random number then the
user is told “Lower” and the loop continues until the user chooses a number that
matches the random number.
else:
print("Lower")
The purpose of the algorithm is that the user chooses what the largest random
number can be. The user must then guess what the random number is until they
get it correct. To help the user, they are given clues as to whether the random
number is higher or lower than their guess.
It is the last paragraph that is the answer to the question. The rest of the example
was how you would have gone about understanding the problem in your head or
by making notes.
print(subr(2, 5))
The answer is that the purpose is to calculate z to the power of y. The reason for
the IF statement is in case the power y is 0 in which case the answer will always be
1.
print(subr(5, 5))
2) a = "hello"
b = "goodbye"
3) def subr(x):
result = ""
for i in range(len(x) - 2):
result = result + x[i] + ","
#next i
result = result + x[i+1] + "."
return result
#end subr
print(subr("Blobby"))
Extension: Write a single line of code to replace the first WHILE loop
and another for the second WHILE loop in question 2 to
make the code more efficient.
Trace tables
Trace tables are used to test the logic of an algorithm. They can help to identify
any errors in the logic or confirm that the algorithm is working as it is intended to.
A trace table may include a row for each line of code where the values of
variables could change or a new row may only be used when there is no more
space in the current row. It will also include a column for each variable. If there
are any outputs then an output column should also be used. You can then trace
through an algorithm by writing down the value of each variable during each line
of code.
Working through this one line at a time gives the following results:
Notice in the example above how it is only necessary to record when the value of
a variable changes. It is not necessary to repeat its value if it has not changed.
© paullong.net 2020 Page 37 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
Working through this one line at a time gives the following results:
Line a b c OUTPUT
START 20 30
10 20
11 “smaller”
13 20
When an algorithm uses selection, there is often a lot to ignore because certain
routes are not followed.
Trace tables are most useful for algorithms that use iteration because it allows you
to see the changes in variables through each iteration.
Video
Watch this video about creating trace tables http://tiny.cc/tracetables
Working through this one line at a time gives the following results:
Line z y i result RETURN
1 2 4
2 1
4 1
5 2
4 2
5 4
4 3
5 8
4 4
5 16
8 16
This is an example that has been used before for describing the purpose of the
algorithm. Notice how using a trace table makes it easier to understand what the
algorithm is doing.
Notice that a new line has been used for each iteration of the FOR loop. This
makes it easier to spot where errors might occur in a program and to refer back to
each iteration.
Note: although the Computing 101 website uses a separate line in the trace table
for each line of code, this is not necessary in exams.
Trace tables are also useful when tracking the values within an array. When
creating a trace table that includes an array, create a separate column for each
position within the array and group those columns together:
Array Name
0 1 2 x
Video
Watch this video http://tiny.cc/trace-array from Mr AI Teacher to see how a trace
table can be used in an array.
The trace table below starts a new line for each iteration. This makes it easier to
follow what is happening each time the FOR loop runs.
x
Iteration 0 1 2 z i OUTPUT
- 1 3 7 3
1 5 0
2 15 1
3 35 2
- 55
Exam questions tend to ask you to produce a trace table for an algorithm
represented in the Python programming language. However, there may be
occasions when you are asked to produce a trace table for an algorithm
expressed in another format such as a flowchart.
Video
Watch this video http://tiny.cc/trace-flow from Liam McQuay about how trace
tables can be used to trace an algorithm represented by a flowchart.
2) Complete the trace table for the algorithm below when a is “Hi” and b is
“Hello”. Add extra rows that you are likely to need.
if len(a) < len(b):
while len(a) < len(b):
b = b[0:len(b)-1] # this could be written b[0:-1]
#endwhile
elif len(b) < len(a):
while len(b) < len(a):
a = a[0:len(a)-1] # this could be written a[0:-1]
#endwhile
#endif
Iteration a b len(a) len(b)
-
3) Complete the trace table for the algorithm below when x is [“A”, “B”,
“C”, D”]. Add extra rows that you are likely to need.
def subr(x):
result = ""
for i in range(0, len(x) – 1):
result = result + x[i] + "-"
#next i
result = result + x[i+1] + "."
return result
# end subr
x
Iteration 0 1 2 3 i len(x) - 1 result RETURN
Evaluating algorithms
Have you heard of the saying “there is more than one
way to crack an egg”? This is often used to mean
“there is more than one way to solve a problem”.
Each morning you need to get to school. There are
several ways you could do this: you could walk, go by
car, go by bus, catch a train or cycle. Each method
will solve the problem of getting to school.
1) How would you get from Kings Cross to Waterloo? Is there another way?
2) How would you get from Knightsbridge to Liverpool Street? Is there another
way?
3) Compare your answers with others in your class. Are they all the same?
4) Use www.tfl.gov.uk to search for journeys from Euston to Westminster.
Compare the different journey options. Try changing some of the settings
such as Tube only.
From the examples above, some methods will be more efficient than others. If you
do not live close to a train station or your school is not close to a train station, then
catching the train would not be very efficient as it would take a very long time. If
you live 5 miles away from your school, then walking may not be as efficient as
catching the bus, although it would be good exercise. Although different
problems have different solutions, some are more efficient than others.
A more efficient algorithm will complete more quickly than a less efficient
algorithm. There are other efficiency factors that can be considered like the
readability of the code and the amount of memory required but the examples
used in this book will focus on time.
You know there are 8 legs per octopus, so you could do:
8+8+8+8+8+8+8+8+8+8
But this would take you a long time and so you are more likely to calculate:
8 x 10
Both algorithms have the same result – the value of 10 is assigned the variable <x>.
However, algorithm 2 uses 20 instructions (10 times assigning <i> and 10 times
assigning <x> to complete this task whereas algorithm 1 only uses two instructions,
making it more efficient.
Both algorithms have the same purpose, the same result and both work – they add
5 to each element in the array <arr>. However, algorithm 3 has to calculate the
length of <arr> for each iteration, whereas in algorithm 4 this calculation only
takes place once because it is assigned to a variable <arr_len> and that
variable is then used in the WHILE loop.
Algorithm 5 Algorithm 6
myList = [] myList = []
f = open("file56.txt", "r") for i in range(0, 12):
for line in f: f = open("file56.txt", "r")
myList.append(line) myList.append(f.readlines()[i])
#next i f.close()
#next i
f.close()
Both algorithms have the same purpose, the same result, and both work – they
read a line of data from a file and assign it to a position in an array. However,
algorithm 6 has to repeat opening and closing the file for all 12 iterations.
Both algorithms have the same purpose, the same result and both work. However,
algorithm 5 requires an additional process to take place to determine if the age is
greater than or equal to 18. This is unnecessary because if the age is not less than
18 then it must be greater than or equal 18 and so only ELSE is required, rather
than an ELIF which will result in a need to process the additional condition.
It is possible to make the readability of the code more efficient, but this doesn’t
necessarily make the algorithm more efficient in terms of code. Therefore, the
following methods that can be used to make code more readable and use less
lines of code do NOT necessarily improve the time efficiency of an algorithm:
• using loops
• using subroutines
• using IF . . . ELIF rather than lots of IF . . . : . . . END statements if the same
variable is being checked
Algorithm 10
scores = []
for i in range(1,5):
score = input("Please enter score " + str(i) + ": ")
scores.append(score)
#next i
Both algorithms complete in the same amount of time available. Algorithm 10
looks more efficient because the code is in a loop, but algorithm 1 will complete
more quickly because the variable <i> does not have to be assigned and added
to the prompt each time. Algorithm 10 is better written, but not more efficient.
You will see later in this chapter how different algorithms can be used for sorting
and searching data and how some are more efficient than others.
Questions – follow me
1 Examine the algorithm below:
weight = float(input("Please enter weight in kilograms: "))
height = float(input("Please enter height in metres: "))
bmi = weight / height / height
d) Complete the trace table below for this algorithm. You may not need to
use all rows. [2]
weight height bmi description
70 1.6
Algorithm 1 Algorithm 2
p = -3 p = -3
if p > 0: if p > 0:
result = "positive" result = "positive"
elif p == 0: elif p == 0:
result = "nothing" result = "nothing"
elif p < 0: else:
result = "negative" result = "negative"
#endif #endif
Explain why algorithm 2 is more efficient than algorithm 1 in terms of time. [2]
Explain why algorithm 1 is more efficient than algorithm 2 in terms of time. [2]
Algorithm 1 Algorithm 2
grades = ["A", "B", "C", "D"] grades = ["A", "B", "C", "D"]
grade = input("Enter a grade: ") grade = input("Enter a grade: ")
h = len(grades) found = False
found = False i = 0
i = 0 while i < len(grades) and not
while i < h and not found: found:
if grades[i] == grade: if grades[i] == grade:
found = True found = True
else: else:
i += 1 i += 1
#endif found = False
#endwhile #endif
#endwhile
Give two reasons why algorithm 1 is more efficient than algorithm 2 in terms of
time. [4]
3) Searching algorithms
Computers can search for data much more quickly than we can as humans.
Imagine looking up a contact in your mobile phone. Scrolling down the list might
take you a few seconds, but typing the first two letters into a search box finds the
contact very quickly.
Compare that to searching for the topic by clicking on the link in the contents
page or using the find feature of your software.
Both of the tasks above were achievable by both a computer and a human,
although the computer will have been much faster. Now imagine a bank that
needs to find a customer from a list of hundreds of thousands of customers. A
computer can find the customer in a few seconds, but a human would take
several hours if not days.
Video
Watch the video http://tiny.cc/searchBBC on searching algorithms from BBC
Bitesize.
Linear search
The linear search algorithm is a simple sequential search. It looks through a list one
data item at a time until it finds the data it is looking for.
REPEAT
Look at next book
Is it Sam’s book?
If yes, then book found
UNTIL book is found OR no more books
A computer can search for data within an array. The data the computer is
searching for will be referred to as the target.
Using a linear search, the first item in an array (position 0) will be compared with
the target. As 20 ≠ 72 then the target has not been found.
Position 0 1 2 3 4 5 6
Value 20 18 64 19 72 25 4
This process continues for positions 1, 2 and 3 where the target of 72 is still not
found:
Position 0 1 2 3 4 5 6
Value 20 18 64 19 72 25 4
The process continues again for position 4 where the value is 72. As the value
matches the target, the position in the array has been found:
Position 0 1 2 3 4 5 6
Value 20 18 64 19 72 25 4
Video
Watch this linear search video http://tiny.cc/linearsearch from Harvard University.
This is a very basic version of the Python code for a linear search:
1 for item in myArray:
2 if item == target:
3 print(target, "found")
4 #endif
5 #next item
<target> is the item being searched for and <myArray> is the array that will be
searched to find that item.
Line 1 defines the loop which will repeat for every item in <myArray>.
Line 2 determines if <target> has been found. If <target> has been found, then
a confirmation message is printed by line 3.
© paullong.net 2020 Page 51 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
Exam tip: you can use the version above in an exam for lower marks, but if you
are aiming for higher grades then you also need to be able to
understand and write the WHILE loop versions shown below.
The linear search above could be made more efficient though. Currently, it
searches through every single item in the array. Even if it finds the <target> at
position 3 out of 100, it will still continue to search the rest of the array.
Below is a modified more efficient version of the linear search which BREAKs out of
the loop if the <target> has been found. This means the search does not
continue after the <target> has been found.
1 for item in myArray:
2 if item == target:
3 print(target, "found")
4 break
5 #endif
6 #next number
Line 4 only runs if the <target> is found. Line 4 breaks out of the FOR loop so the
rest of <myArray> does not need to be searched.
Line 1 calculates the length of <myArray> so it does not have to be calculated for
each iteration of the WHILE loop.
Line 2 sets a <found> flag to be default so the WHILE loop will run at least once.
Line 3 initialises the <counter> variable which will be used to identify the position in
<myArray> that should be checked.
Line 4 uses a WHILE loop to decide if either the end of <myArray> has been
reached and also that the <target> has not been found. This stops the loop from
continuing after the <target> has been found.
Line 5 uses the current value of counter to look at the next item in <myArray> and
compares it with the <target>. If they match, then lines 6 and 7 are run, but if
they do not match then line 9 runs which will increase the value of counter ready
for the next item in the <myArray>.
Line 6 simply states that the <target> has been found. Line 7 sets the <found>
flag to be True which means that the WHILE loop will not run again.
If the <target> was found then Line 6 displays the position of the <target> in the
<myArray>. If the <target> was not found, then lines 12 to 13 are used to report
this to the user.
Exam tip: you should be familiar with the code above for exams and you should
be able to write similar code if asked in an exam.
5) Complete this flowchart for the linear search algorithm above that uses a
WHILE loop.
So far you have seen the linear search being used directly within the programming
code. The linear search is likely to be re-used within a program several times and
by other programs too. It would therefore be sensible to put it into a function. If
you are aiming for the highest grades, then you should try to do this.
if not found:
counter = -1
#endif
return counter
#end linearsearch
The function can now be re-used several times. In the example below, F is being
searched for in a list of letters. The <linear_search> function is called and the
position of the item is assigned to a variable called <index>.
This final section of code checks to see if <index> was -1. If it was, then it reports
that the <letter> was not found. If it was not -1 it displays the position of the letter
in the list of letters.
if index == -1:
print(letter, "was not found in the list.")
else:
print(letter, "was found at position/index", index)
#endif
The linear search is easy to code, and it works well for a reasonably short list, but
once there are thousands of items in the list the linear search is too slow. There is
not a more efficient algorithm than a linear search when a list is unordered.
© paullong.net 2020 Page 56 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
Binary search
Video
Watch this video http://tiny.cc/linear-vs-binary about the weakness of the linear
search algorithm and how the binary search algorithm overcomes this. Watch this
video http://tiny.cc/binary-CSU for a fun way of understanding the binary search
algorithm from Computer Science Unplugged
A binary search works by splitting a list into two halves. It then keeps halving the list
until the value is found. It will only work on an ordered list. To find the half way
point, the middle item in the list is found to give the left-hand side of the list and the
right-hand side of the list:
For an odd number of items in a list, the middle is the middle item so there are an
equal number of items on the left-hand side to the right-hand side as shown
below:
Position 0 1 2 3 4 5 6
LEFT-HAND SIDE OF LIST Middle RIGHT-HAND SIDE OF LIST
For an even number of items in a list, the middle is the number of items in the list
divided by two using an integer division (DIV):
Position 0 1 2 3 4 5 6 7
LEFT-HAND SIDE OF LIST Middle RIGHT-HAND SIDE OF LIST
Remember: DIV 2 means divide by 2 and only use the whole number part of the
answer.
To find the middle position in a list for a binary search, always divide the (number
of items) by 2 and round down:
The middle item in an even length list can be found visually by looking at the 2
middle values and choosing the right-hand side value:
Position 0 1 2 3 4 5 6 7
middle values
left right
Note: It is also acceptable to visually use the left-hand side item. As long as you
are consistent, either method can be used. The right-hand side value works
better in programming because it is a simple calculation to do as shown
below.
In Python, the middle item in a list would be calculated as:
middle = len(array) // 2
Note: In Python, the middle position in a list using the left-hand side item would be:
Position 0 1 2 3 4 5 6
LEFT-HAND SIDE OF LIST Middle RIGHT-HAND SIDE OF LIST
Position 0 1 2 3 4 5 6 7
LEFT-HAND SIDE OF LIST Middle RIGHT-HAND SIDE OF LIST
Number of items = 8
8+1=9
9 ÷ 2 = 4.5 (median position)
2) To make sure you do not get confused in maths, calculate the median of
each of the above lists (indexing starts at 1). Show your working.
The binary search algorithm uses a method sometimes referred to as divide and
conquer. The middle value of a list is found to divide the list. Then the side of the
list that does not contain the target value being searched for is discarded and the
half that does contain the value is kept. The target value is compared with the
middle value. If the target value is higher than the middle value then the right-
hand side of the list is kept. If the target value is less than the middle value then
the left-hand side of the list is kept. If the target value matches the middle value
then the target value has been found. Here is one way of writing the algorithm:
• REPEAT
- IS Target Value = Middle Value?
▪ target value found
- ELSE Is Target Value > Middle Value?
▪ keep right-hand side
- ELSE (Target Value must be < Middle Value)
▪ keep left-hand side
• UNTIL target value found or no more items in list
The middle value is the 7th item in the list (14 DIV 2 = 7) which is 67.
The target value (89) is greater than the middle value (67) so the right-hand side
of the list is kept:
The target value (89) is less than the new middle value (92) so the left-hand side
of the new list is kept:
The new middle value is 85. The target value (89) is greater than the new middle
value (85) so the right-hand side is kept:
LHS removed
Left-hand side removed 89 Right-hand side removed
89 is the only value left and is automatically the new middle value and as it
matches the target value (89), the target value has been found.
This took 4 iterations of the binary search to find the target value. A linear search
would have taken 10 iterations because the target value is in the 10th position.
Example 2
The target that is being searched for is 41. Here is the list that will be searched:
2 3 7 11 13 17 19 23 29 31 37 41 43 47
The middle value is 23 (right of centre or 14 DIV 2 = position 7). The target value
(41) is greater than the middle value (23) so the right-hand side of the list is kept:
The new middle value is 41 (right of centre or 6 DIV 2 = position 3). The target
value (41) matches the middle value (41) so the target value is found. This took 2
iterations. A linear search would have taken 12.
Example 3
This time the target that is being searched for is E. Here is the list that will be
searched:
A C E G H J K M N P R Y Z
The middle value is the 7th item in the list (13 DIV 2 = 6) which is K.
The target value (E) is less than the middle value (K) so the left-hand side of the
list is kept:
The target value (E) is less than the new middle value (G) so the left-hand side of
the new list is kept:
The new middle value is C (3 DIV 2 = 1). The target value (E) is greater than the
new middle value (A) so the right-hand side is kept:
E is the only value left and is automatically the new middle value and as it
matches the target value (E), the target value has been found.
This took 4 iterations of the binary search to find the target value. A linear search
would have taken 2 iterations because the target value is in the 2nd position.
2) The target being searched for is 7. The list being searched is shown below:
1 3 7 12 18 25 36 49 78 85 92 98 99
Explain how the binary search finds the target value 7 in the list above.
5) Complete this flowchart for the binary search algorithm that uses a REPEAT
loop.
Yes Yes
No No
Extension video
Watch this explanation of the binary search and a simple version of the
pseudocode http://tiny.cc/binarypseudo1 from Harvard University.
Now watch this other explanation of the binary search and its pseudocode
http://tiny.cc/binarypseudo2, also from Harvard University. Stop at 4 minutes 22
seconds.
This is Python code being used to represent a binary search algorithm that reports
back whether an item was found in the list:
Line 1 defines the function and accepts the parameters <target> (search value)
and <arr> (the array to search).
Lines 2-4 assign values to variables. <found> is set to False and will only change
to True if the target value is found in the array. <left> and <right> are used to
define the first and last items in the array to be searched. Initially these will be 0 for
the first item and the last item will be one less than the length of the array
(because the array count starts at 0). <left> or <right> will change during each
iteration to define the new shorter list to use.
Line 5 starts the loop which will continue while the <left> and <right> values do
not match and while the <target> value has not been found. When the <left>
and <right> values match, it means that the final middle value has been found
because the start and end of the list will be the same value.
Line 6 defines the new middle value. It will always be the <left> position plus the
<right> position divided by two and rounded down to an integer value.
Lines 7 to 13 determine if the target value has been found, or whether the list
needs splitting in half to try and find the target value again. If the <target> value
is found then it will match the middle item of the array (line 7) and so True will be
assigned to <found>. This will stop the WHILE loop from continuing next time
around. If the <middle> value is less than the target value then it means that the
right hand side of the list needs to be kept and so the value of <right> remains
the same and the value of <left> is changed to be the position after the current
middle position. The opposite happens if the left-hand side needs to be kept.
7 if arr[middle] == target:
# <arr[3]> is 36, <target> is 6 so not true
8 found = True # this line is not run
9 elif arr[middle] < target:
# arr[3] is 36 and is not < 6 so not true
10 left = middle + 1 # this line is not run
11 else:
# only other option is that 36 < 6
12 right = middle - 1
# <right> becomes 3 – 1 = 2
13 #endif
# while loop will repeat with <left> 0 and right <2>
Position 0 (left) 1 2 (right)
Value 4 6 29
15 #endwhile
A program calls the subroutine above when searching for a make of car:
if in_list:
print(make, "was found in the list.")
else:
print(make, "was not found in the list.")
#endif
1) Explain how the binary search code finds the target values <target>:
a) Mango
b) Lemon
c) Apple
b) Explain how the binary search code searches for the target value
“Orange”.
left =
middle -
1
left = 0
not found?
return
found
found =
False
middle
= (left +
right) // 2
found =
True
4) Try modifying the function so that it returns the position of the target value.
Position 0 1 2 3 4 5 6 7
Value D F Q R S X Y Z
Trace table:
Iteration target arr found len left right middle arr return
(arr) [middle]
- S [D,F,Q,R,S,Y,Z] False 8 0 7
1 4 3 R
2 4 5 X
3 True 4 S
Note: although <left> <= <right> (4 <= 4) on the final iteration, the WHILE
loop still continues because <found> is still False but then it does not
perform another iteration because <found> is set to True on line 8.
2) Using the binary search code, complete the trace table for each of the
following values of <target> and <arr>:
a) <target> is Y and <arr> is:
Position 0 1 2 3 4
Value R S V W Y
Position 0 1 2 3 4 5 6 7 8 9 10
Value 2 4 29 30 32 35 40 64 78 98 99
Position 0 1 2 3 4 5 6
Value Doe Fa La Me Ray Sew Tea
Position 0 1 2 3 4 5 6 7 8 9
Value 0.3 0.8 1.6 2.4 2.5 2.8 3.1 3.2 3.6 4.0
Trace table:
Iteration target found len left right middle arr return
(arr) [middle]
Extension video
Watch the explanation of binary trees at http://tiny.cc/binarypseudo3 from
Harvard University. Start from 4 minutes 22 seconds.
Video
Watch these videos http://tiny.cc/linearbbc and http://tiny.cc/binarybbc from Ed
Tracey’s BBC Bitesize videos to compare the efficiency of the linear and binary
search algorithms.
Using a binary search to find the target value 32 would only take one attempt as
the value 32 is in the middle of the array at position 4 (8 DIV 2). (30 would also
be an acceptable answer if choosing left-hand side of middle point.)
Using a binary search to find the target value 2 would take 4 attempts:
Positions 0 - 7 position 4 (32) as middle
Positions 0 - 3 position 2 (29) as middle
Positions 0 - 1 position 1 (4) as middle
Position 0 position 0 (2) as middle = FOUND
(If using the left-hand side of middle point, then worst-case scenario would be for
the target value of 64.)
© paullong.net 2020 Page 69 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
Position 0 1 2 3 4 5 6 7 8 9 10
Value A F H J L M P R T V Z
1) How many attempts would the linear search take to find each of the
following target values:
a) A b) P c) Z d) L
2) How many attempts would the binary search take to find each of the
following target values:
a) A b) P c) Z d) L
3) Using an example, explain how the simplified linear search algorithm below
could be made more efficient.
Attempt 1 2 3 4 5 6 7 8 9 10 11
Size 1024 512 256 128 64 32 16 8 4 2 1
Notice how these are binary numbers. The number of attempts is equivalent to a
binary logarithm (base-2 logarithm) of the number of items in the list plus one more
attempt. The binary logarithm calculates p for 2p (2 to the power of p) needed
when the size of the list = 2p.
Each time the list size is doubled, the number of attempts only increases by 1. So,
for a list with 2048 items, only 12 attempts would be needed.
The graph below shows how efficient the binary search algorithm is as the number
of items in a list grows:
Log2 n + 1 Graph
18
16
Number of attempts (Log2n +1
14
12
10
0
3700
26200
100
1000
1900
2800
4600
5500
6400
7300
8200
9100
10000
10900
11800
12700
13600
14500
15400
16300
17200
18100
19000
19900
20800
21700
22600
23500
24400
25300
27100
28000
28900
29800
Items in List
You can use a calculator’s log2 function or a search engine to calculate how
many attempts it would take in the worst-case scenario for any size of list.
Questions – follow me
1) A programmer wants to use a search algorithm for this array
[3, 8, 9, 15, 12, 8].
a) Explain how a linear search would search for the target value 12. [4]
b) State why a binary search could not be used for this array. [1]
d) A binary search algorithm is used with the list ['a', 'c', 'e', 'f',
'h', 'j', 'm', 's', 't'] to find the target value 'j'.
Complete the table to show the characters in the order that the algorithm
would compare them against the target value. You may or may not
need to use all the rows. [3]
Middle Value
4) Sorting algorithms
It can often help if data in a list is sorted in order. For example, if you wanted to
use a binary search, then the data would need to be sorted into order. The order
of data could include:
When data is produced as an output to the user, it may need sorting so that the
user can find data in the list quickly.
Bubble sort
Video
Watch http://tiny.cc/bubblesortBBC from Ed Tracey’s BBC Bitesize videos.
Here is a simple version of the bubble sort algorithm based on sorting numbers
where n is the position in the list:
Following the bubble sort algorithm, each pair of items is compared and if
necessary, swapped:
Pos Value Pos Value Pos Value Pos Value Pos Value
4 19 4 19 4 19 4 19 4 23
3 18 3 18 3 18 3 23 3 19
2 17 2 17 2 23 2 18 2 18
1 15 1 23 1 17 1 17 1 17
0 23 0 15 0 15 0 15 0 15
Notice how 23 has risen from the bottom of the list to the top of the list like a
bubble. As swaps took place, the whole process needs to be repeated:
Video
Watch http://tiny.cc/bubble-harvard from Harvard University but stop at 1:45.
The algorithm for this simple version of the bubble sort can be represented as a
flow chart:
In an exam, the list is likely to be presented to you horizontally, rather than vertically
and so from this point forward, all examples will use a horizontal list – just imagine
the bubble moving to the right instead of to the top.
Following the bubble sort algorithm, each pair of items is compared and if
necessary, swapped. The table below shows the first pass (iteration):
Pass 1:
Position 0 1 2 3 4 Notes
Value 30 10 50 40 20 30 is bigger than 10 so items 0 and 1 will swap
The first pass has moved the highest value of 50 to the top of the list. As swaps took
place, the whole process needs to be repeated:
Pass 2:
Position 0 1 2 3 4 Notes
Value 10✓ 30 40 20 50 10 and 30 in order so no swap needed
The second pass has moved the second highest value of 40 to the second highest
position in the list. As swaps took place, the whole process needs to be repeated:
Pass 3:
Position 0 1 2 3 4 Notes
Value 10✓ 30 20 40 50 10 and 30 in order so no swap needed
The third pass has moved the third highest value of 30 to the third highest position
in the list. As swaps took place, the whole process needs to be repeated:
Pass 4:
Position 0 1 2 3 4 Notes
Value 10 20 30 40 50 10 and 30 in order so no swap needed
✓
Value 10 20 30 40 50 30 is bigger than 20 so items 1 and 2 will swap
✓
Value 10 20 30 40 50 30 and 40 in order so no swap needed
✓
Value 10 20 30 40 50 40 and 50 in order so no swap needed
✓
Value 10 20 30 40 50 End of 3rd pass
No swaps took place so the bubble sort is complete and the list is in ascending
order.
You could also be asked to show just the changes that are made with the first row
given for you:
8 12 6 4 7 This is the first row given for you.
Activity – efficiency
Can you find a way of making the bubble sort algorithm above more efficient?
Exam tip: you can use the version above in an exam for lower marks, but if you
are aiming for higher grades then you also need to be able to
understand the more efficient version shown below.
Have you noticed that the after the first pass, the last value in a list is in its final
position? Then after the second pass, the last 2 values in a list are in their final
positions, and after a third pass, the last 3 values are in their final positions ? The
pattern continues. It is therefore not necessary to keep checking the latter values
during each pass. The method above was used earlier, but this time, for each
pass, the bubble sort stops one position shorter than the previous pass.
The blue highlights show steps that do not need to be taken. This is unnecessary
processing. The method below is much more efficient:
The bubble sort algorithm will be used to sort the array [8, 12, 6, 4, 7]. Show
the steps involved using the bubble sort algorithm.
Video
Watch this explanation of a bubble sort http://tiny.cc/bubblecups using
polystyrene cups from KC Ang. Notice how the sort is shorter for each pass.
Here is the complete version of the bubble sort algorithm based on sorting
numbers where n is the position in the list. The green highlights show the
improvements from the simple bubble sort:
7
4
Exam tip: use this complete version of the bubble sort algorithm in the exam.
1) The bubble sort algorithm will be used to sort the array [Q, Z, R, B].
Show the steps involved using the bubble sort algorithm.
Pass 1 Pass 2 Pass 3
0 1 2 3 0 1 2 3 0 1 2 3
Q Z R B
Q Z R B
Q R Z B
Q R B Z
2) The bubble sort algorithm will be used to sort the array [9, 8, 5, 4, 6].
Show the steps involved using the bubble sort algorithm. You may or may
not need to use all rows for each pass.
3) The bubble sort algorithm will be used to sort the array [a, h, g, f, b].
Show the steps involved using the bubble sort algorithm.
4) The bubble sort algorithm will be used to sort the array [3, 12, 4, 7].
Show the steps involved using the bubble sort algorithm.
5) The bubble sort algorithm will be used to sort the array [10, 4, 8, 3, 7].
Show the steps involved using the bubble sort algorithm.
6) The bubble sort algorithm will be used to sort each of the following arrays.
Show the steps where a change is applied to the array.
a) [19, 6, 13, 14, 18] b) [3, 1, 8, 5] c) [Z, P, D, B]
Click on BUBBLE at the top, then SORT at the bottom followed by GO. You can
pause or rewind the animation at any time. Notice how the items are changing in
time with the pseudocode visualisation.
© paullong.net 2020 Page 80 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
1 def bubblesort(arr_in):
2 arr = arr_in[:] #this is to avoid overwriting original list
3 last_compare = len(arr) - 1
4 swapped = True
5 while swapped and last_compare != 0:
6 swapped = False
7 for counter in range(0, last_compare):
8 if arr[counter] > arr[counter+1]:
9 temp = arr[counter]
10 arr[counter] = arr[counter+1]
11 arr[counter+1] = temp
12 swapped = True
13 #endif
14 #next counter
15 last_compare = last_compare - 1
16 #end while
17 return arr
18 #end bubblesort
Position 0 1 2 3 4
last_compare = length - 1
Line 4 sets a <swapped> variable (flag) to False so the WHILE loop runs at least
once.
Lines 5 to 16 are the WHILE loop for each pass (iteration) of the bubble sort. It runs
while items have been swapped. The first condition of items being swapped
means that the bubble sort will stop if no items are swapped because it then
knows the list is in the correct order. The second condition is there so that if the
bubble sort has to swap when only comparing the first two values on the final pass
where the <last_compare> is now 0 (first item in array), then it doesn’t need to run
again to check that there are no swaps
Line 6 sets the variable (flag) named <swapped> to be False so that the WHILE
loop will continue to repeat if values are swapped.
Lines 7 to 14 deal with comparing adjacent items in the list and swapping them if
necessary. The FOR loop starts at the item in position 0 of the array and finishes at
the last but one item in the array because the last item has nothing left to
compare with (remember Python’s top value in range is exclusive (it is not
included in the range).
Each pair of items is compared using lines 8 to 13. If a swap is necessary (line 8),
then a temporary variable <temp> (line 9) is assigned the value of
<arr[counter]> (the current position in the array). The current position in the
array is then assigned the value of the next position in the array
<arr[counter+1]> (line 10). The next position in the array is then assigned the
temporary value <temp> (line 11) which was originally the current position in the
array.
The variable (flag) <swapped> is then set to True so that the WHILE loop knows a
swap took place and will continue with another pass.
Line 15 reduces the <last_compare> by one at the end of each pass so that the
next pass does not need to compare the higher values that are already in their
correct position.
Position 0 1 2 3 4
last_compare = last_compare - 1
Run the code for the complete bubble sort. Watch the step-by-step version to
watch the variables changing and investigate how the code works. Try out
different values for the list.
Try modifying the code so that it sorts a list in descending order instead of
ascending order.
© paullong.net 2020 Page 82 of 106 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 1 - Published by paullong.net
2) Complete the trace table below for the bubble sort algorithm shown above
when the value of <arr_in> is ["P", "B", "D", "C", "Q"].
Iteration last_ swapped counter temp arr arr arr
compare [counter] [counter+1]
3 True [4,3,5,2]
1 False
1a 0 4 3 [3,3,5,2]
True 4 [3,4,5,2]
Merge sort
The merge sort is a quicker algorithm than the bubble sort for longer lists. It uses a
divide and conquer method. It starts by dividing the list repeatedly into halves until
there is a set of lists with only one item in each:
Example – divide
The array [5,3,9,4,15,2,1,16] is to be sorted using a merge sort. First it is
divided:
5 3 9 4 15 2 1 16
5 3 9 4 15 2 1 16
5 3 9 4 15 2 1 16
5 3 9 4 15 2 1 16
There are now 8 separate arrays with just one item in each.
Next the merge sort conquers and combines by merging the arrays into order:
3 5 4 9 2 15 1 16
3 4 5 9 1 2 15 16
During the first pass through, 3 was smaller than 5 so 3 was moved into the first
position of the new array and 5 into the second position. Similarly, 4 was smaller
than 9 so 4 was moved into the first position of the new array and 9 into the
second position. With 1 and 16, they were already in order so 1 was moved into
the first position and 16 into the second position.
During the second pass through, each of the arrays of length two were merged
into arrays of length four. The smallest number 3 was moved into the first position,
then the next number 4 moved into the second position, then the next number 5
moved into the third position and finally 9 moved into the last position. A similar
process took place with the other two arrays.
In the final pass through, the numbers will be merged in order, starting with one,
into the final sorted array:
1 2 3 4 5 9 15 16
Video
Watch this explanation of the merge sort using cups http://tiny.cc/mergesort from
Harvard University but stop at 1:20.
The phrase divide and conquer is used because the list is divided down into smaller
lists and each smaller list is solved before combining each smaller list into longer
lists. You could try replacing the word list with problem in the sentence above and
you can see how this is a bit like decomposition.
Now show the steps involved in conquering the array into a sorted list using the
merge sort:
The merge sort is an out-of-place sort which means it creates extra lists to sort the
main list. These extra lists take up additional memory while the merge sort is
running.
Video
Watch this animation of the merge sort http://tiny.cc/mergesort2 from Geeks for
Geeks. You can skip the code at the end. Observe how the list on the right-hand
side with an odd number of values is split and then merged.
5 3 9 15 2 1
5 3 9 15 2 1
5 3 9 15 2 1
There are now 6 separate arrays with just one item in each. Notice how the two
odd-numbered lists needed to be split into two, but they were not equal halves.
This left 9 and 1 in lists on their own but each one still needed to be added to
another individual list of one item in the bottom row.
When splitting an odd numbered list, it does not matter whether you split with the
longer list on the left-hand side or right-hand side, as long as you are consistent
with all odd numbered lists.
Now it is time to conquer and combine. Follow the same pattern as before
ensuring the odd numbered lists are merged in the same way they were split.
5 3 9 15 2 1
3 5 9 2 15 1
3 5 9 1 2 15
In the final pass through, the numbers will be merged in order, starting with one,
into the final sorted array:
1 2 3 5 9 15
3) Show the steps involved in merge sorting this array [29, 35, 24, 15, 20]
Click on MERGE at the top, then SORT at the bottom followed by GO. You can
pause or rewind the animation at any time. Notice how the items are changing in
time with the pseudocode visualisation.
Extension video
Watch this visual explanation of the recursive merge sort using odd numbers of
items in lists http://tiny.cc/mergerecursive from Yusuf Shakeel.
Watch the step-by-step version of the code as it runs, to see how the code is being
called recursively to split the lists, and how each recursion collapses to merge the
lists.
Bubble Merge
Time of sort Fine for short lists, but very Faster than a bubble sort for
slow for long lists, unless most circumstances.
they are close to being in
the correct order already.
Memory Variables only use memory New lists and variables reserve
during each pass. more memory each time a
recursion takes place requiring
a lot more memory in this out-
of-place sort.
Complexity Algorithm is reasonably Algorithm is very complex to
simple to write and follow. write and follow.
Best case scenario The list is already sorted so The running time is always
only one pass needed to calculated as n x log2n unless
confirm no swaps required. a quick check is done at start
Number of comparisons = to see if list is already in order
n (number of items in list). in which case the running time
is n.
Worst case scenario The list is in reverse order so For 1,000 items, the number of
the maximum number of comparisons is approximately
passes are needed to 10,000. For 500 items, the
‘bubble’ each element number of comparisons is
across the list. This will take approximately 4,500. The
approximately n2 calculation for number of
comparisons. comparisons is n x log2n
Both sorting methods will sort an unsorted list into an order. The input is an unsorted
list and the output will be an ordered list – e.g. numerical or alphabetical order.
One of you should use the bubble sort to sort the cards, and the other should use
the merge sort to sort the cards.
Record how many comparisons you each had to make. Which search was fastest.
Why do you think it was faster?
You could also do this with a group of students sorting yourselves into height or
alphabetical or date of birth order.
700
600
500
Comparisons
400
300
200
100
0
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Number of elements (n)
Questions – follow me
1) a) State the purpose of the algorithm below. [1]
b) Give the name of a more efficient algorithm for achieving the same
purpose. [1]
c) Give three reasons why a programmer would prefer to use the less time
efficient algorithm from (a). [3]
3) a) A bubble sort is to be used to sort the array [8, 3, 4, 10, 11, 9].
Show the missing steps each time a change is applied to the array. [4]
8 3 4 10 11 9
[4, 6, 1, 2, 9, 3, 5, 8]
[1, 2, 3, 4, 5, 6, 8, 9]
c) A merge sort is to be used to sort the array [8, 3, 4, 10, 11, 9].
Show the stages of the merge sort. [5]
5) Truth tables
Introduction
The Boolean data type has two possible values which are TRUE or FALSE, often
represented as 1 or 0, and ON or OFF in an electrical circuit:
TRUE FALSE
1 0
ON OFF
Truth tables
Computers include electronic circuits which can be switched to ON and OFF using
a variety of components. Some of these components are known as logic gates
which include NOT, AND and OR.
A truth table shows the possible values of outputs for all the possible values of
inputs. Outputs are often shown as a Boolean expression. The simplest Boolean
expression is when the output matches the input:
Q=A
The truth table below shows the output for the Boolean expression Q = A:
Logical operators
NOT
The NOT operator is the simplest of all operators. It reverses the value of a single
input.
If the input is TRUE then the output will be FALSE. If the input is FALSE then the
output will be TRUE.
A truth table can be constructed to show the possible values of outputs for all the
possible values of inputs:
INPUT OUTPUT
0 1
1 0
Q = NOT A
A Q
0 1
1 0
Activity – NOT
1) Consider the Boolean expression below with an input of A and an output of
Q:
Q = NOT A
AND
If both inputs are TRUE then the output will be TRUE. If any of the inputs are FALSE
then the output will be FALSE.
A truth table can be constructed to show the possible values of outputs for all the
possible values of inputs:
INPUT OUTPUT
A B Q
0 0 0
0 1 0
1 0 0
1 1 1
Q = A AND B
Activity – AND
1) Consider the Boolean expression below with an input of A and an output of
Q:
Q = A AND B
OR
If either input is TRUE then the output will be TRUE. If both of the inputs are FALSE
then the output will be FALSE.
A truth table can be constructed to show the possible values of outputs for all the
possible values of inputs:
INPUT OUTPUT
A B Q
0 0 0
0 1 1
1 0 1
1 1 1
Q = A OR B
OR operator in Python
In Python code, the AND operator can be included in an expression for a loop or a
selection statement.
Activity – OR
1) Consider the Boolean expression below with an input of A and an output of
Q:
Q = A OR B
Video
Watch the video at http://tiny.cc/truthtables1 to review truth tables for logic gates.
Although you do not need to know what logic gates are, the concept is quite
simple.
Q = (NOT A) AND B
When creating a truth table, it can help to create a column for interim results. In
this case, a column for the output of the NOT gate would be useful. This will be
referred to as X:
Q = (NOT A) AND (B OR C)
Q = X AND Y
Q = (NOT A) AND (B OR C)
When creating the truth table, start with the interim results X and Y. You can then
use X and Y as an input to Q.
When creating a truth table for a combination of logic gates, you need to
determine how many lines are required in the truth table. This can be calculated
using the following rule:
Always start with a line of zeros and finish with a line of 1s.
The tables below show the pattern to use for each number of inputs:
2 inputs: 3 inputs:
Can you spot a pattern? If you look carefully, you will notice that the inputs are in
a sequence as if they were binary numbers.
Precedence rules
Before looking at the precedence rules for logical operators, let’s look at how they
work in maths.
Brackets
Indices
Division
Multiplication
Addition
Subtraction
This means that any calculations within brackets should be calculated first,
followed by any indices (e.g. 52 = 25). Once those calculations have been made,
then division and multiplication should take place. The final calculation to take
place is addition.
For example:
(5 + 2) x 32 + 4 ÷ 2 – 8
7 x 32 + 4 ÷ 2 – 8
7x9+4÷2–8
63 + 2 – 8
((5 + 2) x 32) + (4 ÷ 2) – 8
The rules of precedence for calculations in maths are important where brackets
are not explicitly used.
Logical operators also have precedence rules when combined to form a complex
expression:
Brackets
Not
And
Or
Brackets should always be evaluated first, followed by the NOT operator. In the
absence of further brackets, AND operators should be evaluated next followed by
OR.
NOT W and NOT C should be the next to be evaluated so we will use interim results
of X and Y:
X = NOT W
Y = NOT C Q = X AND Y OR B
Z = X AND Y Q = Z OR B
a) Q = NOT(A AND B)
A B Q
0 0
0 1
1 0
1 1
b) NOT A OR B
A B Q
0 0
0 1
1 0
1 1
c) Q = NOT A AND (B OR C)
A B C X = B OR C Y = NOT A Q = Y AND X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
A B Q
0 0
0 1
1 0
1 1
A B C Q
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Extension:
In an exam, you would only be expected to work with 3 inputs. This question uses a
4th input – D.
f) Q = A OR B AND C OR D
A B C D Q
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
Questions – follow me
1) Explain why data is represented in computer systems in binary form. [2]
A B A AND B
0 0
0 1
1 0
1 [1] 1
A B X Q
FALSE FALSE
FALSE TRUE
TRUE FALSE
TRUE TRUE FALSE
a) Q = NOT A [1]