Year 8 · Summer 1

Algorithms: search and sort · Lesson 1 of 5

Linear vs binary search with numbered cards

UnpluggedTopic 12 Searching and sorting

Year 8 · Algorithms: search and sort · Lesson 1 of 5

You will learn to

  • Carry out a linear search on cards, checking each one in turn.
  • Carry out a binary search on sorted cards: check the middle, then throw away half.
  • Explain why binary search only works on a sorted list.
  • Compare the checks each search needs, using the binary search toy.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Success criteria

You have got it when you can say:

  • I can find 17 in twenty sorted cards both ways, and count my checks.
  • I can explain why binary search needs at most 5 checks for 20 cards.
  • I can choose the right search for a sorted list and for an unsorted list.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last unit

    x = input("Number: ") and the user types 5. What type is x?

    A intB floatC str

    C. str

  2. Last unit

    What does 17 % 5 give?

    A 3.4B 3C 2

    C. 2

  3. Earlier this year

    A '1 TB' drive shows 931 GB in Windows because…

    A Some space is brokenB The drive is fakeC Windows divides by 1,024 but still writes GB

    C. Windows divides by 1,024 but still writes GB

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Key words (1 of 2)

Linear search
Check every item in turn.
Binary search
Search a sorted list by halving it each step.
Bubble sort
Swap neighbours until a whole pass makes no swaps.
Insertion sort
Slide each item into its place in the sorted part.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Key words (2 of 2)

Merge sort
Split into single items, then merge back in order.

The explanation · step 1 of 2

Linear search

Linear search: check every item from the start until you find it.

  • Works on any list, sorted or not.
  • Simple.
  • Slow on long lists: a million items may need a million checks.

The explanation · step 2 of 2 · part 1 of 2

Binary search

Binary search needs a sorted list. Like a dictionary: open in the middle, throw away the wrong half, repeat.

  1. Look at the middle item.
  2. Is it the one? Stop.
  3. Is your item bigger? Throw away the left half. Smaller? Throw away the right half.
  4. Repeat with what's left.

The explanation · step 2 of 2 · part 2 of 2

Binary search (continued)

ItemsLinear (worst)Binary (worst)
10104
1,0001,00010
1,000,0001,000,00020
?Why?

Why so fast? Each check halves the list. Doubling the list only adds one check. (2¹⁰ = 1,024 and 2²⁰ ≈ 1 million.)

The explanation · step 2 of 2

The maths: Most checks binary search can need

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Try it: Binary search, step by step

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Quick quiz · 1 of 3

Binary search only works if the list is…

  1. A Made of numbers
  2. B Short
  3. C Sorted

C. Sorted

Throwing away half only makes sense if the list is in order. Sorted words work too.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Quick quiz · 2 of 3

What is the most checks binary search can need on 1,000 sorted items?

  1. A 1,000
  2. B 10
  3. C 500

B. 10

1,000 halves 9 times to reach 1 (2⁹ = 512), plus one last check: 9 + 1 = 10.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Quick quiz · 3 of 3

A linear search on 50 unsorted items, looking for something that is not there, makes…

  1. A 25 checks
  2. B 50 checks
  3. C 1 check

B. 50 checks

It can only say 'not found' after checking every item: the worst case, n = 50.

Year 8 · Algorithms: search and sort · Lesson 1 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. Bubble sort compares…

    A Random pairsB NeighboursC The first and last items

    B. Neighbours Items next to each other, swapping them if they're in the wrong order.

  2. During bubble sort, a whole pass makes no swaps. This means…

    A Start again from the beginningB The list is sorted: stopC Do one more pass to be safe

    B. The list is sorted: stop No pair was out of order, so the whole list is in order. This is the early stop.