ScholaFly

CS11-03 Computer Science Watch

Linear or binary: which and why

Watch on YouTube

In this lesson

Linear or binary: how to compare linear and binary search, with worked examples and the mistakes examiners report. AQA 8525 GCSE. Searching and sorting 3 of 7. More at https://scholafly.com

By the end: Compare linear and binary search, giving each one's advantages and disadvantages with a reason attached: binary search needs far fewer comparisons on a large list but only works if the list is sorted; linear search works on any list and is simpler, but may have to check every item.

What it covers

  1. 0:00 Intro: linear or binary
  2. 0:34 Linear and binary search
  3. 1:31 Large and sorted
  4. 3:20 Linear or binary: a café till
  5. 5:06 Advantages and disadvantages
  6. 6:46 Choose a search

Key words

About this video

Linear or binary: which and why | GCSE Computer Science | CS11-03

Linear or binary: how to compare linear and binary search, with worked examples and the mistakes examiners report.
AQA 8525 GCSE. Searching and sorting 3 of 7.
More at https://scholafly.com

Watch first: CS11-02 - https://youtu.be/sFtMyGuu4H0
Video code: CS11-03 (search "ScholaFly CS11-03")

#GCSEComputerScience #SearchingAndSorting #LinearOrBinary

For teachers
This GCSE Computer Science lesson teaches linear or binary: which and why. By the end, students should be able to compare linear and binary search, giving each one's advantages and disadvantages with a reason attached: binary search needs far fewer comparisons on a large list but only works if the list is sorted; linear search works on any list and is simpler, but may have to check every item. It works through three worked examples and the mistakes examiners report.

Exam board specification references:
AQA 8525
- 3.1.3

Read the transcript

A school office needs one of two thousand five hundred student records, now. Which search should it use? Binary search is tempting: a handful of looks, where a linear search might need them all. But these records are in sign-up order, not by name, and binary search needs a sorted list. So the answer depends on the list, and must say why.

One course prints this comparison as a statement of its own. On the others it is still useful practice, and you can treat this video as optional. Two short pictures carry everything this comparison needs. Linear search starts at the first item and compares each one in turn with the target. It works on any list, in any order. Binary search compares the middle item, keeps the part that can hold the target, and repeats. It only works if the list is sorted. Every point in a comparison has three parts: the claim, when it holds, and because. A claim on its own is a label, not an answer.

Take two thousand five hundred student ID numbers, already sorted, and one ID to find. A linear search might compare every single one. If the ID is last, or missing, that is two thousand five hundred comparisons. And binary search at most: about twelve, about fifty, or about twelve hundred comparisons? About twelve. Each comparison throws away roughly half of what is left. Two thousand five hundred, then about twelve hundred and fifty, six hundred and twenty-five, three hundred and twelve, and on, halving each time. About eleven halvings leave one ID, and one more look settles it. That gives binary search its advantage, said with all three parts. It needs far fewer comparisons on a large sorted list, because it halves what is left at every step. One examiner's report gives a figure for a question of this kind: Over eighty per cent of students gained at least one mark when explaining why the binary search was better with the sorted list. What turns a claim into an explanation is the reason after it: fewer comparisons, because it halves the part still in play.

The comparison can go the other way, and an honest answer says when. A café till holds today's eight orders, in the order they arrived. One order needs finding. For this till's list, which search fits, and why? Linear search. The orders are not sorted, so binary search cannot be used until someone sorts them. With only eight of them, a linear search needs at most eight comparisons. Sorting first would cost more work than the binary search could ever save. Even on a sorted list, size matters. On four items, linear search needs at most four comparisons and binary search at most three, so the gap hardly matters. Now three lists. A, a million product codes in sorted order. B, twelve names in no order. C, six sorted numbers. Which of the three lists gains the most from using a binary search? A. With so many items in order, halving saves a vast number of comparisons. B is in no order, and C is too short for the gain to matter.

Each search has a disadvantage too, and each one needs its because. Binary search needs the list sorted first, and sorting takes time, so on unsorted data it can cost more than it saves. It is also harder to write, because it has to track low, high and the middle. Linear search may have to check every item, because it can only move one item at a time. On a large list, that makes it slow on average. Simpler to write is a fair advantage for linear search, but only with its because. It works on data in any order, so no time is spent sorting first. A student writes this sentence: binary search is better because it is faster. Pause and rewrite that binary search sentence with a claim, a when and a because. One version: binary search needs fewer comparisons on a large sorted list, because it halves the part still in play at every step. Read it back against the three parts. Fewer comparisons is the claim, a large sorted list is the when, and halving is the because.

Four situations to finish with, and for each one, a search to choose. A sorted phone book of fifty thousand names: which search, and why? Binary search. The names are in order and there are a lot of them, so halving needs far fewer comparisons. Now ten game scores, unsorted, straight off the scoreboard: which search? Linear search, because the scores are not sorted, and ten is too few for sorting first to pay off. Try fixing this point: linear search is slow. What has to follow it? When it is slow, and its because: slow on a large list, because it may have to check every item, one at a time. And the two thousand five hundred records in sign-up order, one more time: which search today? Linear search, today, because the records are not sorted. Sort them once by name, and binary search wins every lookup after that.

If you could argue either side of this now, give the video a thumbs up, so it is marked as one you never need again. Otherwise keep it saved and give it a day; reasons like these read differently with fresh eyes tomorrow.

Next in the chapter: Bubble sort, the first of the sorts. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh four.

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

On the specification

BoardSpecStatement
AQA GCSE 85253.1.3Searching algorithms
For teachers

This GCSE Computer Science lesson teaches linear or binary: which and why. By the end, students should be able to compare linear and binary search, giving each one's advantages and disadvantages with a reason attached: binary search needs far fewer comparisons on a large list but only works if the list is sorted; linear search works on any list and is simpler, but may have to check every item. It works through three worked examples and the mistakes examiners report.