Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (9618)
27 questions · page 2 of 2 · Clear filters
Question text
(a) Complete the pseudocode to find an item in a 1D array Widgets of type STRING, using a
8
linear search.
DECLARE Widgets : ARRAY[1:50000] OF STRING
DECLARE TopOfList : INTEGER
DECLARE EndOfList : INTEGER
DECLARE Count : INTEGER
DECLARE ToFind : STRING
DECLARE Found : BOOLEAN
DECLARE NotInList : BOOLEAN
1
TopOfList
50000
EndOfList
OUTPUT "Enter the name of the item you wish to find "
INPUT ToFind
…
FALSE
NotInList
TopOfList
Count
WHILE … AND …
IF … THEN
TRUE
Found
ENDIF
Count + 1
Count
IF … THEN
TRUE
NotInList
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Item found at position ", Count - 1, " in array"
ELSE
OUTPUT "Item not in array"
ENDIF
[4]
(b) Compare the methods used by the linear and binary search algorithms to find an item in an
array. Refer to Big O notation in your answer.
…
…
…
…
…
…
…
… [4]
Question text
10 (a) State a condition that must be true for an array to be searchable for a binary search.
…
… [1]
(b) Complete the given pseudocode to find an item in a 1D array Names of type STRING using a
binary search.
DECLARE Names : ARRAY[1:100000] OF STRING
DECLARE TopOfList : INTEGER
DECLARE EndOfList : INTEGER
DECLARE CurrentItem : INTEGER
DECLARE ToFind : STRING
DECLARE Found : BOOLEAN
DECLARE NotInList : BOOLEAN
←
TopOfList
1
←
EndOfList
100000
OUTPUT "Which name do you wish to find? "
INPUT ToFind
…
← FALSE
NotInList
WHILE … AND …
←
CurrentItem
(TopOfList + EndOfList) DIV 2
IF … THEN
←
Found
TRUE
ELSE
IF TopOfList >= EndOfList THEN
…
ELSE
IF ToFind > Names[CurrentItem] THEN
…
ELSE
←
EndOfList
CurrentItem – 1
ENDIF
ENDIF
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Item found at position ", CurrentItem, " in array"
ELSE
OUTPUT "Item not in array"
ENDIF
[5]
,
,
(c) Describe the performance of a binary search in relation to the number of data items in the
array being searched. Refer to Big O notation in your answer.
…
…
…
… [2]
Question text
10 A stack is to be set up using the information in the table.
Identifier
Data type
Description
BasePointer
INTEGER
points to the bottom of the stack
TopPointer
INTEGER
points to the top of the stack
Stack
REAL
1D array to implement the stack
A constant, with identifier Capacity, limits the size of the stack to 25 items.
(a) Write the pseudocode for the required declarations.
…
…
…
…
…
… [3]
(b) Complete the pseudocode function Pop() to pop an item from Stack.
// popping an item from the stack
FUNCTION Pop() …
DECLARE Item : REAL
0
Item
… BasePointer THEN
…
Item
…
TopPointer
ELSE
OUTPUT "The stack is empty – error"
ENDIF
…
ENDFUNCTION
[5]
(c) Compare and contrast the queue and stack Abstract Data Types (ADT).
…
…
…
…
…
… [2]
Question text
9
(a) A stack Abstract Data Type (ADT) is to be implemented using pseudocode, with procedures
to initialise it and to push new items onto the stack.
A 1D array Stack stores the contents of the stack.
(i)
Study the pseudocode in part (a)(ii) and complete the table of identifiers by writing the
missing data types and descriptions.
Identifier
Data type
Description
BasePointer
TopPointer
Stack
REAL
[2]
(ii)
Complete the pseudocode.
CONSTANT MaxSize = 40
DECLARE BasePointer : INTEGER
DECLARE TopPointer : INTEGER
DECLARE Stack : ARRAY[1:40] OF REAL
// initialisation of stack
PROCEDURE Initialise()
…
1
…
0
ENDPROCEDURE
// push an item onto the stack
PROCEDURE Push(NewItem : REAL)
… MaxSize THEN
…
…
Stack[TopPointer]
ENDIF
ENDPROCEDURE
[5]
(b) Justify the use of a linked list instead of an array to implement a stack.
…
…
…
… [2]
(c) Explain how a compiler makes use of a stack when translating recursive programming code.
…
…
…
…
…
…
…
… [4]
Question text
11
Pseudocode is to be written to implement a queue Abstract Data Type (ADT) with items of the
string data type. This will be implemented using the information in the table.
Identifier
Data type
Description
FrontPointer
INTEGER
points to the start of the queue
RearPointer
INTEGER
points to the end of the queue
Length
INTEGER
the current size of the queue
Queue
STRING
1D array to implement the queue
A constant, with identifier MaxSize, limits the size of the queue to 60 items.
(a) Write the pseudocode to declare MaxSize, FrontPointer, RearPointer, Length and
Queue.
…
…
…
…
…
… [3]
(b) Complete the following pseudocode for the function Dequeue to remove the front item from
the queue.
FUNCTION Dequeue RETURNS STRING
DECLARE Item : STRING
… > 0 THEN
Item ← …
…
IF Length = 0 THEN
CALL Initialise // reset the pointers
ELSE
IF FrontPointer > MaxSize THEN
… ← 1
ENDIF
ENDIF
ELSE
OUTPUT "The print queue was empty – error!"
Item ← ""
ENDIF
RETURN Item
ENDFUNCTION
[4]
(c) Explain how a new element can be added to the queue if it is implemented using two stacks.
…
…
…
…
…
… [4]
Question text
12 (a) Describe what is meant by recursion.
…
…
…
… [2]
(b) A Fibonacci sequence is a series of numbers formed by adding together the two preceding
numbers, for example:
0, 1, 1, 2, …
This function calculates and returns values in the Fibonacci sequence and uses recursion.
FUNCTION Fib(Number : INTEGER) RETURNS INTEGER
IF Number <= 1 THEN
Result ← Number
ELSE
Result ← Fib(Number – 1) + Fib(Number – 2)
ENDIF
RETURN Result
ENDFUNCTION
Complete the trace table for the function when it is called as Fib(5).
Call number
Function call
Number
Result
Return value
5
[5]
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 Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
11
(a) The pseudocode shown represents a queue Abstract Data Type (ADT) with procedures for
initialisation and to add new items. It is incomplete.
CONSTANT MaxLength = 50
DECLARE FrontPointer : INTEGER
DECLARE RearPointer : INTEGER
DECLARE Length : INTEGER
DECLARE Queue : ARRAY[0 : MaxLength – 1] OF STRING
// initialisation of queue
PROCEDURE Initialise
‑1
FrontPointer
…
…
0
ENDPROCEDURE
// adding a new item to the queue
PROCEDURE Enqueue(NewItem : STRING)
IF … THEN
…
RearPointer
IF RearPointer > MaxLength – 1 THEN
0
RearPointer
ENDIF
…
Length + 1
Length
ENDIF
ENDPROCEDURE
(i)
Study the pseudocode and insert the identifiers to complete this table.
Identifier
Data type
Description
STRING
An array to store the contents of the queue.
INTEGER
Points to the last item of the queue.
INTEGER
Indicates the number of items in the queue.
INTEGER
Points to the first item of the queue.
[2]
(ii)
Complete the given pseudocode.
[5]
(b) Explain the reasons why a queue ADT works better than a stack ADT in organising print jobs.
…
…
…
…
…
… [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 Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
12 (a) The array Names[0:99] is in alphabetical order.
Complete this pseudocode binary search algorithm.
← 0
Lower
…
←
Mid
0
←
Exit
FALSE
OUTPUT "Enter the name to be found "
INPUT Target
REPEAT
… THEN
OUTPUT Target, " does not exist"
←
Exit
TRUE
ENDIF
←
Mid
Lower + (Upper – Lower + 1) DIV 2
IF Names[Mid] < Target THEN
←
…
Lower
ENDIF
IF Names[Mid] > Target THEN
…
ENDIF
… THEN
OUTPUT Target, " was found at location ", Mid
←
Exit
TRUE
ENDIF
…
[6]
(b) Big O notation is used to classify efficiency of algorithms.
The Big O notation for time complexity in a binary search is O(log n).
(i)
State the Big O notation for time complexity of a linear search.
… [1]
(ii)
Describe the meaning of O(log n) as it applies to a binary search algorithm.
…
…
…
… [2]
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 Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
11
A simplified linked list is used to store the names of flowers in alphabetical order. It is implemented
using two 1D arrays:
•
Flower stores the names of the flowers.
•
NextPointer stores the pointer to the next flower name in the list.
HeadPointer indicates the index of the first flower name in the linked list.
HeadPointer
6
When the end of the linked list is reached, the next pointer has the value of 0.
The following table shows the initial content of the arrays.
Index
Flower
NextPointer
1
Rose
7
2
Marigold
1
3
Foxglove
10
4
Iris
9
5
Daisy
3
6
Dahlia
5
7
Saxifrage
0
8
Lupin
2
9
Lily
8
10
Hydrangea
4
(a) Several flower names have been deleted from the linked list. These are crossed out in the
following table.
Complete the table to show the new values of HeadPointer and NextPointer to keep the
remaining flower names in alphabetical order.
HeadPointer
Index
Flower
NextPointer
1
Rose
2
Marigold
3
Foxglove
4
Iris
5
Daisy
6
Dahlia
7
Saxifrage
8
Lupin
9
Lily
10
Hydrangea
[3]
(b) Complete the pseudocode algorithm so that it achieves the following when applied to the
arrays:
•
The flower name is input.
•
The linked list is searched, in order, for the flower name.
•
If the flower name is found, an appropriate message is output to indicate it has been
found.
•
If the flower name is not found, an appropriate message is output to indicate it has not
been found.
•
The algorithm terminates when the next pointer value is 0.
HeadPointer
Pointer
0
Found
OUTPUT "Enter a flower name "
… .
… .
IF Flower[Pointer] = FlowerName THEN
Pointer
Found
0
Pointer
ELSE
… .
ENDIF
ENDWHILE
… .
OUTPUT Flower[Found], " is found"
ELSE
… .
ENDIF
[5]
(c) Explain how you could improve the simplified linked list structure.
…
…
…
… [2]
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 Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
Question text
8
A binary search or a linear search can be used to look for a specific value in an array.
(a) Complete this pseudocode algorithm for a linear search.
DECLARE
MyList : ARRAY[0:9] OF INTEGER
DECLARE
MaxIndex : INTEGER
DECLARE
Index : INTEGER
DECLARE
Found : BOOLEAN
ValueToFind : …
DECLARE
INPUT ValueToFind
←
FALSE
Found
←
0
Index
←
…
MaxIndex
REPEAT
IF MyList[Index] = ValueToFind THEN
←
TRUE
Found
ENDIF
←
…
Index
UNTIL Found OR Index > MaxIndex
IF Found THEN
OUTPUT "Value found at position ", Index
ELSE
OUTPUT …
ENDIF
[4]
(b) (i)
State the necessary condition for a binary search.
…
… [1]
(ii)
Describe how to perform a binary search.
…
…
…
…
…
…
…
… [4]
(iii)
Explain how the performance of a binary search varies according to the number of values
in the array.
…
…
… [1]
(c) Compare the performance of the algorithms for a binary search and a linear search using
Big O notation for order of time complexity.
…
…
…
…
…
…
Question text
10 (a) State three essential features of recursion.
1 …
…
2 …
…
3 …
…
[3]
(b) Explain the reasons why a stack is a suitable Abstract Data Type (ADT) to implement
recursion.
…
…
…
…
…
… [3]
(c) Identify two ADTs other than a stack.
1 …
2 …
[2]
(d) The function StackFull() checks whether a stack is full.
The function uses the variable TopOfStack to represent the pointer to the most recent
position used on the stack, and the variable Max to represent the maximum size of the stack.
Assume TopOfStack and Max are global variables.
FUNCTION StackFull() RETURNS BOOLEAN
IF TopOfStack = Max THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
An algorithm AddInteger is required to add a new integer data element to a stack.
The stack is implemented as an array ArrayStack.
The function AddInteger() calls StackFull() and returns an appropriate message.
Complete the pseudocode for the function AddInteger().
FUNCTION AddInteger(NewInteger : INTEGER) RETURNS STRING
…
…
…
…
…
…
…
Question text
8
(a) State two factors that may affect the performance of a sorting algorithm.
…
…
…
… [2]
(b) The given algorithm is a simple bubble sort that arranges a set of scores stored in a one-
dimensional array into descending order, and orders the corresponding students’ names
stored into a two-dimensional array in the same order as the scores. All the arrays are indexed
from 1.
The contents of both arrays after sorting are shown.
Name
Score
1
2
1
1
98
Smithfield
Tom
2
2
97
Johnson
Jane
…
…
248 5
248 Peters
Jade
249 3
249 Allen
John
←
249
YearSize
←
TRUE
Flag
WHILE Flag = TRUE
←
FALSE
Flag
←
1 TO YearSize - 1
FOR Student
IF Score[Student] < Score[Student + 1] THEN
←
Score[Student]
Temp1
←
Name[Student,1]
Temp2
←
Name[Student,2]
Temp3
←
Score[Student + 1]
Score[Student]
←
Name[Student + 1,1]
Name[Student,1]
←
Name[Student + 1,2]
Name[Student,2]
←
Temp1
Score[Student + 1]
←
Temp2
Name[Student + 1,1]
←
Temp3
Name[Student + 1,2]
←
TRUE
Flag
ENDIF
NEXT Student
ENDWHILE
Write an algorithm, using pseudocode, that will perform the same task using an insertion sort.
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…