Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (old syllabus, to 2021) (9608)
Quick quiz: 10 random questions from this topic. Answer them all, then open the Summary.
Question text
4
(a) A program has sorted some data in the array, List, in ascending order.
The following binary search algorithm is used to search for a value in the array.
FALSE
01
ValueFound
LengthOfList - 1
02
UpperBound
0
03
LowerBound
FALSE
04
NotInList
05
06
WHILE ValueFound = FALSE AND NotInList = FALSE
ROUND((LowerBound + UpperBound) / 2)
07
MidPoint
08
09
IF List[LowerBound] = SearchValue
10
THEN
TRUE
11
ValueFound
12
ELSE
13
IF List[MidPoint] < SearchValue
14
THEN
MidPoint + 1
15
UpperBound
16
ELSE
MidPoint – 1
17
UpperBound
18
ENDIF
19
IF LowerBound > MidPoint
20
THEN
TRUE
21
NotInList
22
ENDIF
23
ENDIF
24
ENDWHILE
25
26
IF ValueFound = FALSE
27
THEN
28
OUTPUT "The value is in the list"
29
ELSE
30
OUTPUT "The value is not found in the list"
31
ENDIF
Note:
The pseudocode function
ROUND(Reall : REAL) RETURNS INTEGER
rounds a number to the nearest integer value.
For example: ROUND(4.5) returns 5 and ROUND(4.4) returns 4
(i)
There are four errors in the algorithm.
Write the line of code where an error is present and write the correction in pseudocode.
Error 1 …
Correction …
Error 2 …
Correction …
Error 3 …
Correction …
Error 4 …
Correction …
[4]
(ii)
A binary search is one algorithm that can be used to search an array.
Identify another searching algorithm.
… [1]
(b) The following is an example of a sorting algorithm. It sorts the data in the array ArrayData.
""
01
TempValue
02
REPEAT
TRUE
03
Sorted
0 TO 4
04
FOR Count
05
IF ArrayData[Count] > ArrayData[Count + 1]
06
THEN
ArrayData[Count + 1]
07
TempValue
ArrayData[Count]
08
ArrayData[Count + 1]
TempValue
09
ArrayData[Count]
FALSE
10
Sorted
11
ENDIF
12
ENDFOR
13
UNTIL Sorted = TRUE
(i)
Complete the trace table for the algorithm given in part (b), for the ArrayData values
given in the table.
ArrayData
Count
TempValue
Sorted
0
1
2
3
4
5
5
20
12
25
32
29
[4]
(ii)
Rewrite lines 4 to 12 of the algorithm in part (b) using a WHILE loop instead of a FOR
loop.
…
…
…
…
…
…
…
Question text
3
Some algorithms can be written using recursion.
(a) State two features of recursion.
Feature 1 …
Feature 2 …
[2]
(b) Explain what a compiler has to do to implement recursion.
…
…
…
…
…
… [3]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
3
NameList is a 1D array that stores a sorted list of names. A programmer declares the array in
pseudocode as follows:
NameList : Array[0 : 100] OF STRING
The programmer wants to search the list using a binary search algorithm.
The programmer decides to write the search algorithm as a recursive function. The function, Find,
takes three parameters:
•
Name, the string to be searched for
•
Start, the index of the first item in the list to be searched
•
Finish, the index of the last item in the list to be searched
The function will return the position of the name in the list, or −1 if the name is not found.
Complete the pseudocode for the recursive function.
FUNCTION Find(BYVALUE Name : STRING, BYVALUE Start : INTEGER,
BYVALUE Finish : INTEGER) RETURNS INTEGER
// base case
IF …
THEN
RETURN −1
ELSE
…
Middle
IF …
THEN
RETURN …
ELSE
// general case
IF SearchItem > …
THEN
…
ELSE
…
ENDIF
ENDIF
ENDIF
[7]
ENDFUNCTION
Question text
5
There are several sorting algorithms. One type of sorting algorithm is an insertion sort.
(a) Explain how an insertion sort puts a set of data into ascending order.
…
…
…
…
…
…
…
…
…
… [4]
(b) The following algorithm performs a bubble sort. It is currently incomplete.
Complete the algorithm.
NumberOfItems – 2
Counter
REPEAT
FALSE
DataSwapped
0 TO …
FOR CurrentValue
IF DataList[CurrentValue] > DataList[CurrentValue + 1]
THEN
DataList[ … ]
ValueTemp
DataList[CurrentValue + 1]
DataList[CurrentValue]
…
DataList[CurrentValue + 1]
…
DataSwapped
ENDIF
ENDFOR
UNTIL DataSwapped = …
[5]
Question text
4
Each node of a binary tree is a record. Each record has a left pointer, an integer data value
between 0 and 100 inclusive, and a right pointer.
For example:
Item
Example data
LeftPointer
2
Data
34
RightPointer
3
(a) Write pseudocode to declare the record with the identifier Node.
…
…
…
…
…
…
… [2]
(b) Write pseudocode to declare a new node, Node100, and assign 100 to its data value, 1 to
the left pointer and 4 to the right pointer.
…
…
…
…
…
…
… [3]
(c) The ordered binary tree is stored as a 1D global array named BinaryTree of type Node.
RootNode and FreePointer are declared as global variables.
A null pointer is represented by –1.
The current state of the binary tree is shown in the following table:
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
1
23
3
FreePointer
6
[1]
-1
5
2
[2]
-1
8
4
[3]
5
100
-1
[4]
-1
9
-1
[5]
-1
88
-1
[6]
-1
null
-1
[7]
-1
null
-1
(i)
State the purpose of the free pointer.
…
… [1]
(ii)
Identify an appropriate integer value to represent null data.
… [1]
(iii)
Draw the current state of the binary tree.
[2]
(iv)
The procedure AddData():
•
takes the node to be added to the tree as a parameter
•
finds the location for the node to be stored
•
stores the node in the next free array index
•
stores –1 in the new node’s LeftPointer and RightPointer
•
updates the pointers in the other nodes
•
updates FreePointer.
Complete the pseudocode for the procedure AddData().
PROCEDURE AddData(NewNode)
…
BinaryTree[FreePointer]
BinaryTree[FreePoi
Question text
2
(a) (i)
State what is meant by a recursively defined procedure.
…
… [1]
(ii)
Write the line number from the pseudocode shown in part (b) that shows the
procedure X is recursive. …
[1]
(b) The recursive procedure X is defined as follows:
01 PROCEDURE X(Index, Item)
02
IF MyList[Index] > 0
03
THEN
04
IF MyList(Index) >= Item
05
THEN
06
MyList[Index] ← MyList[Index + 1]
07
ENDIF
08
CALL X(Index + 1, Item)
09
ENDIF
10 ENDPROCEDURE
An array MyList is used to store a sorted data set of non-zero integers. Unused cells contain
zero.
1
2
3
4
5
6
7
8
9
10
MyList
3
5
8
9
13
16
27
0
0
0
(i)
Complete the trace table for the dry-run of the pseudocode for the procedure
CALL X(1, 9).
MyList
Index
Item
1
2
3
4
5
6
7
8
9
10
3
5
8
9
13
16
27
0
0
0
1
9
[4]
State the purpose of procedure X when used with the array MyList.
(ii)
…
… [1]
Question text
3
The arrays PollData[1:10] and CardData[1:10] store data.
PollData
12
85
52
57
25
11
33
59
56
91
CardData
11
12
25
33
52
56
57
59
91
85
An insertion sort sorts these data.
(a) State why it will take less time to complete an insertion sort on CardData than on PollData.
…
… [1]
(b) The following pseudocode algorithm performs an insertion sort on the CardData array.
Complete the following pseudocode algorithm.
01
ArraySize ← 10
FOR Pointer ← 2 TO …
02
03
ValueToInsert ← CardData[Pointer]
HolePosition ← …
04
WHILE (HolePosition > 1 AND ( … > … ))
05
CardData[HolePosition] ← CardData[ … ]
06
HolePosition ← …
07
08
ENDWHILE
CardData[HolePosition] ← …
09
10
ENDFOR
[7]
(c) (i)
A binary search algorithm is used to find a specific value in an array.
Explain why an array needs to be sorted before a binary search algorithm can be used.
…
…
…
…
…
…
…
… [2]
(ii)
The current contents of CardData are shown.
11
12
25
33
52
56
57
59
85
91
Explain how a binary search will find the value 25 in CardData.
…
…
…
…
…
…
…
…
Question text
12 ENDFUNCTION
(a) State the line number in the pseudocode algorithm that shows function NumberPattern()
is recursive. Justify your choice.
Line number …
Justification …
…
…
[2]
(b) The function is called as follows:
NumberPattern(1,1,12)
Dry run the algorithm and complete the following trace table. State the final value returned.
Show your working.
Value1
Value2
Temp
EndValue
OUTPUT
RETURN value
Final value returned …
Working
…
…
…
…
…
…
…
[5]
(c) State the purpose of the algorithm.
…
… [1]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of the University of
Question text
1
An array, NumberArray, stores 100 integer values. The array needs to be sorted into ascending
numerical order.
(a) Describe how an insertion sort will sort the data in NumberArray.
…
…
…
…
…
…
…
…
…
… [4]
(b) Another type of sorting algorithm is a bubble sort.
The procedure Bubble() takes an array as a parameter. It performs a bubble sort on the
array. The sorting algorithm stops as soon as all the elements are in ascending order.
Complete the procedure Bubble().
PROCEDURE Bubble(BYREF NumberArray : ARRAY[0 : 99] OF INTEGER)
DECLARE Outer : INTEGER
DECLARE Swap : BOOLEAN
DECLARE Inner : INTEGER
DECLARE Temp : INTEGER
LENGTH(NumberArray) - 1
Outer
REPEAT
…
Inner
FALSE
Swap
REPEAT
IF NumberArray[Inner] > NumberArray[Inner + 1]
THEN
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
NumberArray[Inner]
Temp
NumberArray[Inner + 1]
…
Swap
ENDIF
Inner + 1
Inner
UNTIL Inner = …
Outer - 1
Outer
UNTIL Swap = … OR Outer = …
ENDPROCEDURE
[5]
Question text
2
A computer games club wants to run a competition. The club needs a system to store the scores
achieved in the competition.
A selection of score data is as follows:
99, 125, 121, 97, 109, 95, 135, 149
(a) A linked list of nodes will be used to store the data. Each node consists of the data, a left
pointer and a right pointer. The linked list will be organised as a binary tree.
(i)
Complete the binary tree to show how the score data above will be organised.
RootPointer
The symbol ∅ represents a null pointer.
LeftPointer
RightPointer
99
97
125
∅
121
[5]
(ii)
The following diagram shows a 2D array that stores the nodes of the binary tree’s linked
list.
Add the correct pointer values to complete the diagram, using your answer from
part (a)(i).
RootPointer
Index LeftPointer
Data
RightPointer
0
99
0
1
125
2
121
3
97
FreePointer
4
109
5
95
6
135
7
149
8
[6]
(b) The club also considers storing the data in the order in which it receives the scores as a
linked list in a 1D array of records.
The following pseudocode algorithm searches for an element in the linked list.
Complete the six missing sections in the algorithm.
FUNCTION FindElement(Item : INTEGER) RETURNS …
… ← RootPointer
WHILE CurrentPointer … NullPointer
IF List[CurrentPointer].Data <> …
THEN
CurrentPointer ← List[ … ].Pointer
ELSE
RETURN CurrentPointer
ENDIF
ENDWHILE
CurrentPointer ← NullPointer
… CurrentPointer
ENDFUNCTION
[6]
(c) The games club is looking at two programming paradigms: imperative and object-oriented
programming paradigms.
Describe what is meant by the imperative programming paradigm and the object-oriented
programming paradigm.
(i)
Imperative …
…
…
…
…
… [3]
(ii)
Object-oriented …
…
…
…
…
… [3]
(d) Players complete one game to place them into a category for the competition. The games club
wants to implement a program to place players into the correct category. The programmer
has decided to use object-oriented programming (OOP).
The highest score that can be achieved in the game is 150. Any score less than 50 will not
qualify for the competition. Players will be placed in a category based on their score.
The following diagram shows the design for the class Player. This includes the properties
and methods.
Player
Sco