Computing
Step into a digital world.....

J277 17: Searching Algorithms

OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2

Searching algorithms

Linear search and binary search. One checks everything, one halves the problem.

Paper 2 · Lesson 17

1 / 20
volgende
Slide 1: Slide
newEditorComputer ScienceUpper Secondary (Key Stage 4)GCSE

In deze les zitten 20 slides, met interactieve quizzen en tekstslides.

time-iconLesduur is: 60 min
Dit is niet oké

Introductie

OCR GCSE Computer Science (J277), Paper 2, lesson 17 of 26 (Block C). Students learn how a linear search checks each item in turn and how a binary search repeatedly checks the middle item of a sorted list and discards half, then compare the two algorithms and trace a binary search step by step the way the exam expects. Activities include a do now on lesson 16, picture questions, a drag-and-drop comparing the searches, a spot-the-mistake task, a sorting question on the binary search steps, fill in the blanks and an exam-style question with a model answer. This lesson was created with AI assistance.

Onderdelen in deze les

OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2

Searching algorithms

Linear search and binary search. One checks everything, one halves the problem.

Paper 2 · Lesson 17

Lesson 17 of the J277 series (Block C, Paper 2, section 2.1.3 Searching and sorting algorithms).

Device-free: show the slide; pupils write the title and date. Image credit: Sasun Bughdaryan / Unsplash (cover hologram)

00 · MISSION · BY THE END OF THIS LESSON

By the end of this lesson you can…

01  Describe how a linear search finds an item.

02  Show the steps of a binary search on a sorted list.

03  Compare the two searches and say when to use each.

Read the three outcomes. J277 expects pupils to understand the main steps of linear and binary search, apply them to a data set and identify the algorithm from code or a description. They do not need to write the code from memory, but should be able to follow it.

Device-free: pupils copy the outcomes and tick each one at the end.

02 · CHECK · DO NOW (LESSON 16)

Warm up: designing algorithms

1  Which flowchart symbol shows a decision?

2  What does a parallelogram show?

3  What is pseudocode?

4  What is a trace table used for?

▮ TERMINAL // do_now_answers

1  a diamond (Yes / No exits)

2  an input or an output

3  an algorithm written in

   code-like English

4  recording the value of each

   variable as it runs

Retrieval from lesson 16. Give 4 minutes, then click to reveal the answers line by line.

1 A diamond, with Yes and No exits. 2 An input or an output. 3 A way of writing an algorithm in structured, code-like English without the strict syntax of a real language. 4 Recording the value of each variable (and any output) as an algorithm runs, to check it or find errors.

Device-free: questions on the board; mini whiteboards.

Recap trace: x = 2, then x = x * 3 runs twice. What is x now?

A

6

B

18

C

12

D

8

Answer: B, 18. Trace it: x = 2; first time x = 2 3 = 6; second time x = 6 3 = 18.

Common wrong answers: 6 (stopped after one go) and 12 (multiplied the 6 by 2 instead of 3). A quick trace table on the board settles it.

Device-free: students hold up A, B, C or D cards. Image credit: Luca Bravo / Unsplash

01 · LEARN · MISSION BRIEFING

Find it fast

Every app that finds a contact, a song or a product runs a search algorithm.

LINEAR SEARCH

Check every item, one after another.

BINARY SEARCH

Check the middle, throw half away, repeat.

WHICH ONE?

Sorted or not? Long list or short?

SYSTEM VOICE

System online. Today's mission: find data fast. Learn how a linear search checks every item in turn, how a binary search halves a sorted list again and again, and when to use each one.

Click play for the system voice briefing. The words are also on the slide.

Quick hook: ask how pupils would find one name in a class list of 30, then in a phone book of a million names. Most will describe a linear search for the first and something like a binary search for the second.

Device-free: pupils write the two search names and a one-line description of each.

01 · LEARN · LINEAR SEARCH

One item at a time

1  Start at the first item.

2  Is it the target? If yes, stop: found.

3  If not, move to the next item.

4  Repeat until it is found or there are no items left.

The list does not need to be sorted.

▮ TERMINAL // linear_search.erl

found = false

i = 0

while i < items.length AND found == false

    if items[i] == target then

        found = true

    else

        i = i + 1

    endif

endwhile

Click to reveal the steps. A linear search checks each item in turn from the start of the list until the target is found or the end of the list is reached.

The terminal shows a linear search in OCR Exam Reference Language. Point out: the loop stops as soon as found becomes true, and i moves on one place each time. Pupils need to recognise this code, not write it from memory.

Device-free: pupils write the four steps in their own words.

01 · LEARN · LINEAR SEARCH IN ACTION

Find 4 in: 7, 3, 9, 4, 6

▮ TERMINAL // search_log

check 1  7 = 4 ?  no

check 2  3 = 4 ?  no

check 3  9 = 4 ?  no

check 4  4 = 4 ?  FOUND

> found at position 4_

BEST CASE

The target is first: 1 check.

WORST CASE

The target is last, or missing: every item is checked.

Think: how many checks to find 6? And to find 8, which is not in the list?

Click to run the search one check at a time. Each item is compared with the target until it matches.

Think: finding 6 takes 5 checks (it is last). Finding 8 also takes 5 checks: every item is compared before the search can report "not found". That is the worst case: for a list of n items, up to n checks.

Device-free: pupils write the four checks, then the checks needed for 6 and for 8.

01 · LEARN · BINARY SEARCH

Halve it, again and again

1  The list must be sorted.

2  Check the middle item.

3  Found it? Stop.

4  Target smaller? Keep the left half. Bigger? Keep the right half.

5  Repeat with the half that is left.

Remember: every check cuts the list in half.

Click to reveal the steps. The picture is the idea to remember: every check cuts what is left in half, and one half is thrown away. It is also how you look up a word in a dictionary: you open it near the middle, see whether your word comes before or after, and ignore the other half.

Stress step 1: binary search only works on a sorted list. If the data is unsorted, it must be sorted first or a linear search used instead.

Device-free: pupils play "guess my number" from 1 to 100 in pairs, always guessing the middle of what is left. It never takes more than 7 guesses. Image credit: Lucas George Wendt / Unsplash

01 · LEARN · BINARY SEARCH IN ACTION

Find 23 in a sorted list

▮ TERMINAL // sorted_list

index  0  1  2  3  4  5  6  7  8  9

item   2  5  8 12 16 23 38 56 72 91

 

mid = (first + last) DIV 2

3 CHECKS

A linear search would need 6 checks to reach 23. On a list of 1,000 items, binary search needs at most 10.

first 0, last 9

mid 4 → 16. 23 > 16: go right

first 5, last 9

mid 7 → 56. 23 < 56: go left

first 5, last 6

mid 5 → 23. Found!

Click to reveal each step. OCR finds the middle with mid = (first + last) DIV 2, where DIV is whole-number division.

Step 1: first 0, last 9, mid = 9 DIV 2 = 4, item 16. 23 is bigger, so discard 16 and everything left of it: first becomes 5. Step 2: first 5, last 9, mid = 14 DIV 2 = 7, item 56. 23 is smaller, so last becomes 6. Step 3: first 5, last 6, mid = 11 DIV 2 = 5, item 23. Found in 3 checks.

Some textbooks round the middle up instead of down. The exam accepts either, as long as it is used the same way every time.

Device-free: pupils copy the list and cross out the discarded half at each step.

01 · LEARN · COMPARE

Linear or binary?

LINEAR SEARCH

+ works on unsorted data

+ simple to write

– slow on long lists

BINARY SEARCH

+ far fewer checks on long lists

– the data must be sorted first

– harder to write

SYSTEM VOICE · KEY DEFINITION

A binary search checks the middle item of a sorted list. If it is not the target, it throws away the half that cannot contain it, and repeats with the half that is left.

The trade-off examiners look for: binary search is much more efficient on large lists, but only works if the data is sorted. Linear search works on any list and is simpler, but on a long list it may check every item.

For a short list (say 10 items) the difference hardly matters. Sorting the data just to do one search may not be worth it.

Play the key definition. Device-free: pupils draw the two columns and add one more point to each.

Linear search or binary search?

LINEAR SEARCH

BINARY SEARCH



Checks the middle item first

Works on an unsorted list

Simple, but slow on long lists

The list must be sorted first

Discards half the list each check

Checks every item in turn

Answers. Linear search: works on an unsorted list; simple, but slow on long lists; checks every item in turn. Binary search: checks the middle item first; the list must be sorted first; discards half the list each check.

Device-free: pupils draw two columns and write each statement in the right one.

A linear search looks through 1,000 books. What is the most checks it could need?

A

10

B

500

C

2000

D

1000

Answer: D, 1000. In the worst case (the book is last, or not there at all) a linear search checks every item, once each. 2000 would mean checking each book twice.

500 is the average number of checks, not the most. 10 is roughly the most a binary search would need, but only if the books were sorted.

Device-free: students hold up A, B, C or D cards. Image credit: Jayanth Muppaneni / Unsplash

Binary search on 3, 8, 11, 15, 20, 27, 31. Which item is checked first?

A

20

B

3

C

15

D

27

Answer: C, 15. There are 7 items (index 0 to 6), so mid = (0 + 6) DIV 2 = 3, and the item at index 3 is 15, the middle value.

Wrong answer 3: that is where a linear search starts. The card catalogue in the picture is sorted alphabetically, which is exactly what makes a binary-style search possible.

Device-free: students hold up A, B, C or D cards. Image credit: Jan Antonin Kolar / Unsplash

03 · DEBUG · SPOT THE MISTAKE

One line in Priya's notes is wrong

▮ TERMINAL // priya_searching.txt

1  linear: checks items one by one

2  linear: works on unsorted data

3  binary: works on any list

4  binary: discards half each check

5  binary: faster on long lists

Check each line against what you know. Then tap the marker.

Tip: "the data must be sorted" is the point examiners look for first.

Give a minute to find the mistake before tapping the marker.

Answer: line 3. Binary search needs the data in order, otherwise discarding half the list could throw away the target. Lines 1, 2, 4 and 5 are correct.

Device-free: copy the five lines onto the board; pupils write the wrong line number and the correction.

1

Make sure the list is sorted

2

If they match, stop

3

Repeat with the half that is left

4

Find the middle item

5

Discard the half that cannot hold the target

6

Compare the middle item with the target

Put the steps of a binary search in order.

Correct order: 1 make sure the list is sorted; 2 find the middle item; 3 compare it with the target; 4 if they match, stop; 5 otherwise discard the half that cannot hold the target; 6 repeat with the half that is left (until found or no items remain).

Ask: when does a binary search report "not found"? (When there are no items left to check.)

Device-free: pupils write the six steps on sticky notes and order them on the desk.

Complete the summary.

A linear search checks every item in turn, starting with the first. A binary search only works if the list is sorted. It checks the middle item, then discards half of the list. On a long list, binary search is usually much faster.

linear
sorted
middle
half
faster
slower
random
encrypted

Answers in order: linear, sorted, middle, half, faster. Distractors: slower, random, encrypted.

Device-free: paragraph on the board with gaps and the word bank; pupils write the five missing words.

EXAM · A sorted list holds 4, 9, 14, 21, 30, 38, 45, 52, 60. Show how a binary search finds 52. [4 marks]

Exam-style question, 4 marks. Give 6 minutes. The model answer is on the next slide.

Mark scheme: 1 mark comparing 52 with the middle item, 30; 1 mark discarding 30 and the left half because 52 is bigger; 1 mark comparing with the new middle, 45, and discarding again; 1 mark comparing with 52 and finding it.

Accept rounding the middle up instead of down if used consistently (then 52 is found at the second check: 30, then 52). Do not award marks for a linear search.

Device-free: pupils answer on paper, then peer-mark with the next slide.

04 · EXAM · MODEL ANSWER

Middle, compare, discard

▮ TERMINAL // binary_search_52

1 middle item is 30

  52 > 30: discard 4 to 30

2 middle of 38 45 52 60 is 45

  52 > 45: discard 38 and 45

3 middle of 52 60 is 52

  52 = 52: FOUND

MARK SCHEME

1 mark: compare with 30

1 mark: discard the left half

1 mark: compare with 45 and discard

1 mark: compare with 52, found

Rounding the middle up is fine if you do it every time.

Click to reveal the model answer step by step.

With mid = (first + last) DIV 2: indexes 0 to 8 give mid 4 (30); then first 5, last 8 give mid 6 (45); then first 7, last 8 give mid 7 (52). Three comparisons.

Common mistakes: forgetting to discard the middle item itself, and starting at the first item (that is a linear search and scores 0).

Device-free: pupils check their own answer against the four marks and correct it in a different colour.

How sure are you about searching?

1

Very: I can trace both searches

2

Fairly: binary search is tricky

3

Not yet: I need another go

Anonymous exit poll. If many pupils find binary search tricky, start lesson 18 with a quick "guess my number" game and one more traced example.

Device-free: thumbs up, sideways or down.

05 · NEXT · KEEP GOING

Keep going

Isaac Computer Science

Free online lessons and questions. Look for the searching algorithms topic.

BBC Bitesize

GCSE Computer Science, OCR: revise searching and sorting algorithms.

Guess my number

Play it at home: 1 to 100, always guess the middle. You never need more than 7 guesses.

Next lesson: sorting algorithms, bubble sort, insertion sort and merge sort.

Point pupils to the places to practise. Isaac Computer Science and BBC Bitesize are free. Guess my number is a binary search: each guess halves the numbers left, and 2 to the power 7 is 128, more than 100, so 7 guesses always do it.

Device-free: pupils write a sorted list of 8 friends' names and show how a binary search would find one of them, to bring to lesson 18.