OCR Unit 12 Assessment
OCR Unit 12 Assessment
Unit 12 Algorithms
Assessment test
1. (a) (i) What is an algorithm? [1]
set of instructions (a series of steps) to solve a given problem
(ii) List three properties of a good algorithm. [3]
Finiteness: It must have a finite number of steps and eventually terminate.
Correctness: It should solve the problem and produce the correct output for all
valid inputs ( syntax and logical).
Efficiency: It should be efficient in terms of time and space, using minimal
resources to achieve the solution.
(4 marks)
2. (a) Explain how a serial search can be used to find the word mongoose in the list
baboon, cheetah, elephant, giraffe, leopard, lion, mongoose, rhinoceros [2]
List: "baboon", "cheetah", "elephant", "giraffe", "leopard", "lion", "mongoose",
"rhinoceros"
Serial search (also known as linear search) works by checking each element of the list one
by one, starting from the beginning, until the target word (in this case, "mongoose") is
found.
The search starts with "baboon" and moves through each subsequent word in the list.
When "mongoose" is reached, the search ends and the position of the word is returned.
(b) Explain one disadvantage of a serial search compared with a binary search of any
large sorted list of data. [2]
A disadvantage of a serial search is that it can be slow on large lists because it
checks each item sequentially, one after the other. In the worst-case scenario, it may need to
check every single element. In contrast, a binary search is more efficient, halving the search
space with each step, which makes it much faster on sorted data, reducing the number of
comparisons needed.
(c) The binary search algorithm below processes an array called nameList. The names
in nameList are stored in ascending alphabetical order and low, high, guess are all
integer variables. animal contains the string being searched for in nameList. The
operator div performs integer division.
found = False
high = len(nameList)-1
1
Assessment test
Unit 12 Algorithms
low = 0
while found == False and high >= low
guess = (low + high) div 2
if nameList[guess] == animal then
found = True
else
if nameList[guess] > animal then
high = guess – 1
else
low = guess + 1
endif
endif
endwhile
if found == True
print (“animal found in position ”, guess)
else
print (“animal not in list”)
endif
(i) Complete the following trace table when animal = "lion" using the array:
(iii) What will be output if the user searches for “eland”? [1]
(iv) How many guesses would be needed to find an item in a list of 2000 items? [1]
11
(11 marks)
3. (a) The Big-O notation gives a measure of the time complexity of an algorithm relative to
the size of the problem.
Three different algorithms A, B, C to solve the same problem are:
2
Assessment test
Unit 12 Algorithms
A = O(n2), B = O(log n) and C = O(n!).
(i) Which is the most efficient algorithm in terms of execution time? [1]
B = O(log n)
(ii) Which is the least efficient algorithm in terms of execution time? [1]
C = O(n!)
(b) Use the Big-O notation to express the complexity of the following pseudocode
segments. In each case, show how you arrive at the answer.
(i) i = n
while i > 0
i = i DIV 2 # (DIV =integer division)
endwhile [2]
The number of iterations will be approximately log2n because i is halved in each step.
Therefore, the time complexity is O(log n).
Answer: O(log n)
(ii) for i = 1 to n
for j = 1 to n
for k = 1 to n
listX[i, j, k] = 0
next k
next j
next i [2]
There are 3 nested loops, each running from 1 to n.
The total number of iterations is n×n×n=n3n \times n \times n = n^3n×n×n=n3.
Thus, the time complexity is O(n³).
Answer: O(n³)
procedure bigO(aList)
numberOfPrints = 0
n = length(aList)
for i = 0 to n-1
print("In outer loop: ", aList[i])
numberOfPrints = numberOfPrints + 1
k = n/2 - 1
for j = 0 to k
3
Assessment test
Unit 12 Algorithms
print(" In inner loop: ",aList[j])
numberOfPrints = numberOfPrints + 1
next j
next i
print("number Of Print statements executed: ",
numberOfPrints)
numberOfPrints = numberOfPrints + 1
endprocedure
#main program
listOfItems = [1, 2, 3, 4, 5, 6]
bigO(listOfItems)
(i) Calculate the number of times a print statement is executed in the subroutine
bigO(aList), called from the main program and accepting as a parameter a list
of 6 items.
[2]
6 x 3 = 18
6 + 18 = 24
24+1=25
answer= 25
(ii) Hence express its time complexity using Big-O notation and explain your answer.
[2]
the outer loop runs n times, and for each outer loop iteration, the inner loop runs about
n/2n/2n/2 times.
The total number of operations is proportional to n2n^2n2, so the time complexity is
O(n2)O(n^2)O(n2).
Answer: O(n²)
(10 marks)
4. The following pseudocode subroutine merges two sorted lists of equal or unequal lengths.
It is called with the two lists shown in the main program.
NOTE: Given that list1 = [1, 2, 5, 10], then list1[1:] = [2, 5, 10].
That is, it returns a sublist starting with list1[1].
4
Assessment test
Unit 12 Algorithms
6. return list2
7. endif
8. if length (list2) = 0 then
9. return list1
10. endif
11. if list1[0] <= list2[0] then
12. return [list1[0]] + merge(list1[1:], list2)
13. endif
14. if list2[0] <= list1[0] then
15. return [list2[0]] + merge(list1, list2[1:])
16. endif
17. endfunction
18.
19. #main program
20. list1 = [3, 7, 9]
21. list2 = [1, 2, 5, 10]
22. mergedList = merge(list1,list2)
23. print(mergedList)
[1, 2, 3, 5, 7, 9, 10]
(ii) Give the statement numbers of the recursive calls in the subroutine. [2]
12, 15
(iii) What parameters will be passed to the subroutine the first, second and third times
that a recursive call is made? [3]
First time: merge([3, 7, 9], [1, 2, 5, 10])
procedure p(listA)
i = length(listA)-1
flag = True
while i > 0 and flag = True
flag = False
for j = 0 to i - 1
if listA[j] > listA[j + 1] then
temp = listA[j]
5
Assessment test
Unit 12 Algorithms
listA[j] = listA[j + 1]
listA[j + 1] = temp
flag = True
endif
next j
i = i - 1
endwhile
endprocedure
The array listA[19,10,21,7,26,16] is to be processed by this procedure.
(a) List the array after the WHILE loop has been executed once. [1]
[10, 19, 7, 21, 16, 26]
(b) What algorithm does the procedure p describe? [1]
Bubble Sort
(c) What is the purpose of flag in this procedure? [2]
It tracks whether any swaps were made in the current pass. If no swaps are made, the
array is already sorted, and the algorithm terminates early.
4 true 0 2 5
4 true 1 5 8
4 true 2 8 12
4 true 3 12 16
(10 marks)
6. Djikstra’s algorithm finds the shortest path between a given start node and every other node
in a weighted graph.
Shown below is a weighted graph.
6
Assessment test
Unit 12 Algorithms
In the algorithm below to find the shortest distances from A to the other nodes, a tentative
distance from A to every other node is initially assigned.
(a) In the first row of the table below, show what these tentative distances are. [2]
(b) In the second row of the table, show the tentative distances after A and the next
node have been visited. [3]
A B C D E F
Initial tentative
0 10 12 6 8
distance
tentative distance
after 2 nodes 0 10 12 6 15 18
visited
7
Assessment test
Unit 12 Algorithms
(b) Name and describe briefly another approach to solving a problem if no algorithm can
be found which will find a solution in a reasonable time. [2]
One approach to solving a problem when no efficient algorithm exists is heuristic methods.
Heuristics are strategies or techniques that aim to find a solution to a problem quickly
when classical algorithms are too slow or do not exist. While heuristics do not
guarantee an optimal solution, they provide a good enough or approximate solution in a
reasonable amount of time.
(4 marks)
[Total 50 marks]