Study · Pearson Edexcel 1CP2 · 1.2.6–1.2.7
Trace binary search, bubble sort and merge sort; justify a choice.
About 90 minutes. Predict each figure, then try the green checks before you open them. The Quiz tab is the chapter check. Resources holds the long written questions.
Your learning route
Searching finds a value. Sorting puts values into order. This chapter traces linear search, binary search, bubble sort and merge sort, then asks you to choose a method for a particular list and a particular job.
Sit down twice. First, both searches, using the figures and writing the indexes yourself. Second, both sorts and the choice between them. Count the comparisons the stated algorithm makes. Do not replace a trace with a memorised slogan about speed.
The references are Pearson Edexcel 1CP2, sections 1.2.6–1.2.7. OCR also expects insertion sort. A short insertion trace is in the board section so that an OCR candidate is not left without the method.
Learning objectives
By the end of the chapter you should be able to:
- Trace a linear search and say when it stops
- Trace a binary search on ordered data, including a missing target
- Trace one or more passes of bubble sort, with and without an early stop
- Show the split and the merge of a small merge sort
- Choose a search or a sort using the size of the data, whether it is ordered, and how often it will be used
- Name tests that would expose a wrong search or a sort that drops a duplicate
9.1 Sorting is not a compulsory first step
Definition: An algorithm is a precise, finite set of steps that solves a stated problem. A search reports where a target is, or that it is absent. A sort rearranges items into a requested order. Sorting first is worthwhile when the ordered list will be searched often enough to repay the sorting work. Sorting a short list once, just to search it once, can be more work than reading the list from left to right.
Key terms:
- Comparison: one test between values, such as "is this item equal to the target?" or "is the left neighbour greater than the right?"
- Index: the position of an item. This chapter counts the first item as index 0, matching the Python examples.
- Efficiency, for this chapter: a reasoned comparison of work, usually comparisons or passes, for the data you were given. It is not a memorised formula dropped in without a trace.
Why This Matters: "Binary search is faster" is not an answer. Faster than what, on what data, after what preparation? A class register of 20 names that is searched once may not justify a sort. A large ordered register searched all day can.
Common Misconception: "Data must be sorted before any search." Linear search does not need sorted data. Binary search does. The requirement belongs to the algorithm, not to the word "search".
Examiner tip
Say what you are counting. A question that says "comparisons" might mean item inspections, or it might mean every relational operation in the code. Follow the implementation you were given.
Check yourself: A list of 12 unsorted names will be searched once. Why might you refuse to sort it first?
Retrieval answer
Sorting the 12 names costs extra comparisons and writes. A single linear search looks at each name at most once and can stop early. Unless the list will be reused in order, the sort is extra work for one lookup.
9.2 Linear search looks one item at a time
Stop at the first match
The list is unsorted in the general case, so every item may need a look. Predict the index before you reveal the next comparison.
- Index 04
- Index 19
- Index 213
- Index 318
- Index 424
- Index 531
- Index 640
No comparison yet. The first item to check is index 0.
Definition: A linear search starts at one end and compares the target with each item until it finds a match or runs out of items. The first-match version in this chapter returns the index and stops. It does not need the list to be ordered.
Key terms:
- First match: the earliest index whose value equals the target. Later copies are not visited.
- Absent target: every item was compared, or the list was empty, and nothing matched. This chapter's methods return −1.
- Empty list: there is no index 0. The search performs no comparisons and reports absence.
Use the figure on [4, 9, 13, 18, 24, 31, 40]. Finding 31 inspects index 0 through index 5. That is six comparisons, then a stop. It should not continue to 40. Finding 4 stops at index 0. Finding 10 inspects all seven items and returns −1.
def linear_search(values, target):
for index in range(len(values)):
if values[index] == target:
return index
return -1
Why This Matters: Linear search is the honest method for unsorted data and for "find the first one". It is also the method you use to check a binary-search answer on a short list.
Common Misconception: "The index returned is the number of comparisons." They can differ. A match at index 0 took one comparison. A match at index 5 took six. An absent target in a list of seven took seven comparisons and returned −1, which is not an index in the list.
Examiner tip
Write the index you return, and write −1 rather than "none" or "false" unless the question's code returns those.
Check yourself: The list is [14, 8, 27, 8, 31] and the target is 8. What does a first-match linear search return?
Retrieval answer
Index 1. The 8 at index 3 is a later copy. A first-match search stops at the first 8 and does not report index 3.
9.3 Binary search keeps a shrinking interval
Discard half, including the middle
The list must already be in order. Predict the middle index, then decide whether low or high moves.
- Index 04
- Index 19
- Index 213
- Index 318
- Index 424
- Index 531
- Index 640
Interval 0 to 6. Predict floor((low + high) / 2).
Definition: Binary search requires data in order. It keeps two boundaries, low and high, around the items that could still hold the target. It compares the target with the middle item. If the target is smaller, it discards the middle and everything above it. If the target is larger, it discards the middle and everything below it. A match ends the search. If low becomes greater than high, the target is absent.
def binary_search(values, target):
low = 0
high = len(values) - 1
while low <= high:
middle = (low + high) // 2
if values[middle] == target:
return middle
elif values[middle] < target:
low = middle + 1
else:
high = middle - 1
return -1
The // is integer division, so the middle rounds down. Using a different rounding rule can change the trace. The +1 and −1 matter because the middle item has already been checked. Leaving it inside the interval can make the loop repeat the same middle forever.
Use the figure. For target 31 in [4, 9, 13, 18, 24, 31, 40]:
| Step | low | high | middle | Item | Next |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 18 | low becomes 4 |
| 2 | 4 | 6 | 5 | 31 | return 5 |
For a missing target such as 10, keep going until the boundaries cross. Do not stop just because a nearby value "looks close".
Why This Matters: Binary search questions are traces. The marks are the boundaries and the middle, in order. The figure uses the same floor rule as the Python above.
Common Misconception: "Binary search works on any list if you try hard enough." If the list is not ordered, a smaller item can sit to the right of the middle. Discarding a side would throw away the target. Ordered data is a condition, not a decoration.
Examiner tip
Write low, high and middle on every row. If you only write the items you looked at, a marker cannot see whether you moved the correct boundary.
Check yourself: On [3, 8, 12, 17, 23, 29, 35], what is the first middle index when the target is 23?
Retrieval answer
Low 0, high 6, middle (0 + 6) // 2 = 3. Index 3 holds 17, which is smaller than 23, so the next low is 4. The next middle is (4 + 6) // 2 = 5, which holds 29. Then low stays 4 and high becomes 4, and index 4 holds 23.
9.4 Bubble sort swaps neighbours
One pass does not finish the sort
Compare each adjacent pair from left to right. A swap moves the larger value rightwards. Check whether the list is finished at the end of the pass.
- Index 07
- Index 12
- Index 25
- Index 31
Start with [7, 2, 5, 1]. The first pair is 7 and 2.
Definition: Bubble sort compares adjacent items and swaps them when they are in the wrong order. In an ascending sort, a left-to-right pass moves the largest unsorted item to the right-hand end. Further passes sort what remains. The end of pass one is not the end of the sort.
Use the figure on [7, 2, 5, 1]:
| Comparison | Result |
|---|---|
| 7 and 2 | swap to [2, 7, 5, 1] |
| 7 and 5 | swap to [2, 5, 7, 1] |
| 7 and 1 | swap to [2, 5, 1, 7] |
The second pass produces [2, 1, 5, 7]. The third swaps 2 and 1 and finishes [1, 2, 5, 7]. With the unsorted end shrinking, the comparison counts are 3, then 2, then 1.
A version that records whether any swap happened may stop after a pass with no swaps. A version with a fixed number of passes still does those passes. Do not claim that already-sorted input finishes in one pass unless the implementation has that early stop.
Why This Matters: Bubble-sort questions ask for the list after a named pass, or for the number of swaps. The figure shows the pair being compared. Write the whole list after each swap, not only the two numbers.
Common Misconception: "One pass puts every item in its final place." One pass of this version puts the largest item at the end. The other items may still be disordered. [2, 5, 1, 7] is not sorted.
Examiner tip
Say ascending or descending before you start. A descending trace swaps when the left item is smaller, which is the opposite test.
Check yourself: One left-to-right ascending pass on [6, 3, 8, 2]. What are the lists after each comparison?
Retrieval answer
6 and 3 swap to [3, 6, 8, 2]. 6 and 8 stay, so the list is unchanged. 8 and 2 swap to [3, 6, 2, 8]. Another pass is needed because 6 is still left of 2.
9.5 Merge sort divides, then merges
The merge creates the order
Reveal one stage at a time. When one side runs out, copy the rest of the other side. Do not invent an extra comparison.
[7, 2, 5, 1]
[7, 2, 5, 1] divides into [7, 2] and [5, 1], then into [7], [2], [5] and [1]. A single item is already sorted. Splitting has not ordered the whole list.
Definition: Merge sort splits a list into smaller lists until each piece has at most one item. A one-item list is sorted. It then merges sorted pieces by repeatedly taking the smaller of the two items at the front. The ordering happens in the merge, not in the split.
Use the figure on [7, 2, 5, 1]. Split into [7, 2] and [5, 1], then into [7], [2], [5] and [1]. Merge to [2, 7] and [1, 5]. The final merge compares 2 with 1 and takes 1, compares 2 with 5 and takes 2, compares 7 with 5 and takes 5, then copies the remaining 7. The result is [1, 2, 5, 7].
When one side is empty, copy the rest of the other side. Do not invent comparisons against nothing. A typical merge into an array uses extra space for the merged result. Bubble sort can swap inside the original list and needs little extra storage, but it often does many more comparisons on a long disordered list.
Why This Matters: Merge-sort questions reward the order of selections. "It splits and then it is sorted" misses the only stage that chooses items.
Common Misconception: "Merge sort is always better, so bubble sort should never be taught." Bubble sort is easy to trace in a small array and can work in place. Merge sort does fewer comparisons as the list grows and needs extra space. The better choice depends on the size, the space, and whether you must show a simple trace.
Examiner tip
Write the two fronts you compared and which one you took. If you jump to the final list, the marker cannot award the intermediate merges.
Check yourself: Merge the already sorted lists [2, 7] and [3, 4].
Retrieval answer
Compare 2 and 3, take 2. Compare 7 and 3, take 3. Compare 7 and 4, take 4. Copy 7. The result is [2, 3, 4, 7].
9.6 Choose the algorithm, then test it
Definition: A suitable algorithm is one whose conditions match the data and whose result matches the question. Suitability is not a league table.
Test a search with a target at the start, a target at the end, a missing target, a duplicate if the list can contain one, and an empty list if that input is allowed. Say whether you need the first match. Ordinary binary search does not promise the first of several equal values.
Test a sort with data that is already ordered, reversed, duplicated, and a single item. Check that every original value is still there. A procedure that deletes a duplicate can look sorted and still be wrong.
Why This Matters: The last marks in an algorithm question are often the condition. "I would use binary search because it is fast" fails if the names are unsorted or the search happens once.
Common Misconception: "A sorted output proves the algorithm." It proves the output order of that one test. It does not prove that a duplicate was kept, or that a different input would work. State the test and what it is able to show.
Examiner tip
For a "which algorithm" question, give one reason tied to the data and one limit of the method you rejected. Two halves of a comparison, not two compliments of the winner.
Check yourself: You need the first occurrence of a repeated surname in an unsorted register of 30 pupils, and you will do this once. Which search, and why?
Retrieval answer
Linear search. The register is unsorted, so binary search is unsafe. You need the first match, which this linear search returns by stopping at the first equal item. Sorting first would be extra work for a single lookup and would also need a version that preserves the original first position.
Independent practice
Original practice · 8 marks. Use the supplied binary-search algorithm on [3, 8, 12, 17, 23, 29, 35] with target 23. Record each interval and middle index. Then trace target 10 until the interval becomes empty. State why using the same procedure on an unsorted list is unsafe.
Worked trace and marking
For 23: (low, high, middle) is (0,6,3) with 17; (4,6,5) with 29; (4,4,4) with 23. Return 4. Award one mark per correct row [3] and the result [1].
For 10: inspect index 3 (17), then index 1 (8), then index 2 (12). The bounds become low 2, high 1, so return −1. Award the middle-index sequence [1], final bounds [1] and missing result [1]. Ordered data is necessary because the comparison must justify discarding an entire side [1].
Homework
Original practice · 6 marks. Trace one complete left-to-right bubble pass on [6, 3, 8, 2]. Explain why another pass is needed. Then merge the already sorted lists [2, 7] and [3, 4].
Six-mark feedback
Bubble states: [3,6,8,2], unchanged after 6 versus 8, then [3,6,2,8] [3]. Another pass is needed because 6 and 2 remain out of order [1]. Merge takes 2, 3, 4, then the remaining 7 [2].
Compare one linear search with "sort, then search" for a small unsorted class register used once. Write a second recommendation for a large register that is searched many times and can be kept in order. Name the assumptions that would change your mind. Use comparisons and repeated work. Do not write "binary is faster" without saying faster than which method, and after what sorting cost.
How the exam boards differ
Pearson Edexcel 1CP2 expects linear search, binary search, bubble sort and merge sort, and a reason for choosing one. The traces above match that course.
OCR J277 also expects insertion sort. Insertion sort builds a sorted portion on the left. Each new item is inserted into the position that keeps that portion ordered. On [7, 2, 5, 1], start with [7]. Insert 2 in front of 7 to get [2, 7]. Insert 5 between 2 and 7 to get [2, 5, 7]. Insert 1 at the front to get [1, 2, 5, 7]. OCR answers should be ready to trace that method as well as bubble and merge.
AQA 8525 expects linear search, binary search, bubble sort and merge sort. Insertion sort is not one of the standard algorithms AQA currently lists. Do not spend an AQA answer describing insertion sort unless the question gives you that algorithm.
All three boards expect binary search to be applied only to ordered data, and they expect you to follow the midpoint rule supplied in the question if it differs from floor division.
Sources and further practice
Practice questions and point allocations on this page are original Proof Academy material unless a question is explicitly identified as a past-paper bridge.
- Pearson Edexcel 1CP2 specification, topic 1.2
- OCR GCSE Computer Science J277, section 2.1
- AQA GCSE Computer Science 8525, section 3.1
- PMT papers and mark schemes, for trace-table style
When you use an original paper, open its matching mark scheme. A short classroom exercise is not a substitute for a full official task.
Keep your evidence
Export your notes before changing devices.
Notes stay in this browser. The Quiz tab records the chapter check, and Resources holds the long-answer questions.