Merge Insertion Bubble Linear and Binary Search
Merge Insertion Bubble Linear and Binary Search
01 procedure insertionSort(dataArray:byRef)
02 for i = 1 to dataArray.Length - 1
03 temp = dataArray[i]
04 tempPos = i – 1
05 exit = false
06 while tempPos >= 0 and exit == false
07 if dataArray[tempPos] < temp then
08 dataArray[tempPos + 1] = dataArray[tempPos]
09 tempPos = tempPos - 1
10 else
11 exit = true
12 endif
13 endwhile
14 dataArray[tempPos + 1] = temp
15 next i
16 endprocedure
Compare the use of merge sort, quick sort and insertion sort on an array with a small number of elements, and
on an array with a very large number of elements.
You should make reference to the time complexities of each algorithm using the Big O notation in your answer.
[6]
Index 0 1 2 3 4 5
Data 2 18 6 4 12 3
Describe how a merge sort would sort the given array into descending order.
[6]
Explain why an insertion sort might use less memory than a merge sort.
[2]
[4]
(ii) Name two sorting algorithms, other than a bubble sort and merge sort.
[2]
4 A 1-dimensional array stores a set of numbered cards from 0 to 7. An example of this data is shown in Fig in 4.1
The worst case scenario for Merge is O(n log(n)) and for Bubble is O(n^2).
Compare the use of a merge sort and a bubble sort on this array, evaluating the performance of each sort,
making reference to the worse case Big O notation.
[9]
Currently the first five numbers before they have been sorted are:
195 584 167 147 158 187 160 125 184 236
Currently the last five numbers before they have been sorted are:
State the name of one other sorting algorithm that Poppy could have used.
[1]
(b) * Discuss how a bubble sort works and how efficient it will be when sorting these 250 000 items into order from
lowest to highest.
(c) State the number of comparisons that will need to be made in the first pass of the bubble sort.
[1]
[4]
The arrays that are passed to the procedure store integer values.
length returns the total number of elements the array can hold.
The program needs a second procedure, sortData. It will be called taking the array data[] as a parameter by
The procedure will then perform a bubble sort on the data in the array.
(i) Show each stage of a bubble sort on the following contents of data[]:
95 10 5 33 100 77 45
[4]
[8]
95 10 5 33 100 77 45
[4]
Fig. 1
A computer program is required to keep track of the scores for each competition. The user will enter the number
of players, and the name of each player, in that competition to a maximum of 10.
(i) The programmer has decided to use a bubble sort to sort the players’ scores in descending order of score.
[2]
Identify the characteristic of this problem that minimises the disadvantages of a bubble sort.
[1]
(iii) Write a procedure, sortScores, to perform a bubble sort on the global array scores to sort the players’
scores into descending numeric order.
A procedure, insertionSort, has been written to sort an array numbers. The procedure is incomplete.
procedure insertionSort()
for count = 0 to numbers.length - 1
position = ........................................................
while position > 0 and numbers[position] < numbers[position-1]
temp = .......................................................
numbers[position-1] = ........................................
numbers[position] = temp
position = ...................................................
endwhile
next count
endprocedure
[4]
Insertion sort has a worst case time complexity of O(n2) and space complexity of O(1).
An alternative sorting algorithm that could be used is bubble sort which also has a worst case time complexity of
O(n2) and space complexity of O(1).
01 procedure insertionSort(dataArray:byRef)
02 for i = 1 to dataArray.Length - 1
03 temp = dataArray[i]
04 tempPos = i – 1
05 exit = false
06 while tempPos >= 0 and exit == false
07 if dataArray[tempPos] < temp then
08 dataArray[tempPos + 1] = dataArray[tempPos]
09 tempPos = tempPos - 1
10 else
11 exit = true
12 endif
13 endwhile
14 dataArray[tempPos + 1] = temp
15 next i
16 endprocedure
[2]
[3]
10 A computer program stores data input on a stack named dataItems. The stack has two subprograms to add
and remove data items from the stack. The stack is implemented as a 1D array, dataArray.
Sub-program Description
push() The parameter is added to the top of the stack
6
15
100
23
As an array, the data in dataArray is sorted and then searched for a specific value.
(i) The data in dataArray is sorted into ascending order using an insertion sort.
100 22 5 36 999 12
[5]
(ii) The array dataArray can now be searched using a binary search.
function searchItem(dataItem)
for count = 0 to 49
if dataArray[count] == dataItem then
return(count)
endif
next count
return(-1)
[4]
[3]
(ii) Show how an insertion sort would sort the following data:
[6]
(i) Using Big-O notation state the best case complexity of insertion sort.
[1]
[3]
[3]
(ii) Demonstrate an insertion sort to place the following numbers into descending numerical order.
12 7 4 5 26
[4]
(iii) State one disadvantage of an insertion sort compared with a quick sort.
[1]
A programmer is writing a computer program to sort the cards into the correct order (0 to 7).
(i) Show how an insertion sort would sort the array in Fig 4.1 into the correct order. Draw the array after each
move.
[3]
(ii) Describe how a quick sort algorithm works with the data in Fig 4.2.
14(a) Identify one situation where a linear search is more appropriate than a binary search.
[1]
State a different type of loop that could be used instead of the while loop in the given algorithm.
[1]
Tick the worst-case space complexity for a binary and linear search.
Binary Linear
search search
O(log(n))
O(1)
O(n)
Tick the best-case space complexity for a binary and linear search.
Binary Linear
search search
O(log(n))
O(1)
O(n)
Tick the average time complexity for a binary and linear search.
Binary Linear
search search
O(log(n))
O(1)
O(n)
[6]
Some of Oscar’s customers are rated as gold. Customers who are rated as gold are given priority when they
make a taxi booking. Some customers rated as gold are shown here.
Arshad Betty Dave Freddie Harry Jimmy Kanwal Lynn Siad Tommy Will
When a customer makes a booking, Oscar will make use of a binary search to check if they are gold rated.
(i) State the three values that will be set as the midpoints and then checked against ‘Tommy’ on each iteration
of the binary search.
Midpoint 1
Midpoint 2
Midpoint 3
[3]
Benefit
(iii) State one other search algorithm that Oscar could have used.
[1]
(iv) State the pre-condition which has been met, which meant that Oscar did not need to use the search
algorithm you stated in the part above.
[1]
A linear search is used to check if the entered departure station exists in the array.
(i) Identify one precondition that is needed before a binary search could be used with the station array.
[1]
Explain how a linear search would check if the departure station exists in the array.
[4]
The user needs to be able to search for, and find, a specific order number.
State an appropriate search algorithm that could be used, and justify your choice against an alternative Search
algorithm.
Justification
[3]
Explain how concurrent processing could be used in searching algorithms, and evaluate the benefits and trade-
offs from implementing concurrent processing in a searching algorithm.
[9]
[4]
[8]
Use the values given to show the first three steps for:
[3]
[3]
(iii) Explain the difference between binary searching and serial searching.
[2]
(iv) State one advantage and one disadvantage of a binary search compared with a serial search.
[2]
When comparing linear and binary search it is possible to look at the best, worst and average number of items in
the array that need to be checked to find the item being searched for. Assume every item in the array is equally
likely to be searched for.
[1]
(c) Assuming an array is sorted give a situation when a linear search would perform better than a binary search.
[1]
1 a Mark Band 3 – High level (7-9 marks) 9 AO1: Knowledge and Understanding
The candidate demonstrates a thorough AO1.1 Indicative content
knowledge and understanding of big O and (2) O(1)
sorting algorithms; the material is generally AO1.2
accurate and detailed. (2) Constant space, does not change
The candidate is able to apply their AO2.1
knowledge and understanding directly and (2) O(n)
consistently to the context provided. AO3.3
Evidence/examples will be explicitly (3) Linear
relevant to the explanation. Same as number of elements
The candidate is able to weigh up the use As number of elements increases so
of the sorting algorithms which results in a does the time/space
supported and realistic judgment as to
whether it is possible to use them in this O(n2)
context.
There is a well-developed line of reasoning polynominal
which is clear and logically structured. The As number of elements increases,
information presented is relevant and time/space increases by *n
substantiated.
O(n log(n))
Mark Band 2 – Mid level (4-6 marks)
The candidate demonstrates reasonable Linearithmic
knoledge and understanding of big O and
sorting algorithms; the material is generally
accurate but at times underdeveloped. AO2: Application
The candidate is able to apply their
knowledge and understanding directly to Space: Merge sort will require more
the context provided although one or two memory usage as the number of
opportunities are missed. elements increases. Insertion will not
Evidence/examples are for the most part require any more space than original.
implicitly relevant to the explanation. Quick will increase but not as much as
The candidate makes a reasonable merge.
attempt to come to a conclusion showing Best time: Insertion increases at the
some recognition of influencing factors that same rate as the number of elements.
would determine whether it is possible to Quick and merge increase at greater
use the sorting algorithms in this context. rate
There is a line of reasoning presented with Worst time: insertion and quick
some structure. The information presented increase significantly by n for each
is in the most part relevant and supported additional item. Merge sort increases
by some evidence less per element.
Log more appropriate for large number
Mark Band 1 – Low Level (1-3 marks) of elements
The candidate demonstrates a basic
knowledge of big O and sorting algorithms AO3: Evaluation
with limited understanding shown; the e.g.
material is basic and contains some
inaccuracies. The candidates makes a Small array – space is not important.
limited attempt to apply acquired Few number of elements. Look for
knowledge and understanding to the consistency.
0 marks
No attempt to answer the question or
response is not worthy of credit.
Total 15
Total 8
Insertion
Quick
Total 6
0 marks
No attempt to answer the question or
response is not worthy of credit.
Total 9
Mark Band 1 – Low Level (1-3 marks) The algorithm is easy to implement as
The candidate demonstrates a basic the number of lines of code is less than
knowledge of bubble sorts with limited other standard sorting algorithms.
understanding shown; the material is basic Although a bubble sort will be able to
and contains some inaccuracies. The sort the items into order, it will take
candidates makes a limited attempt to longer than other sorting algorithms
apply acquired knowledge and due to the number of items and the
understanding to the context provided. The current order or items in the unsorted
candidate provides nothing more than an list.
unsupported assertion.The information is
0 marks
No attempt to answer the question or
response is not worthy of credit.
c 249,999 1
A02.2
(1)
Total 15
6 i 1 mark per set of swaps 4 AO1.1 Pair swaps may be shown individually or at
(1) AO1.2 the end of each traversal (award mark
(1) AO2.1 point 1 and 2 if first traversal is shown on
10 95 5 33 100 77 45 1 (2) one line)
10 5 95 33 100 77 45
Examiner’s Comments
10 5 33 95 100 77 45
10 5 33 95 77 100 45 1 A high proportion of candidates knew the
basic principle of a Bubblesort and how it
10 5 33 95 77 45 100
operated, with most candidates obtaining
5 10 33 95 77 45 100 1 at least the first marking point. Some
5 10 33 77 95 45 100 candidates gave textual descriptions that
5 10 33 77 45 95 100 made it difficult to follow the logic of the
response. Those candidates that
5 10 33 45 77 95 100 1 presented each step in the algorithm,
clearly highlighting each swap that took
place, demonstrated the clearest
understanding.
Exemplar 1
swapped = true
while swapped == true
swapped = false
for innerCount = 0 to
data.length - 2
if data[innerCount] > data
[innerCount + 1] then
temp = data[innerCount]
data[innerCount] = data
[innerCount + 1]
data[innerCount+1] =
temp
swapped = true
end if
next innerCount
endwhile
endprocedure
iii 1 mark per row(s) as shown: 4 AO1.2 In the 3rd mark point the line may appear
(2) AO2.1 just once
(1) AO2.2
95 10 5 33 100 77 45 (1) Examiner’s Comments
10 95 5 33 100 77 45 1
Those candidates who understood the
5 10 95 33 100 77 45 1 principles behind insertion sort often clearly
5 10 33 95 100 77 45 1 delineated their responses into the 'sorted'
and the 'unsorted' parts of the array. This
5 10 33 95 100 77 45
made it easier to follow the steps that had
5 10 33 77 95 100 45 1 been applied. Some candidates wrote long-
5 10 33 45 77 95 100 winded textual descriptions of the
principles of the process without actually
showing how the principles applied to the
actual data, which meant that credit could
not be given.
Exemplar 2
Total 16
Total 13
Examiner’s Comment:
Most candidates achieved some credit,
especially for a description of the bubble
sort. Fewer candidates could compare the
relative merits of both bubble and insertion
sort in terms of the best / average / worse
case.
Total 9
Total 5
return(-1)
endfunction
e.g. 2
function searchItem(dataItem)
count = 0
count = count + 1
endwhile
if count==50
count=-1
endif
return(count)
endfunction
Total 16
11 a i 1 mark for each correct item in bold 3 answers must be in the correct case as
AO1.1 given
(3) e.g. currentData
Examiner’s Comment:
Many candidates found it difficult to apply
the logic required to calculate the correct
solution. Stronger candidates could do so
even if they did not know the algorithm for
insertion sort.
ii 1 mark for contents of each row in table 6 … each row is dependent upon the
AO2.1 preceding row being correct
(6)
Examiner’s Comment:
Some candidates confused insertion sort
with other sorting algorithms, but many
candidates gave good answers in
diagrammatic form. Answers in
diagrammatic form after each pass of the
loop were often far clearer than prose
descriptions. This form of answer should
be encouraged.
b i O(n) 1
AO1.1
(1)
ii 1 mark per bullet to max 3 3 B(ii) dependent upon b(i) being correct
AO1.2 i.e. answers for O(n) only
(3)
The best case is for a sorted list (O(n))
As the number of elements increases Accept appropriate graph for bullet points
… the number of steps increases in a 2 and 3
linear fashion
Examiner’s Comment:
Whilst many candidates had some
knowledge of 'Big O' notation fewer could
apply it correctly within the context given.
Total 13
Examiner's Comments
1 mark per correct row after row 1 in Do not allow swap(ped) or pivots
sequence to max 4
Examiner's Comments
Total 8
13 i 1 mark for each set of 2 moves 3 Allow follow through if one move is
incorrect
02174356
01274356 (1)
01247356
01234756 (1)
01234576
01234567 (1)
ii 1 mark per bullet to max 6 6 If no description i.e. the candidate has just
shown the quick sort, max 4 marks.
Uses divide-and-conquer (1)
First item becomes pivot / 2 is the pivot
(1)
Compare each item to the pivot (e.g.
compare 0 to 2, then 1 to 2) (1)
Make two lists, 1 with less than the
pivot… (0, 1) (1)
… 1 with more than the pivot
(7,4,3,5,6) (1)
Quick sort the new lists (1)
Recombine the sub-lists (1)
OR
Example of alternative quicksort method
marks for:
Total 9
Binary Linear
search search
O(log(n))
O(1) ✓ ✓
O(n)
Best-case space complexity:
Binary Linear
search search
O(log(n))
O(1) ✓ ✓
O(n)
Binary Linear
search search
O(log(n)) ✓
O(1)
O(n) ✓
Total 14
A02.1
(1)
Total 7
Total 5
Total 3
18 Mark Band 3 – High level (7-9 marks) 9 AO1: knowledge and understanding
The candidate demonstrates a thorough Indicative content
knowledge and understanding of
concurrent processing; the material is
generally accurate and detailed. Carrying out more than one task at a
The candidate is able to apply their time
knowledge and understanding directly and Multiple processors
consistently to the context provided Each processor performs
(searching algorithms). simultaneously
Evidence/examples will be explicitly Each processor performs tasks
relevant to the explanation. independently
The candidate provides a thorough
discussion which is well balanced. and/or
Evaluative comments are consistenly
relevant and well considered.
A program has multiple threads
There is a well-developed line of reasoning Each thread starts and ends at
which is clear and logically structured. The different times
information presented is relevant and Each thread overlaps
substantiated. Each thread runs independently
The candidates makes a limited attempt to May waste time investigating inefficient
apply acquired knowledge and solutions
understanding to the context provided More difficult to program especially to
(searching algorithms). cooperate
The candidate provides a limited More memory intensive
discussion which is narrow in focus. Linear search scales very with
Judgements if made are weak and additional processors
unsubstantiated. Binary search can perform better on
The information is basic and comunicated large data sets with one processor than
in an unstructured way. The information is linear search with many processors
supported by limited evidence and the
relationship to the evidence may not be
clear.
0 marks
No attempt to answer the question or
response is not worthy of credit.
Total 9
19 a Find the middle point in the list / 21 / 4 Some marks such as the comparison may
element 4 be by implication if the candidate’s logic
Compare it to the value 47, false works
Is 47 greater than middle point, true
New subset is 46-51 / change lower Must refer to the list given in the question
bound to 46 / element 5 i.e. not a generic description
Find the middle of the new subset / 47 /
element 6 Is this value equal to 47, true Examiner's Comments
Search finishes
In general, many candidates had an
understanding of how a binary search
operated. Unfortunately, some gave a
generic description rather than answering
the specific question which required the
candidate to illustrate how a binary search
would operate on a specific data set. Some
candidates drew concise and elegant
diagrams with appropriate annotations that
made their answers much clearer than
those who wrote prose at great length.
Total 12
Total 10
Total 6