ScholaFly

CS11-01 Computer Science Watch

Linear search

Watch on YouTube

In this lesson

Linear search: how to describe how a linear search works, with worked examples and the mistakes examiners report. AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 1 of 7. More at https://scholafly.com

By the end: Describe how a linear search works - start at the first item, compare each item in turn with the target, stop when it is found or when the end of the list is reached without finding it - apply it to a given list, and recognise it from code or pseudocode.

What it covers

  1. 0:00 Intro: linear search
  2. 0:30 Linear search
  3. 3:35 A missing target
  4. 6:37 Best and worst case
  5. 8:37 Linear search in code

Key words

About this video

Linear search | GCSE Computer Science | CS11-01

Linear search: how to describe how a linear search works, with worked examples and the mistakes examiners report.
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 1 of 7.
More at https://scholafly.com

Watch first: CS10-05 - https://youtu.be/Ioa4-no7NkE
Video code: CS11-01 (search "ScholaFly CS11-01")

#GCSEComputerScience #SearchingAndSorting #LinearSearch

For teachers
This GCSE Computer Science lesson teaches linear search. By the end, students should be able to describe how a linear search works - start at the first item, compare each item in turn with the target, stop when it is found or when the end of the list is reached without finding it - apply it to a given list, and recognise it from code or pseudocode. 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

Two hundred jumbled raffle tickets, and the winning number is called. How many must you check to know you have not won? Most of them feels like plenty, but with no order to go on, the winning number could be on the very last ticket. That one-at-a-time check has a name, and its hard part is knowing when it may stop.

Checking a list one item at a time, in order, starting from the first item, is called a linear search. It is an algorithm, which means a fixed set of steps that solves a problem. The item you are looking for is called the target. At every step, the search compares one item with the target and checks one thing: whether they are equal. Start at the first item and compare it with the target. If they match, the search stops and reports found. If not, it moves to the next item and compares again. It moves one place at a time, left to right, and never skips. The list does not need to be in any order, so every list in this video is jumbled on purpose. Take this one: twelve, seven, thirty-one, four, nineteen, twenty-six. The target is nineteen. How many comparisons will the search make before it stops on nineteen? Five comparisons. Nineteen is the fifth item, and every item up to it gets compared once. Here they are in order. Compare twelve with nineteen: not equal, move on. Compare seven with nineteen: not equal, move on. Thirty-one, not equal. Four, not equal. Then nineteen compared with nineteen. Equal, so the search stops and reports that nineteen is found. Now a different target, twenty-six: how many comparisons this time? Six. Twenty-six sits last, so all six get compared, and the sixth one is the match. One examiner's report describes how written descriptions of this search went wrong. Many responses discussed checking each number but did not state whether this would be in a particular order. Some candidates discussed checking values randomly, or even from the right down to the left. What went missing is where the search starts and the direction it travels. The fix is one sentence you can write every time: start at the first item and check each one in order.

A target that is not in the list ends the search in a different way. Same jumbled list, twelve, seven, thirty-one, four, nineteen, twenty-six, and this time the target is five. Where will the search for five stop, and what will it report? It stops after the last item. Nothing is left to compare, so it reports not found. Every item gets its comparison. Twelve with five, not equal. Seven, thirty-one, four, nineteen and twenty-six are each compared with five, and none of them is equal. After the sixth comparison there is no next item. That is the second way a linear search stops: it runs out of items, and the target has not been found. Every description of this search ends with two exits, said as a pair. Found, and the search stops on the match. Or the end of the list with no match, and it reports not found. Now three students each write one sentence about when the search stops. A, when it finds the item. B, when it reaches the end of the list. C, when it finds the item, or reaches the end of the list without finding it. Which of those three sentences describes the stop in full? C. A gives only the found exit, and B gives only the other one. C carries both. Here is a line from one examiner's report. Question thirteen was a description of a linear search and it was pleasing to see some very detailed answers with nearly seventy per cent of students gaining at least two of the three available marks. A different report names what was left out. However, the final mark point awarded for when the item is not found so the algorithm stops and the explicit point of comparing the item with the target were the most frequently missed marks. Both gaps close with the same habit. Write compare the item with the target, never look at it, and finish every description with the not-found stop.

How many comparisons a linear search makes depends on where the target sits. Take a list of fifty names, in no particular order. Where would the target have to sit for the search to finish fastest? Right at the front. The first comparison is a match, so the search stops at once. That is called the best case. The target is the first item, because only one name has to be compared. Now the other end: what makes a search of those fifty names take longest? Two situations. It sits last, or it is missing altogether. Either way, every one of the fifty gets checked. That is the worst case, and like every case it needs its reason. Worst case: the target is last or not there, because every item before it has to be compared first. For fifty names, that is fifty comparisons. One examiner's report notes: Whilst many candidates identified that the worst case for a linear search was when the item to locate was either the last item or not in the list, fewer were able to give the reason for this. The fix is to put because straight after the case, followed by the count. Name the case, then say how many items had to be compared to get there.

Code for a linear search has a shape, and that shape is what lets you name it. This version searches a list called items for a target the user types in. It has one loop, one comparison inside the loop, and a flag called found, which is a variable that holds True or False. Which line of this code compares an item with the target? Line five. It asks whether the item at position i is equal to the target. Line four keeps the loop going while found is False and i is still inside the list. If line five finds a match, line six sets found to True. Otherwise line eight moves i on by one. Line four is where both exits live. The loop stops when found turns True, or when i runs past the last position. To report where the match was found, which line of this code needs to change? Line twelve, the output for a match. It can output i as well, because i still holds the place of the matching item. Courses differ on how much of this code you need. One prints that it need not be remembered, and another has asked students to write a search like it, which the programming videos teach. Reading it and naming it is the skill here.

Steps, exits and cases come back now, each on a list this video has not searched. Searching nine, three, fourteen, six for the target six: what gets compared first? Nine, the first item, compared with the target. The search always starts at the front. Now try the same four numbers with a target of two: how does the search end? Nine, three, fourteen and six all fail to match. The end of the list arrives, so it reports not found. Here's a list of a thousand codes: what is the worst case, and why? The code is last, or missing, because all thousand codes must be compared first. One more, back to the raffle tickets: when do you know for sure you have not won? Only after the last ticket has been compared with no match. Until then, the winning number could still be further down.

If you could now run this search on any list you were handed, give it a thumbs up, and it is one video you never need to sit through again. If not, save it, and try the search once on a shuffled row of playing cards, because doing it by hand makes it stay.

Next in the chapter: Binary search, a faster search that only works when the list is in order. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh two.

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

BoardSpecStatement
AQA GCSE 85253.1.3Searching algorithms
Edexcel GCSE 1CP21.2.6Algorithms
OCR GCSE J2772.1.3Searching and sorting algorithms
For teachers

This GCSE Computer Science lesson teaches linear search. By the end, students should be able to describe how a linear search works - start at the first item, compare each item in turn with the target, stop when it is found or when the end of the list is reached without finding it - apply it to a given list, and recognise it from code or pseudocode. It works through four worked examples and the mistakes examiners report.