levo

Algorithms

Linear search and binary search

  • 3 things to know
  • Paper 1

Overview

How linear and binary searches work, why a binary search needs a sorted list, choosing the middle item, traced searches through short lists, worst cases, and when each search is the better choice.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

How does a binary search work?

Process

How does a linear search work?

Key term

Does a linear search need the list to be sorted?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    How does a linear search work?

    Model answer

    A linear search checks each item in a list in turn, starting with the first. The search stops when the item is found or when the list runs out.

  2. 2

    Does a linear search need the list to be sorted?

    Model answer

    A linear search works on a sorted or an unsorted list. The search simply compares every item with the target until it finds a match.

  3. 3

    A linear search looks for 9 in the list [4, 7, 9, 2]. Which items are compared?

    Model answer

    The search compares 4, then 7, then 9. The item is found on the third comparison, so 2 is never checked.

  4. 4

    A linear search looks for 6 in the list [4, 7, 9, 2]. How many comparisons are made?

    Model answer

    The search makes four comparisons, one with every item. The value 6 is not in the list, so the search reports that it was not found.

  5. 5

    What is the worst case for a linear search?

    Model answer

    The worst case is when the target is the last item or is not in the list. Every item must then be compared.

  6. 6

    How does a binary search work?

    Model answer

    A binary search compares the target with the middle item of a sorted list. The half that cannot contain the target is discarded. The search repeats on the remaining half until the item is found or none are left.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Algorithms
Programming
Data Representation
Computer Systems

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

The table one extra row.
gainslosescopies

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe algorithm outputs 1.
BThe variable x falls from 5 to 3 to 1.
CThe loop stops because 1 is not greater than 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

ASC means sorting from smallest to largest, or A to Z.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Algorithms, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.