Section Three Fundamentals of algorithms

AQA A level Computer Science
Teaching and Learning Resources
Section Three Fundamentals of algorithms
PRACTICE QUESTIONS (WITH ANSWERS)
1
Two methods for searching a dataset are a binary search and a linear search.
a)
Write pseudo-code to show how a binary search works.
FindMe stores the record title that we are searching for
LowestPointer
1
HighestPointer
NumberofRecords
Do
MiddlePointer
(LowestPointer + HighestPointer) / 2
If Record at MiddlePointer < FindMe Then
LowestPointer
MiddlePointer +1
End If
If Record at MiddlePointer > FindMe Then
HighestPointer
MiddlePointer -1
End If
Until Record at MiddlePointer = FindMe
b)
Explain how efficient this method is on a large ordered set of data, compared
to a linear search.
The linear search is much more efficient as it will halve the amount of records it
looks at each time. A million records would only take 20 ’looks‘. It has the time
complexity of O(logN). The linear search will have to look through every record
until it finds what it is looking for. It has a time complexity of O(N).
2
The following graph shows the distance between five towns.
10
Town A
Town B
30
Town C
20
Town E
20
10
30
Town D
B
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
a)
Show this as an adjacency matrix.
A
B
C
D
E
A
∞
10
20
∞
∞
B
10
∞
20
∞
20
C
20
20
∞
30
∞
D
∞
∞
30
∞
10
E
∞
20
∞
10
∞
b) Show this as an adjacency list.
Node
Adjacent nodes
Town A
B,10,C,30
Town B
A,10,C,20,E,20
Town C
A,30,B,20,D,10
Town D
C,30,E,10
Town E
B,20,D,10
3
Convert the following infix expressions into Reverse Polish Notation (RPN)
a) 5 + 6
56+
b) 4 * 7 + 12
4 7 * 12 +
4
Explain one advantage of using RPN over infix notation.
It is in a format that is used by an interpreter and therefore RPN expressions can be
evaluated quicker than infix.
5
A binary search tree is used to store data arriving in this sequence: d, c, a, f, b, g
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
a)
Draw a binary search tree to show how this could be implemented.
d
c
f
g
a
b
b)
Write pseudo-code to add data to this binary search tree.
‘ Find next gap in the Node array
NodeCount
1
While Node(NodeCount) is not empty
NodeCount
NodeCount + 1
End While
‘ NodeCount stores the next blank
Node(NodeCount)
AddItem
‘ start at the root node
PresentNode
1
While Node(PresentNode) is not empty do
‘ Branch Left or Right?
If AddItem < Node(PresentNode) Then
‘ If Left branch is empty then assign NodeCount
If Left(PresentNode) = 0 Then
Left(PresentNode)
NodeCount
End If
PresentNode
Left(PresentNode)
Else
‘ If Right branch is empty then assign NodeCount
If Right(PresentNode) = 0 Then
Right(PresentNode)
NodeCount
End If
PresentNode
Right(PresentNo)
End If
End While
AQA A level Computer Science
© Hodder & Stoughton Limited 2015
AQA A level Computer Science
Teaching and Learning Resources
c)
Write pseudo-code to carry out a traversal that will put this data into alphabetic order.
Set current node as root
Traverse
End
Define Procedure Traverse
If there is a node to the left then
Move left to child node
Traverse
End If
Visit the root
If there is a node to the right then
Move right to child node
Traverse
End If
End Procedure
AQA A level Computer Science
© Hodder & Stoughton Limited 2015