Question 1
13 marks
1(a) 6 marks
A program makes use of searching and sorting algorithms.
The following incomplete pseudocode algorithm uses a binary search to find the integer numberToFind in the array array. It returns the index of the array or -1 if the integer is not found.
Complete the pseudocode algorithm.
function binarySearch(array,……)
lowerbound = 0
upperbound = array.length - 1
while true
if(upperbound < lowerbound) then
return ……
else
mid = (upperbound + lowerbound) ……
if(array[mid] <numberToFind) then
lowerbound = mid ……
elseif(array[mid] > numberToFind) then
upperbound = mid ……
else
return ……
endif
endif
endwhile
endfunction
Mark scheme 1(a)
Tick each point your answer makes. Your mark is worked out from your ticks, and you can change it.
1 mark for each completed statement
function binarySearch(array, numberToFind)
lowerbound = 0
upperbound = array.length - 1
while true
if(upperbound <lowerbound) then
return -1
else
mid = (upperbound + lowerbound)DIV 2
if(array[mid] <numberToFind) then
lowerbound = mid + 1
elseif(array[mid] > numberToFind)
then
upperbound = mid - 1
else
return mid
endif
endif
endwhile
endfunction
Guidance
Accept
mid = int((upperbound + lowerbound)/ 2)
Examiner’s Comments
Binary search (in both iterative and recursive form) is a standard algorithm that candidates are expected to be able to code at AS Level. Candidates made a number of common errors including not using integer division (DIV and / were accepted) for the midpoint calculation.
Some candidates returned the value being searched for at the array index rather than returning mid as the index as specified in the question.
Another common error that was made was to update mid by the wrong value, swapping the +1 and -1 round.
1(b)
An array stores the following data:
| 20 | 8 | 33 | 16 |
|---|
1(b)(i) 5 marks
Describe how the given data will be sorted into descending numerical order using an insertion sort.
You should refer to the data in this array throughout your answer.
Mark scheme 1(b)(i)
Tick each point your answer makes. Your mark is worked out from your ticks, and you can change it.
1 mark per bullet to max 5
-
20 becomes the sorted list / [20] is the sorted list
-
... take 8 and compare against ...
-
... it is less than 20 so keep it in place / [20 8] is now the sorted list
-
Take 33 and compare it to each value in the sorted list. It is greater than 20, so move 20 and 8 and insert 33 / 33 is compared and moved down and [33 20 8] is now the sorted list
-
Repeat for all other elements.
Guidance
Max 4 if ascending and not descending
Zero marks if not provided a description and just shown data values
Examiner’s Comments
Insertion sort is one of the standard sorting algorithms that candidates need to be familiar with using. A pleasing number of candidates made a reasonable start to the
response, but fewer could produce a comprehensive response.
The question required candidates to describe and not just show how insertion sort worked, so responses that only gave diagrams showing numbers moving with no annotation did not gain credit. Some candidates mistakenly described bubble sort or merge sort.
Quite often, where candidate scored four marks, they omitted the first pass of the sort where the first item (20) is set as the sorted list. Some potentially quite good responses were too vague as to how the next item at the start of the next pass filtered back through the sorted list to its correct position.
1(b)(ii) 2 marks
The size of the array has now been increased to seven elements.
The insertion sort algorithm needs to be tested to ensure it sorts a range of test data into descending numerical order.
For example, the test data in the array here will test to see if the insertion sort will sort data in the opposite order.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|
Give two other different sets of data in the array that can be used to test the insertion sort and state the purpose of each set of test data.
Each test needs to have a different purpose.
Set One
Test Data 1
Purpose of test data 1
Set Two
Test Data 2
Purpose of test data 2
Mark scheme 1(b)(ii)
Tick each point your answer makes. Your mark is worked out from your ticks, and you can change it.
1 mark for each set of test data and purpose
e.g.
-
Test Data: 7 6 5 4 3 2 1 Purpose: Testing that data is already in order
-
Test Data: 1 7 2 6 3 5 4 Purpose: Testing that mixed data is sorted
-
Test Data: Six One Seven Three Four Fix Two
-
Purpose: Testing that words are rejected/ Testing words are sorted (alphabetically)
-
Test Data: -1, 0, 2, -1, 3, -3
-
Purpose: Testing the algorithm works with negative numbers.
Guidance
Each purpose must be different.
Accept viable alternatives
The purpose must be relevant to the test data given.
A mark cannot be award unless both the test data and purpose are given.
Examiner’s Comments
Many candidates gained some credit for giving a valid reason for a set of test data that they presented. Clear responses included testing decimals, negatives, different data types, and testing on data
that was not already in order.