1. A 1-dimensional array stores a set of numbered cards from 0 to 7. An example of this data is shown in Fig in 4.
The programmer wants to search for a specific card in the array.
State whether a binary search or a linear search would be the most appropriate method to search for a specific
card, and justify your answer.
Search method _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Justification
[3]
© OCR 2019. 1 of 20 PhysicsAndMathsTutor.com
2. A programmer is developing an ordering system for a fast food restaurant. When a member of staff inputs an
order, it is added to a linked list for completion by the chefs.
The user needs to be able to search for, and find, a specific order number.
State an appropriate search algorithm that could be used, and justify your choice against an alternative Search
algorithm.
Appropriate Search Algorithm
Justification
[3]
© OCR 2019. 2 of 20 PhysicsAndMathsTutor.com
3. The target integer 8 exists in a list of integers 1, 4, 6, 9, 8, 12, 15 but is not found during a binary search. There
are no errors in the code.
(i) Give the reason why the target integer 8 is not found.
[1]
(ii) Identify and describe an alternative search algorithm that could be used.
[3]
© OCR 2019. 3 of 20 PhysicsAndMathsTutor.com
4.
A country’s national rail operator provides an app for customers to purchase tickets. An array is used to store
the names of the stations on the network. Customers must enter a departure station into the app.
The current contents of the array are shown:
Cavalry Bridge Walkway Museum Monument Council Theatre Cinema
House
A linear search is used to check if the entered departure station exists in the array.
(i) Identify one precondition that is needed before a binary search could be used with the station array.
[1]
(ii) A user enters the departure station ‘Bridge Heights’
Explain how a linear search would check if the departure station exists in the array.
[4]
© OCR 2019. 4 of 20 PhysicsAndMathsTutor.com
5. A computer program stores data input on a stack named dataItems. The stack has two subprograms to add
and remove data items from the stack. The stack is implemented as a 1D array, dataArray.
Sub-program Description
push() The parameter is added to the top of the stack
pop() The parameter is added to the top of the stack
The current contents of dataItems are shown:
6
15
100
23
As an array, the data in dataArray is sorted and then searched for a specific value.
(i) The data in dataArray is sorted into ascending order using an insertion sort.
The current contents of dataArray are shown:
100 22 5 36 999 12
Show the steps of an insertion sort on the current contents of the array dataArray.
© OCR 2019. 5 of 20 PhysicsAndMathsTutor.com
[5]
(ii) The array dataArray can now be searched using a binary search.
Describe the stages of a binary search on an array of size n.
© OCR 2019. 6 of 20 PhysicsAndMathsTutor.com
[7]
(iii) The array has 50 items.
The function, searchItem(), performs a linear search for a data item.
function searchItem(dataItem)
for count = 0 to 49
if dataArray[count] == dataItem then
return(count)
endif
next count
return(-1)
endfunction
Rewrite the function using a while loop.
© OCR 2019. 7 of 20 PhysicsAndMathsTutor.com
[4]
© OCR 2019. 8 of 20 PhysicsAndMathsTutor.com
6(a). Describe the steps involved in a binary search to find the value 47 in the list below.
4, 7, 8, 21, 46, 47, 51
[4]
© OCR 2019. 9 of 20 PhysicsAndMathsTutor.com
(b). A programmer has been tasked with writing a function that uses a binary search to return a Boolean value. The
function should return true if the target integer is found in a list of integers. Using pseudocode, write an
algorithm for the function.
[8]
© OCR 2019. 10 of 20 PhysicsAndMathsTutor.com
7. Show how a binary search would be performed on the array shown in Fig. 4.2 to find the value ‘duck’.
Fig. 4.2
[3]
© OCR 2019. 11 of 20 PhysicsAndMathsTutor.com
8. A program stores entered data in a binary search tree.
The current contents of the tree are shown:
A pseudocode algorithm is written to search the tree to determine if the data item “Sweden‘ is in the tree.
The function currentNode.left()returns the node positioned to the left of currentNode.
The function currentNode.right()returns the node positioned to the right of currentNode.
function searchForData(currentNode:byVal, searchValue:byVal)
thisNode = getData (...................................)
if thisNode == ........................................ then
return ................................................
elseif thisNode < searchValue then
if currentNode.left() != null then
return (searchForData(currentNode.left(), searchValue))
else
return .....................................
endif
else
if ......................................... != null then
return (searchForData(currentNode.right(), searchValue))
else
return false
endif
endif
endfunction
© OCR 2019. 12 of 20 PhysicsAndMathsTutor.com
(i) Complete the algorithm.
[5]
(ii) The algorithm needs to be used in different scenarios, with a range of different trees.
Identify two preconditions needed of a tree for this algorithm to work.
[2]
END OF QUESTION PAPER
© OCR 2019. 13 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
1 1 mark for linear search, 2 for justification 3
Justification:
The array is not sorted (1)
Linear does not need ordered / linear
goes through all elements from
beginning / binary needs a sorted array
(1)
Total 3
2 Algorithm, max 1 3 No marks for justification if linear has not
AO1.1 been identified
(1)
linear AO2.1
(2)
Justification, 1 mark per bullet to max 2
Items do not have to be in a specific
order
Binary needs items in order Examiner’s Comment:
Many candidates correctly identified a
linear search and could justify the need for
it. However, a lot of candidates did answer
binary search without appreciating that the
data set needed to be in order first.
Total 3
3 i The integers in the list are unsorted (1) 1
Examiner's Comments
Most candidates correctly identified that a
list needs to be in order for a binary search
to be applied. However, a worrying number
of candidates thought that it could not be
applied because there was an even
number of items in the list, and hence no
clear mid-point.
© OCR 2019. 14 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
ii Identification (Max 1) 3 Accept serial
Examiner's Comments
Perform a linear search
Many candidates identified a linear search,
Description (Max 2) but fewer could give a full description as to
how a linear search operates. A number of
candidates confused searching with sorting
starting at the first element / each item algorithms.
is checked...
until value is found
or end of list reached and not found
Total 4
4 i The data needs to be sorted / in 1
alphabetical order AO2.1 Examiner’s Comments
(1)
Most candidates knew that the list had to
be sorted before a binary search could be
performed.
ii 1 mark per bullet to max 4 4
AO2.1
Start at the first item (Cavalry) (2)
Compare with departure station ‘Bridge AO2.2
Heights’ (2)
If matched, report found Examiner’s Comments
Otherwise continue to the next item in
list (Bridge) The majority of candidates described a
Continue until item found, or end of list linear search, but some described a binary
reached… search by mistake. Where candidates did
and then False returned describe a linear search, they generally did
so with sufficient precision.
Total 5
© OCR 2019. 15 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
5 i 1 mark per row (after first row) 5
AO2.2
(5)
100 22 5 36 999 12 Examiner’s Comments
22 100 5 36 999 12 1 mark
5 22 100 36 999 12 1 mark Candidates should be encouraged to
5 22 100 36 999 12 1 mark demonstrate sorting algorithms through the
5 12 22 36 100 999 1 mark use of clear diagrams that show the
steps/passes in the sorting algorithm.
Where candidates used verbose text, it
often made it far harder to follow whether
or not the correct sorting algorithm had
been applied. Most candidates did
implement an insertion sort, but some did
describe bubble or merge sorts instead.
ii 1 mark per bullet to max 7 7
AO1.1
Repeat (2)
Calculating an array midpoint… AO1.2
…by adding the array lower bound to (3)
the array upper bound, dividing by 2 AO2.1 Examiner’s Comments
and rounding (1)
Compare array midpoint with value to AO2.2 The paper title is ‘Algorithms and
search for… (1) programming’. Binary search is a standard
…if equal set found flag to true algorithm that should be fully understood
…if array midpoint < value to search by candidates, and candidates should be
for, change lowerbound to equal able to program it. Many candidates
midpoint + 1 produced very vague descriptions that
…if array midpoint > value to search were far too general to credit at this level.
for, change upperbound to equal Candidates needed to be able to discuss
midpoint - 1 how the upper and lower bound pointers
Until lowerbound is greater than or are used in this algorithm (or equivalent for
equal to upperbound recursive solutions).
Return/output found flag
© OCR 2019. 16 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
iii 1 mark per bullet 4
AO1.2
Setting variable to start at 0 (1)
Suitable while structure (endwhile or AO3.1
clear indentation) (1)
looping 50 times AO3.2
Incrementing the variable within the (2)
loop
e.g. 1 Examiner’s Comments
function searchItem(dataItem)
count = 0 It was a little disappointing to see a number
while count < 50
of candidates using variations on for loops
rather than rewriting the code using a while
if dataArray(count) == dataItem then
loop as required. A significant number of
return(count) candidates still struggled to demonstrate a
endif coherent logical response that would work
count = count + 1
for something that is relatively simplistic,
thus showing a lack of proficiency in coding
practice.
endwhile
return(-1)
endfunction
e.g. 2
function searchItem(dataItem)
count = 0
while count < 50 and
dataArray[count]!=dataItem
count = count + 1
endwhile
if count==50
count=-1
endif
return(count)
endfunction
Total 16
© OCR 2019. 17 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
6 a Find the middle point in the list / 21 / 4 Some marks such as the comparison may
element 4 be by implication if the candidate’s logic
Compare it to the value 47, false works
Is 47 greater than middle point, true
New subset is 46-51 / change lower Must refer to the list given in the question
bound to 46 / element 5 i.e. not a generic description
Find the middle of the new subset / 47 /
element 6 Is this value equal to 47, true Examiner's Comments
Search finishes
In general, many candidates had an
understanding of how a binary search
operated. Unfortunately, some gave a
generic description rather than answering
the specific question which required the
candidate to illustrate how a binary search
would operate on a specific data set. Some
candidates drew concise and elegant
diagrams with appropriate annotations that
made their answers much clearer than
those who wrote prose at great length.
© OCR 2019. 18 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
b Finding midPoint and correctly 8 Max 8 marks
checking if midPoint value is target
value ...
... and if so returning true Note: candidates may have given a
Correctly checking that all elements recursive algorithm and this should is
have been checked ... perfectly acceptable.
... and if so returning false
Identify top or bottom of list ... Examiner's Comments
... if top then leftPtr set/passed as
midPoint + 1 ... The Binary Search is one of the algorithms
... if bottom then rightPtr set/passed specifically identified in the specification
as midPoint – 1 that candidates need to be able to program
Correct use of indentation (AO2.1) and understand. It is a difficult algorithm to
code correctly and only the most able
Example iterative example candidates managed to produce a strong
response to give the degree of accuracy
required. Many candidates wrote in
structured English which was not
acceptable – the question specifically
required a pseudocode solution.
Candidates are not expected to be able to
write pseudocode in the form given by
OCR in the specification appendix, and
variations from various programming
languages were taken into account.
However, the overall ability to write
pseudocode proved to be a key
differentiator and many candidates should
aim to improve their ability to write
pseudocode before the A2 examination.
Total 12
7 1 mark for each bullet 3
Duck is smaller than goat
Duck is less than frog/elephant
Duck is equal to duck/less than
elephant so only duck left
Total 3
© OCR 2019. 19 of 20 PhysicsAndMathsTutor.com
Question Answer/Indicative content Marks Guidance
8 i 1 mark per bullet to max 5 5 The line elseif thisNode <
AO2.2 searchValue then should have read
function searchForData(currentNode:byVal, (2) elseif thisNode > searchValue
AO3.2
searchValue:byVal)
then
thisNode = getData(currentNode) (3)
if thisNode == searchValue then If candidates attempt to correct the code
return true and their answers are consistent with, and
elseif thisNode < searchValue then
work with their amendment, such answers
should be credited.
if currentNode.left () != null then
return (searchForData(currentNode.left
(), searchValue))
else
Examiner’s Comments
return false
endif Some candidates found the first two marks
more difficult to access than the last three,
else
because they did not fully understand that
if currentNode.right() != null then
the parameters to the function were to be
return (searchForData(currentNode.right used.
(), searchValue))
else
return false
endif
endif
endfunction
ii It’s a binary tree 2
It’s ordered / sorted AO2.2 Examiner’s Comments
(2)
Many candidates recognised that a binary
search tree was required, but some lost
marks when they specified that 2 children
were always required for each parent
node, when it is a maximum of two children
that are allowed, so that there can be 0, 1
or 2 children.
Total 7
© OCR 2019. 20 of 20 PhysicsAndMathsTutor.com
Powered by TCPDF (www.tcpdf.org)