2.1.3 Searching and sorting algorithms · Paper 2
Searching and sorting
Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.
You need to know
- Binary search, linear search, bubble sort, merge sort and insertion sort
- The main steps of each, and any pre-requisites (e.g. binary search needs sorted data)
- Apply each one to a set of data
- Recognise each algorithm from its code or pseudocode
You don't need to know
- The code or pseudocode for these algorithms off by heart
Linear search
A linear search checks each item in turn, starting at the first, until it finds the item or reaches the end of the list.
- Works on unsorted data
- Simple to write
- Slow on long lists: the item might be last, or not there at all
Try it
Tap a number, then press Play to watch a linear search find it.
Key points
Linear search
Before this: 2.2.3Arrays
- A linear search checks each item in turn, starting at the first.
- It stops when it finds the item, or reaches the end of the list.
- It works on unsorted data.
- It is simple, but slow on large lists: the item could be at the end.
found = false for i = 0 to names.length - 1 if names[i] == target then found = true endif next i print(found)
Key words
Say what each one means, then tap to check.
Binary search
A binary search only works on sorted data. Each check throws away half of the items that are left.
- Look at the middle item. If there are two middle items, pick either one (this page uses the left one).
- If it's the item you want, stop.
- If what you want is smaller, throw away the middle and everything after it. If it's bigger, throw away the middle and everything before it.
- Repeat with the items that are left, until you find it or nothing is left.
Try it
Tap a number, then step through the binary search. Watch half the list disappear each time.
Key points
Binary search
Before this: Linear search
- A binary search only works on sorted data.
- Look at the middle item. If it's the one you want, stop.
- If what you want is smaller, throw away the middle and everything after it. If bigger, throw away the middle and everything before it.
- Repeat with the half that's left until you find it, or nothing is left.
- Each check halves the list, so it is very fast on large lists.
Example: find 21 in a sorted list.
3 7 9 [14] 18 21 25 middle is 14, 21 is bigger: keep the right half 18 [21] 25 middle is 21: found
Key words
Say what each one means, then tap to check.
Which search is faster?
| Linear search | Binary search | |
|---|---|---|
| Data must be sorted? | No | Yes |
| Speed on long lists | Slow | Very fast |
| Easy to code? | Yes | Harder |
Each binary search check halves what's left, so doubling the list adds only one more check. But if the data isn't sorted, it has to be sorted first, which takes time too.
Try it
Race the two searches. Try a bigger list each time.
Try it
Practise an exam question. Choose a search, then two reasons that fit.
- Works even if the list is not sorted
- Simple to write, and quick enough for a short list
- The list is already sorted, so it can be used
- Each check halves what is left, so it needs far fewer checks
- Each item is checked once, so no data is missed
- Finding a guest's name in an unsorted list of 20 names.
- Finding a word in a dictionary app with 100,000 words in alphabetical order.
- Checking if a barcode is in a shop's sorted list of 50,000 products.
- Finding a song in a playlist of 30 songs, kept in the order they were added.
Key points
Linear or binary?
Before this: Binary search
- Linear search: works on unsorted data and is simple to write. Fine for short lists.
- Binary search: much faster on long lists, but the data must be sorted first.
- With 1,000 sorted items, a linear search may need 1,000 checks. A binary search needs at most 10.
- If data is unsorted, either use a linear search or sort it first.
| Linear search | Binary search | |
|---|---|---|
| Data must be sorted? | No | Yes |
| Speed on big lists | Slow | Fast |
| How easy to code | Easy | Harder |
Key words
Say what each one means, then tap to check.
Bubble sort
A bubble sort compares each pair of neighbours and swaps them if they're in the wrong order. One pass moves the biggest item to the end, like a bubble rising.
It keeps doing passes until a pass makes no swaps. Then the list is sorted.
Try it
Step through a bubble sort. Watch the list after each pass build up underneath.
Key points
Bubble sort
Before this: 2.2.3Arrays
- A bubble sort compares each pair of items next to each other, and swaps them if they're in the wrong order.
- One pass through the list moves the biggest item to the end.
- It keeps doing passes until a pass makes no swaps. Then the list is sorted.
- Simple to write, but slow on large lists.
Example: sorting 5, 1, 4, 2.
Pass 1: 5 1 4 2 1 5 4 2 1 4 5 2 1 4 2 5 Pass 2: 1 4 2 5 1 2 4 5 Pass 3: no swaps, so it's sorted
Key words
Say what each one means, then tap to check.
Insertion sort
An insertion sort builds a sorted part on the left, one item at a time. Take the next item and slide it left until it's in the right place among the sorted items.
Try it
Step through an insertion sort. The purple bar is the item being inserted.
Key points
Insertion sort
Before this: Bubble sort
- An insertion sort builds a sorted list one item at a time.
- Start with the second item. Move it left until it is in the right place among the items before it.
- Then take the next item and do the same, until every item has been inserted.
- Good for small lists, or lists that are nearly sorted.
Example: sorting 5, 2, 4, 1, 3. The sorted part is on the left of the bar.
5 | 2 4 1 3 2 5 | 4 1 3 2 4 5 | 1 3 1 2 4 5 | 3 1 2 3 4 5
Key words
Say what each one means, then tap to check.
Merge sort
A merge sort splits the list in half again and again until every list has one item. Then it merges pairs of lists back together in order, until there is one sorted list.
To merge two sorted lists, compare their first items and take the smaller one. Repeat until both lists are empty.
Try it
Step through a merge sort. This is how to set out your working in the exam.
Key points
Merge sort
Before this: Bubble sort
- A merge sort splits the list in half again and again until every list has just one item.
- Then it merges pairs of lists back together, in order.
- It keeps merging until there is one sorted list.
- Much faster than a bubble sort on large lists, but it uses more memory.
Example: sorting 7, 3, 5, 1.
Split: 7 3 5 1 7 3 | 5 1 7 | 3 | 5 | 1 Merge: 3 7 | 1 5 1 3 5 7
Key words
Say what each one means, then tap to check.
Which sort?
| Bubble sort | Insertion sort | Merge sort | |
|---|---|---|---|
| Long lists | Slow | Slow | Fast |
| Short or nearly sorted lists | OK | Good | OK |
| Extra memory | None | None | Needs more |
| Easy to code? | Yes | Yes | Harder |
Try it
Which algorithm is it?
- Looks at the middle item and throws away half the list each time.
- Compares neighbours and swaps them. Stops after a pass with no swaps.
- Splits the list into single items, then joins them back together in order.
- Checks every item in turn, from the start.
- Takes each item and slides it into place among the sorted items on its left.
Try it
Practise an exam question. Choose a sort, then two reasons that fit.
- Much faster on long lists
- Takes about the same time however mixed up the list is
- Very quick when the list is short or nearly sorted
- Needs no extra memory
- Simple to write
- A bank sorting 2 million customer records on a powerful server.
- A tiny sensor with very little memory, sorting its last 10 readings.
- Adding one new score to a leaderboard that is already in order.
- Sorting every song in a music library of 500,000 songs.
Key points
Spot the algorithm
Before this: Linear or binary?Merge sortInsertion sort
- Linear search: one loop that checks every item against the target.
- Binary search: variables like low, high and mid, and DIV 2 to find the middle.
- Bubble sort: two loops, comparing items[j] with items[j + 1] and swapping with a temp variable.
- Insertion sort: a while loop that shifts items one place right until the gap is in the right place.
- Merge sort: splits into left and right halves, then merges them.
Bubble sort (Python)
for i in range(len(items) - 1): for j in range(len(items) - 1 - i): if items[j] > items[j + 1]: temp = items[j] items[j] = items[j + 1] items[j + 1] = temp
Insertion sort (OCR reference language)
for i = 1 to items.length - 1 current = items[i] pos = i while pos > 0 AND items[pos - 1] > current items[pos] = items[pos - 1] pos = pos - 1 endwhile items[pos] = current next i
Key words
Say what each one means, then tap to check.
Exam practice
21 questions in the style of the OCR exam, 48 marks in total. Try each one, then check the mark scheme.
Linear search
3, 9, 14, 20, 21
Hint
Start at the first item. Say what happens at each one until you reach 14.
Describe means say what it is like or what happens. Make each point a separate fact.
3 marks: make 3 separate points.
Show mark scheme
- Start at the first item, 3; it is not 14.
- Move to the next item, 9; it is not 14.
- Check 14, it matches, so stop / found.
Don't accept: Starting in the middle of the list. That is a binary search.
1 mark for each step.
Hint
Which items do you need to look at? What do you compare each one with? When can you be sure the name isn't there?
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
4 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Loop through every item / index of the array.
- Compare each item with target.
- Set found to true (or output) when they match.
- Output the result after the loop.
Also accept: A while loop that stops as soon as the name is found.
Don't accept: Just "if target in names". This does not show the linear search.
Example answer
found = false for i = 0 to names.length - 1 if names[i] == target then found = true endif next i print(found)
1 mark for each point. Python is fine too.
Binary search
2, 5, 7, 11, 13, 17, 19, 23, 29
Hint
Find the middle item (13). Is 7 smaller or bigger? Throw away the half it can't be in, then repeat.
Show means show each step of your working, not just the answer.
3 marks: your working can earn marks as well as the answer.
Show mark scheme
- Middle is 13; 7 is smaller, so discard 13 and everything after it.
- Remaining 2, 5, 7, 11. Middle is 5 (or 7); 7 is bigger, so discard 5 and before.
- Middle is 7, so it is found.
Don't accept: Checking 2, 5, then 7 from the start. That is a linear search.
Accept either middle item when the list has an even number of items.
Hint
A binary search throws away half the list after checking the middle. What must be true about the list for that to work?
State means give a short, clear answer. You don't need to explain it.
1 mark: one answer is enough.
Show mark scheme
- The data must be sorted / in order.
Which search is faster?
Hint
What is special about this list? And how much of the list can each search rule out with one check?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- The list is sorted, so a binary search can be used.
- Binary search halves the list each time, so it needs far fewer checks / is faster on a large list.
1 mark for each point.
Hint
Think about what kind of data it works on, and how long it takes on a huge list.
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Advantage: works on unsorted data / simple to code.
- Disadvantage: slow on large lists / may have to check every item.
1 mark for the advantage, 1 mark for the disadvantage.
Bubble sort
6, 2, 8, 3
Hint
Compare 6 and 2, then 6 and 8, then 8 and 3. Swap each pair that is the wrong way round.
Show means show each step of your working, not just the answer.
2 marks: your working can earn marks as well as the answer.
Show mark scheme
- Swaps 6 and 2, then 8 and 3 (no swap for 6 and 8).
- Result: 2, 6, 3, 8
1 mark for the swaps, 1 mark for the result.
Hint
When does a bubble sort stop doing passes?
Explain means say what happens and why. Words like “because” and “so” help.
1 mark: one point, with the reason.
Show mark scheme
- A whole pass is made with no swaps.
Hint
Is it easy to write? Is it quick on a big list?
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Advantage: simple to code / understand; doesn't need extra memory.
- Disadvantage: slow / inefficient on large lists.
1 mark for the advantage, 1 mark for the disadvantage.
Insertion sort
8, 3, 6, 1
Hint
Start with the second item, 3. Move it left into place. Then do the same with 6, then with 1.
Show means show each step of your working, not just the answer.
3 marks: your working can earn marks as well as the answer.
Show mark scheme
- Insert 3: 3, 8, 6, 1
- Insert 6: 3, 6, 8, 1
- Insert 1: 1, 3, 6, 8
Don't accept: Only the final sorted list. Each stage needs to be shown.
1 mark for each stage.
Hint
When does an insertion sort have the least work to do?
Give means give a short, clear answer. You don't need to explain it.
1 mark: one answer is enough.
Show mark scheme
- The list is small / nearly sorted already.
Merge sort
9, 4, 6, 2, 8, 1, 5, 3
Hint
First split until every item is on its own. Then merge into pairs, then fours, then all eight.
Show means show each step of your working, not just the answer.
4 marks: your working can earn marks as well as the answer.
Show mark scheme
- Split into halves until single items: 9 | 4 | 6 | 2 | 8 | 1 | 5 | 3
- Merge into pairs: 4 9 | 2 6 | 1 8 | 3 5
- Merge into fours: 2 4 6 9 | 1 3 5 8
- Final merge: 1 2 3 4 5 6 8 9
Also accept: The splitting shown in stages (fours, then twos, then ones) before the merging.
Don't accept: Swapping pairs of neighbours. That is a bubble sort.
1 mark for each stage.
Hint
Which one is faster on big lists? Which one uses more memory?
Compare means say how the two things are similar or different. Mention both of them in each point.
2 marks: each point should mention both things.
Show mark scheme
- Merge sort is faster / more efficient on large lists.
- Merge sort uses more memory / is harder to code.
Don't accept: Describing how each sort works without comparing them.
1 mark for each point. Each point must compare the two sorts.
Which sort?
low = 0 high = items.length - 1 found = false while found == false AND low <= high mid = (low + high) DIV 2 if items[mid] == target then found = true elseif items[mid] < target then low = mid + 1 else high = mid - 1 endif endwhile
Hint
Look at the variable names, and at how mid is worked out.
Name means a word or a short phrase is enough.
2 marks: make 2 separate points.
Show mark scheme
- Binary search.
- It finds a middle point / uses low and high / halves the search area each time.
1 mark for the name, 1 mark for the reason.
for i in range(len(items) - 1): for j in range(len(items) - 1 - i): if items[j] > items[j + 1]: temp = items[j] items[j] = items[j + 1] items[j + 1] = temp
Hint
Two loops, comparing items[j] with items[j + 1]. Which algorithm does that?
Name means a word or a short phrase is enough.
2 marks: make 2 separate points.
Show mark scheme
- Bubble sort.
- It compares items next to each other / swaps adjacent items using temp.
1 mark for the name, 1 mark for the reason.
for i = 1 to items.length - 1 current = items[i] pos = i while pos > 0 AND items[pos - 1] > current items[pos] = items[pos - 1] pos = pos - 1 endwhile items[pos] = current next i
Hint
Look at the while loop: it shifts items one place right to make a gap for current.
Name means a word or a short phrase is enough.
1 mark: one answer is enough.
Show mark scheme
- Insertion sort.
Mixed questions on the whole page
Hint
Look at the order of the numbers. What does a binary search do after it checks the middle item?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- The list is not sorted / not in order.
- Binary search rules out half the list by comparing with the middle item, which only works if the items are in order / it could throw away the half that contains the item.
Don't accept: "The list is too short". A binary search only works on a sorted list.
1 mark for each point.
3, 9, 14, 22, 30, 41, 56
Hint
Find the middle item (22). Is 41 bigger or smaller? Keep the half it could be in, then repeat.
Show means show each step of your working, not just the answer.
3 marks: your working can earn marks as well as the answer.
Show mark scheme
- Middle item is 22. 41 is bigger, so discard 22 and everything before it.
- Remaining list is 30, 41, 56. Middle item is 41.
- 41 is found.
Also accept: Showing index positions instead of values, e.g. the middle is at index 3.
Don't accept: Checking the items one by one from the start. That is a linear search.
1 mark for each step.
7, 3, 9, 2
Hint
Compare each pair from left to right: 7 and 3, then 7 and 9, then 9 and 2.
Show means show each step of your working, not just the answer.
3 marks: your working can earn marks as well as the answer.
Show mark scheme
- Compare 7 and 3, swap: 3, 7, 9, 2.
- Compare 7 and 9, no swap.
- Compare 9 and 2, swap: 3, 7, 2, 9.
Don't accept: 3, 2, 7, 9. That is the list after the second pass.
Don't accept: The sorted list 2, 3, 7, 9. The question asks for the first pass only.
1 mark for each comparison.
Hint
Two stages: splitting, then merging. What happens in each?
Describe means say what it is like or what happens. Make each point a separate fact.
3 marks: make 3 separate points.
Show mark scheme
- The list is repeatedly split in half…
- …until each list contains one item.
- Pairs of lists are merged together in order…
- …until there is one sorted list.
Also accept: A worked example with a list, if it shows both the splitting and the merging.
Don't accept: Swapping pairs of neighbours. That is a bubble sort.
1 mark each, max 3
Hint
Look at the word "unsorted". Which search works on any list?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- Linear search.
- The list is unsorted, so a binary search can't be used unless it is sorted first / with only 50 items a linear search is quick enough.
Don't accept: Binary search. It only works on sorted data.
1 mark for linear search, 1 mark for the reason.