Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (old syllabus, to 2021) (9608)

11 questions · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9608/41/O/N/21 · Question 5Oct/Nov 2021Paper 418 marks
Loading question…
Question text

5
The recursive algorithm for the Recursion() function is defined in pseudocode as follows:
FUNCTION Recursion(A, B : INTEGER) RETURNS INTEGER
IF A <= 100
THEN
RETURN 1
ELSE
IF A > B
THEN
RETURN 5 + Recursion(A - 1, B)
ELSE
RETURN 10 + Recursion(A – 10, B)
ENDIF
ENDIF
ENDFUNCTION
(a) The function is called with the following pseudocode statement:
OUTPUT Recursion(104, 102)
Dry run the function and complete the trace table. Give the output the program will produce.
Trace table:
A
B
Function call
Return value
Output = …
Working …
…
…
…
…
…
[4]
(b) Rewrite the function Recursion() in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
… [4]

2
9608/41/O/N/21 · Question 8Oct/Nov 2021Paper 4162 marks
Loading question…
Question text

8
Data entered into a computer is stored in an ordered binary tree.
The binary tree is stored in a 2D array, BinaryTree.
The first element of the array is index 0.
(a) The current contents of the binary tree are:
50
35
67
2
43
52
77
Complete the LeftPointer and RightPointer values in the following table for the binary
tree shown.
A null pointer is represented by –1.
RootNode
Index
LeftPointer
Data
RightPointer
0
[0]
50
[1]
67
[2]
77
[3]
35
[4]
2
[5]
43
[6]
52
[7]
[8]
[9]
[10]
[2]
(b) A post-order tree traversal outputs the left node, then the right node, then the root node.
In the tree given in part (a), the post-order tree traversal would output:
2
43
35
52
77
67
50
Complete the following recursive pseudocode algorithm PostOrder().
PROCEDURE PostOrder( … : INTEGER)
IF BinaryTree[RootNode, 0] <> -1
THEN
… (BinaryTree[RootNode, … ])
ENDIF
IF BinaryTree[RootNode, 2] <> -1
THEN
… (BinaryTree[RootNode, 2])
ENDIF
OUTPUT BinaryTree[RootNode, … ]
ENDPROCEDURE
[5]

3
9608/42/O/N/21 · Question 1Oct/Nov 2021Paper 429 marks
Loading question…
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]

Advertisement
4
9608/42/O/N/21 · Question 4Oct/Nov 2021Paper 4242 marks
Loading question…
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

5
9608/42/O/N/21 · Question 5Oct/Nov 2021Paper 4211 marks
Loading question…
Question text

5
Study the following recursive pseudocode algorithm.
FUNCTION Recursive(Num1, Num2 : INTEGER) RETURNS INTEGER
IF Num1 > Num2
THEN
RETURN 10
ELSE
IF Num1 = Num2
THEN
RETURN Num1
ELSE
RETURN Num1 + Recursive(Num1 * 2, Num2)
ENDIF
ENDIF
ENDFUNCTION
(a) The function is called as follows:
Recursive(1, 15)
Dry run the function and complete the trace table. Give the final return value.
Trace table:
Num1
Num2
Function call
Return value
Final return value …
Working …
…
…
…
…
[4]
(b) Rewrite the function Recursive() in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

6
9608/41/O/N/20 · Question 5Oct/Nov 2020Paper 419 marks
Loading question…
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]

7
9608/41/O/N/20 · Question 12Oct/Nov 2020Paper 418 marks
Loading question…
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

8
9608/42/O/N/20 · Question 1Oct/Nov 2020Paper 4212 marks
Loading question…
Question text

1
There are several different searching and sorting algorithms.
(a) Identify two sorting algorithms.
1 …
2 …
[2]
(b) Consider the following pseudocode algorithm.
0
LowerBound
LengthOfList – 1
UpperBound
FALSE
ValueFound
OUTPUT "Value to find: "
INPUT ValueToFind
WHILE ValueFound = FALSE AND UpperBound <> LowerBound
(LowerBound + UpperBound)DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
TRUE
ValueFound
ELSE
IF List[MidPoint] < ValueToFind
THEN
MidPoint + 1
LowerBound
ELSE
MidPoint – 1
UpperBound
ENDIF
ENDIF
ENDWHILE
IF ValueFound = FALSE
THEN
(LowerBound + UpperBound) DIV 2
MidPoint
IF List[MidPoint] = ValueToFind
THEN
OUTPUT "Item in position " & MidPoint & " in list"
ELSE
OUTPUT "Not in list"
ENDIF
ELSE
OUTPUT "Item in position " & MidPoint & " in list"
ENDIF
Note: DIV is an operator that performs integer division.
The array List contains the following values:
2, 5, 21, 25, 36, 48, 51, 59, 65, 70
(i)
Complete the trace table to show a dry run of the algorithm, when the value 21 is input.
LowerBound
UpperBound
ValueFound
ValueToFind
MidPoint
[3]
(ii)
Identify this type of searching algorithm.
… [1]
(iii)
The value 59 is input.
State the number of times the while loop condition is executed.
… [1]
(iv)
State the minimum number of times the while loop condition will be executed to search
for a value.
… [1]
(v)
MidPoint is calculated and checked again after the while loop is terminated.
Explain why this additional calculation and check is necessary.
…
…
…
… [2]
(vi)
A new data set is used as follows:
5, 9, 10, 12, 15, 13, 17, 19, 20, 2
Explain why the algorithm will not find the value 2 in this data set.
…
…
…
… [2]

Advertisement
9
9608/41/O/N/18 · Question 4Oct/Nov 2018Paper 4115 marks
Loading question…
Question text

4
The recursive algorithm for the Calculate() function is defined as follows:
01 FUNCTION Calculate(BYVALUE Number : INTEGER) RETURNS INTEGER
02
IF Number = 0
03
THEN
−10
04
Calculate
05
ELSE
Number * Calculate(Number − 1)
06
Calculate
07
ENDIF
08 ENDFUNCTION
(a) (i)
State what is meant by a recursive algorithm.
…
… [1]
(ii)
State the line number in Calculate() where the recursive call takes place.
… [1]
Question 4(b) begins on the next page.
(b) The function is called with Calculate(3).
Dry run the function and complete the trace table below. State the final value returned. Show
your working.
01 FUNCTION Calculate(BYVALUE Number : INTEGER) RETURNS INTEGER
02
IF Number = 0
03
THEN
−10
04
Calculate
05
ELSE
Number * Calculate(Number − 1)
06
Calculate
07
ENDIF
08 ENDFUNCTION
Working …
…
…
…
Trace table:
Number = 0 ?
Call number
Function call
Return value
Final return value …
[6]
(c) A recursive algorithm within a subroutine can be replaced with an iterative algorithm.
(i)
Describe one problem that can occur when running a subroutine that has a recursive
algorithm.
…
…
…
… [2]
(ii)
Rewrite the Calculate() function in pseudocode, using an iterative algorithm.
…
…
…
…
…
…
…
…
…

10
9608/42/O/N/18 · Question 3Oct/Nov 2018Paper 4213 marks
Loading question…
Question text

3
A bubble sort algorithm is used to sort an integer array, List. This algorithm can process arrays
of different lengths.
(a) Write pseudocode to complete the bubble sort algorithm shown.
… TO 0 STEP - 1
01 FOR Outer
0 TO ( … )
02
FOR Inner
IF …
…
03
>
04
THEN
…
05
Temp
…
06
List[Inner]
…
07
List[Inner + 1]
08
ENDIF
09
ENDFOR
10 ENDFOR
[7]
(b) (i)
State the order of the sorted array.
… [1]
(ii)
State which line of the algorithm you would change to sort the array into the opposite
order.
State the change you would make.
Line …
Change …
…
[1]
(c) Use pseudocode to write an alternative version of this bubble sort algorithm that will exit the
algorithm when the list is fully sorted.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

11
9608/42/O/N/17 · Question 4Oct/Nov 2017Paper 426 marks
Loading question…
Question text

4
(a) The array Numbers[0 : Max] stores numbers. An insertion sort can be used to sort these
numbers into ascending order.
Complete the following pseudocode for the insertion sort algorithm.
1 TO (Max - 1)
FOR Pointer
…
ItemToInsert
…
CurrentItem
WHILE (CurrentItem > 0) AND (Numbers[CurrentItem – 1] > ItemToInsert)
Numbers[CurrentItem - 1]
Numbers[ … ]
CurrentItem – 1
CurrentItem
ENDWHILE
…
Numbers[CurrentItem]
ENDFOR
[4]
(b) Identify two features of the array Numbers that would have an impact on the performance of
this insertion sort algorithm.
1 …
2 …
[2]

Advertisement