CS11-02 Computer Science Watch
Binary search
In this lesson
Binary search: how to state that a binary search needs the list to be sorted, with worked examples and the mistakes examiners report. AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 2 of 7. More at https://scholafly.com
By the end: State that a binary search needs the list to be sorted, and describe and apply it - compare the target with the middle item, stop if they match, otherwise discard the middle item and the half that cannot hold the target, and repeat on what is left until the item is found or nothing is left.
What it covers
- 0:00 Intro: binary search
- 0:41 Binary search
- 4:19 Binary search: searching for 12
- 7:59 Names count as sorted
- 9:58 An unsorted list
Key words
About this video
Binary search | GCSE Computer Science | CS11-02
Binary search: how to state that a binary search needs the list to be sorted, with worked examples and the mistakes examiners report.
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 2 of 7.
More at https://scholafly.com
Watch first: CS11-01 - https://youtu.be/5LPPPeks4zc
Video code: CS11-02 (search "ScholaFly CS11-02")
#GCSEComputerScience #SearchingAndSorting #BinarySearch
For teachers
This GCSE Computer Science lesson teaches binary search. By the end, students should be able to state that a binary search needs the list to be sorted, and describe and apply it - compare the target with the middle item, stop if they match, otherwise discard the middle item and the half that cannot hold the target, and repeat on what is left until the item is found or nothing is left. It works through four worked examples and the mistakes examiners report.
Exam board specification references:
AQA 8525
- 3.1.3
Edexcel 1CP2
- 1.2.6
OCR J277
- 2.1.3
Read the transcript
Someone picks a number from one to a thousand and answers each guess with only higher or lower. Can ten guesses always find it? Ten sounds hopeless against a thousand, but choose each guess well and it is enough every time. Behind that is a search computers run on lists, and it has one condition that must hold before it can even start. The final question of the video shows why ten guesses are always enough.
A binary search, binary because each step splits what is left in two, only works on a list that is already sorted, in order from smallest to largest. Sorted does not have to mean numbers. Names in alphabetical order are sorted too, and binary search runs on them the same way. Instead of starting at the front, it starts in the middle. It compares the middle item with the target, and there are three possible outcomes. Equal: the target is found, and the search stops. Target smaller than the middle item: it can only be in the left part. Target larger: it can only be in the right part. The part that cannot hold the target is thrown away, and the middle item goes with it, because it has already been checked. What is left is called the part still in play, and the search repeats on it. Here is a sorted list of nine: three, eight, eleven, fifteen, twenty, twenty-four, twenty-nine, thirty-three, forty-one. The target is twenty-nine. Number the positions one to nine. The middle position is the first plus the last, divided by two. One plus nine is ten, and half of ten is five. Position five holds twenty. Twenty-nine against twenty: which part of the list stays in play? The right part. Twenty-nine is larger than twenty, so only twenty-four, twenty-nine, thirty-three and forty-one stay in play. Twenty itself goes as well. It was the middle, it has been checked, and it is not the target. Four items are left, in positions six to nine, and four items have no single middle. Six plus nine, divided by two, is seven and a half. This video rounds down every time, so the middle is position seven. Rounding up works too, as long as one rule is kept all the way through. Position seven holds twenty-nine. Compare it with the target: equal. Found, after two comparisons. One examiner's report says this about describing the search: The majority of candidates clearly understood that the middle value is chosen and compared against the value being searched for. Further on, it adds: One possibility is that the middle value is the number we are searching for. Few candidates discussed this possibility. Both are true at once: starting in the middle was well known, and the equal outcome was the one left out. The fix is to name equal first, every time, because it is where the search ends.
A search for a missing number shows the part in play shrinking all the way to nothing. Same sorted list of nine, and the target is twelve. Low marks the first position still in play, and high marks the last. They start at one and nine. The middle is position five, twenty. Twelve is smaller, so the left part stays, and the middle goes too. High moves to four, one below the middle. Low at one, high at four, target twelve: what is the next middle, and which marker moves? One plus four, over two, is two and a half, rounded down to two. Position two holds eight, twelve is larger, so low moves to three. In play now: eleven and fifteen. Three plus four, over two, is three and a half, rounded down to three. Eleven is smaller than twelve, so low moves to four. Low and high are both four now. The middle is position four, fifteen. Twelve is smaller, so high moves to three. Low is four and high is three. What does that tell the search? Nothing is left in play, so twelve is not in the list. The search stops and reports not found. Count what was in play at each step: nine, then four, then two, then one, then none. An empty part means the search has run out of places to look, just as reaching the end of the list did in a linear search. From one examiner's report: Another common error was to continue to include the midpoint rather than discarding it when deciding on the bounds for the new sublist. The fix is to write the new low or high as one past the middle, never the middle itself. Low becomes the middle plus one, or high becomes the middle minus one. Breaking a problem into smaller parts, then dealing with those parts one at a time, is called divide and conquer. No course prints the term in its specification, but one has asked for it by name. Now a question asks for a search that uses divide and conquer, and offers three options. Linear search, binary search, or either: which is the divide and conquer search? Binary search. It divides what is left at every step and deals with just one part, while a linear search moves one item at a time and never divides anything.
Names work the same way, because alphabetical order is an order. Eight names, in alphabetical order: Ava, Ben, Cal, Dev, Eli, Fay, Gus, Hal. The target is Fay. With no single middle, the same rule holds as before: round down, every time. Rounding down, which name is the first middle, and which way does the search for Fay go? Dev, at position four, because one plus eight over two is four and a half, rounded down. Fay comes after Dev in the alphabet, so the right part stays. Dev goes with the left part. Eli, Fay, Gus and Hal stay in play, positions five to eight. Five plus eight over two is six and a half, so position six: Fay. Equal, and found in two comparisons. One examiner's report gives a figure: With question twelve point three only fifty per cent of students could identify that the list needed to be sorted for a binary search with a common misconception being that a list of strings cannot be ordered or the binary search only works on lists of numbers. Two habits answer both halves of that. Say sorted before the first step of any description, and treat names in alphabetical order as sorted.
Now the same method, run on a list that breaks the sorted rule. Here the list is in no order at all, and the target sits at position five. Fourteen, two, nine, thirty, twenty-one: binary search it for twenty-one. What happens? It reports not found. Nine is the first middle, and twenty-one is larger, so only thirty and twenty-one stay in play. The next middle, rounding down, is thirty. Twenty-one is smaller, so only the left part may stay, and on that side nothing is left. Twenty-one was thrown away with the right part. Halving only works because, in a sorted list, everything left of the middle is smaller and everything right of it is larger. Without that order, throwing a part away can throw the target away. In code, a binary search gives itself away with three things: a low and a high marker, a line that works out the middle, and lines that move low or high one past the middle. DIV divides and drops the remainder, which is the rounding down.
Sorted, middle, three outcomes, middle gone: four questions put those to work on lists you have not seen. In two, five, nine, thirteen, eighteen, twenty-two, thirty, which is the first middle? Thirteen, at position four. One plus seven is eight, and half of eight is four. Now search that list for twenty-two: how many comparisons does it take? Two comparisons. Twenty-two is larger than the first middle, so low becomes five, and the next middle is twenty-two itself. A different list, of names: Ali, Cat, Bo, Dan. Will a binary search for Bo find it? No. Those names are not in alphabetical order. The search checks Cat, Bo comes before Cat, so only Ali is kept and Bo is missed. Back to the number game for one more: why do ten guesses always cover a thousand? Each guess goes in the middle, so every higher or lower throws away about half. A thousand, five hundred, two hundred and fifty, and on, until by the tenth guess only one number can be left.
If you have nailed it, a thumbs up marks this video as done, so you never have to come back to it. If the halving still feels slippery, save it for later; nobody halves cleanly the first time, and the second try is always tidier.
Next in the chapter: Linear or binary, which and why. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh three.
Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.
Related terms
For: AQA GCSE 8525, Edexcel GCSE 1CP2, OCR GCSE J277
On the specification
For teachers
This GCSE Computer Science lesson teaches binary search. By the end, students should be able to state that a binary search needs the list to be sorted, and describe and apply it - compare the target with the middle item, stop if they match, otherwise discard the middle item and the half that cannot hold the target, and repeat on what is left until the item is found or nothing is left. It works through four worked examples and the mistakes examiners report.