Searching and sorting algorithms
Steps of binary and linear search plus bubble, merge and insertion sort.
Learning Objectives
- 1Understand the main steps of linear search, binary search, bubble sort, merge sort and insertion sort
- 2Understand any pre-requisites each algorithm needs, such as a sorted list
- 3Apply each algorithm to a given data set by hand
- 4Identify which algorithm is shown, given its code, pseudocode or Exam Reference Language
Searching and sorting algorithms
Linear search
def linear_search(data, target):
for i in range(len(data)):
if data[i] == target:
return i
return -1function linearSearch(data, size, target)
for i = 0 to size - 1
if data[i] == target then
return i
endif
next i
return -1
endfunctionBinary search
def binary_search(data, target):
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if data[mid] == target:
return mid
elif data[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1function binarySearch(data, size, target)
low = 0
high = size - 1
while low <= high
mid = (low + high) DIV 2
if data[mid] == target then
return mid
elseif data[mid] < target then
low = mid + 1
else
high = mid - 1
endif
endwhile
return -1
endfunctionBubble sort
def bubble_sort(data):
n = len(data)
for i in range(n - 1):
for j in range(n - 1 - i):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
return dataprocedure bubbleSort(data, size)
for i = 0 to size - 2
for j = 0 to size - 2 - i
if data[j] > data[j + 1] then
temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
endif
next j
next i
endprocedureMerge sort
Insertion sort
def insertion_sort(data):
for i in range(1, len(data)):
key = data[i]
j = i - 1
while j >= 0 and data[j] > key:
data[j + 1] = data[j]
j = j - 1
data[j + 1] = key
return dataprocedure insertionSort(data, size)
for i = 1 to size - 1
key = data[i]
j = i - 1
while j >= 0 AND data[j] > key
data[j + 1] = data[j]
j = j - 1
endwhile
data[j + 1] = key
next i
endprocedureExam Phrasing: Use These Exact Phrases
- ✓Linear search checks every item in order, from the start, until the target is found or the list ends
- ✓Binary search only works on a sorted list, and repeatedly discards half the remaining data
- ✓Bubble sort repeatedly compares and swaps adjacent items until no swaps are needed
- ✓Merge sort splits the list into single items, then merges them back together in order
- ✓Insertion sort inserts each item into its correct position among the items already sorted
- 💡You don't need to memorise the exact code for these algorithms, focus on understanding the steps well enough to apply them by hand to a data set, or identify the algorithm from given code.
- 💡State pre-requisites precisely, binary search requires a sorted list, this is a common, easy mark.
- 💡Describing binary search, do not start with "split the list in half", examiners are told that is incorrect as a first step and earns nothing. The first step is: select the middle item and check whether it equals the target. "Compare it" on its own is also not enough, say you check for a match.
- 💡For the end of a binary search, say it repeats until the value is found or the remaining list is down to 1 or 0 items. "Until the number is not in the list" is not enough, you have to say how you know that.
- 💡For merge sort, always show the divide stage (splitting into single items) AND the merge stage (comparing and combining in order), never just append sub-lists and sort them afterwards.
- 💡For linear search, the precise stopping condition is: the target is found, OR the end of the list is reached without finding it.
- 💡When applying an algorithm to a data set by hand, show your working clearly, examiners can award partial credit for a correct method even if the final answer has a small error.
Wrong:
Merge sort: simply append (concatenate) the sub-lists together, then sort the combined list
Right:
Merge sort: merge the sub-lists together by comparing their front items and always taking the smaller one first, so the result is already in order
Why it matters: Examiners specifically flagged this as a common, serious misconception, appending then sorting is not merge sort and receives no credit for the merge step.
Wrong:
Binary search works on any list
Right:
Binary search only works on a list that is already sorted
Why it matters: Its logic (discarding half the list based on comparing to the middle) only works correctly if the data is in order.
Wrong:
An array can grow or shrink in size, just like a list
Right:
An array is a fixed-length, static data structure, unlike a dynamic list which can be appended to
Why it matters: Examiners noted candidates using Python (where lists are dynamic) sometimes incorrectly described arrays as flexible in size.
Question:
Apply bubble sort to sort the list [5, 1, 4, 2] into ascending order. Show the list after each pass. [4]
💡 Attempt this before revealing the answer, practice makes perfect!
Model Answer:
Pass 1: compare 5&1, swap → [1,5,4,2]. Compare 5&4, swap → [1,4,5,2]. Compare 5&2, swap → [1,4,2,5]. Pass 2 (one fewer comparison needed, since 5 is already in its final position): compare 1&4, no swap. Compare 4&2, swap → [1,2,4,5]. Pass 3 (one comparison only): compare 1&2, no swap, so the list [1,2,4,5] is confirmed sorted.
Mark Scheme:
- 1Correct list after pass 1: [1, 4, 2, 5]
- 2Correct list after pass 2: [1, 2, 4, 5]
- 3Recognises the list is now sorted (a final pass with no swaps confirms this)
- 4Shows adjacent comparisons and swaps correctly throughout, in the correct order
Tap a card to flip it. Use the arrow keys to move and space to flip.
Exam-style questions. Have a go, then reveal the mark scheme to check your answer.
1.State the pre-requisite that binary search requires, and explain why it is needed.
[2]2.A candidate sorts [4, 2, 6, 1] using merge sort by writing [4, 2] and [6, 1] as two lists, then joining them to get [4, 2, 6, 1], then separately sorting this combined list. Explain why this is not a correct demonstration of merge sort.
[2]3.Apply linear search to find the value 9 in the list [4, 8, 9, 2, 6]. State the index it would be found at, and explain why linear search would need to check fewer items than if it were searching for 6.
[3]4.You are given the code below. Identify which sorting algorithm it represents, and justify your answer using a feature of the code.
[3]for i in range(len(data) - 1):
for j in range(len(data) - 1 - i):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]for i = 0 to size - 2
for j = 0 to size - 2 - i
if data[j] > data[j + 1] then
temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
endif
next j
next i5.Apply insertion sort to the list [7, 3, 5, 1]. Show the list after each item has been inserted into its correct position.
[4]6.Compare linear search and binary search, and explain when each would be the appropriate choice.
[6]The examiners' reports are the most useful and least read of the three: they say what candidates actually got wrong each year. All files are hosted by OCR.
Past-paper questions grouped by topic
The same OCR questions, sorted by topic rather than by year (via Physics & Maths Tutor).
A common and repeated misconception was that merge sort merges sub-lists together by simply appending one to the other (creating an unsorted list) and then sorting that combined list. In real merge sort, every list at every stage of merging is already in order, it is the process of merging (comparing and taking the smaller front item each time) that performs the sort. Where candidates showed this misconception, the relevant mark was not given.
When identifying the stopping condition for linear search, many candidates responded "when the value is not found", which is imprecise since that is the outcome, not the condition. The technically correct condition is when the end of the list is reached without finding the value, though answers referring to reaching the end of the list or having checked every item were also accepted.
Fewer candidates than expected could correctly identify that an array is defined by a fixed length (a static structure), likely because Python, the most commonly used language, supports dynamic lists by default rather than fixed-size arrays. Candidates should be clear that an array's size is fixed once declared, unlike a list which can grow.