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

22 questions · page 2 of 2

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
16
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.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…

17
9608/41/M/J/18 · Question 2May/June 2018Paper 4116 marks
Loading question…
Question text

2
The array ItemList[1:20] stores data. A bubble sort sorts these data.
(a) Complete the pseudocode algorithm for a bubble sort.
20
01
MaxIndex
…
02
NumberItems
1 TO …
03
FOR Outer
1 to NumberItems
04
FOR Inner
IF ItemList[Inner] > …
05
06
THEN
ItemList[ … ]
07
Temp
ItemList[ … ]
08
ItemList[Inner]
…
09
ItemList[Inner + 1]
10
ENDIF
11
ENDFOR
…
12
NumberItems
13
ENDFOR
[7]
(b) The algorithm in part (a) is inefficient.
(i)
Explain why the algorithm in part (a) is inefficient.
…
…
…
… [2]
(ii)
Explain how you would improve the efficiency of this algorithm.
…
…
…
…
… [3]
(c) An insertion sort is another sorting algorithm.
State two situations when an insertion sort is more efficient than a bubble sort. Give a reason
for each.
Situation 1 …
…
Reason …
…
…
Situation 2 …
…
Reason …
…
…
[4]
96

18
9608/42/M/J/18 · Question 3May/June 2018Paper 4218 marks
Loading question…
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.
…
…
…
…
…
…
…
…

Advertisement
19
9608/42/M/J/18 · Question 6May/June 2018Paper 4213 marks
Loading question…
Question text

6
An Abstract Data Type (ADT) is used to create an unordered binary tree. The binary tree is created
as an array of nodes. Each node consists of a data value and two pointers.
A record type, Node, is declared using pseudocode.
TYPE Node
DECLARE DataValue : STRING
DECLARE LeftPointer : INTEGER
DECLARE RightPointer : INTEGER
ENDTYPE
The following statement declares an array BinaryTree.
DECLARE BinaryTree : ARRAY[0:14] OF Node
A variable, NextNode, points to the next free node.
The following diagram shows a possible node.
ArrayIndex
DataValue
LeftPointer
RightPointer
0
"Red"
1
2
The commands in the following table create and add nodes to the binary tree.
Command
Comment
CreateTree(NodeData)
Sets NextNode to 0.
Writes NodeData into DataValue at the position NextNode
Updates NextNode using NextNode = NextNode + 1
AttachLeft(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the LeftPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
AttachRight(NodeData, ParentNode)
Writes NodeData into DataValue of NextNode
Sets the RightPointer of node ParentNode to NextNode
Updates NextNode using NextNode = NextNode + 1
(a) The following commands are executed.
CreateTree("Red")
AttachLeft("Blue", 0)
AttachRight("Green", 0)
The following diagram shows the current state of the binary tree.
0
"Red"
1
2
1
"Blue"
2 "Green"
…
…
…
…
…
…
…
…
…
…
…
…
Write on the diagram to show the state of the binary tree after the following commands have
been executed.
AttachRight("Black", 2)
AttachLeft("Brown", 2)
AttachLeft("Peach", 3)
AttachLeft("Yellow", 1)
AttachRight("Purple", 1)
AttachLeft("White", 6)
AttachLeft("Pink", 7)
AttachLeft("Grey", 9)
AttachRight("Orange", 9)
[5]
(b) A new command has been added to initialise the pointers of the binary tree to − 1 to indicate
they are not in use.
A leaf is a node of the binary tree which has no children. In the case of this binary tree, a node
with a LeftPointer of − 1 and a RightPointer of − 1 is a leaf.
Write a recursive function, in program code, to traverse the binary tree and output the value
of DataValue for each leaf node.
Programming language …
Program code
…
…
…
…
…
…
…
…
…
…
…

20
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]

21
9608/41/M/J/17 · Question 3May/June 2017Paper 417 marks
Loading question…
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

22
9608/41/M/J/16 · Question 2May/June 2016Paper 417 marks
Loading question…
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]

Advertisement