levo

Algorithms

Linear and binary search

  • 3 things to know
  • J277/02
Start revising · £5/month

Overview

How linear and binary searches work, why a binary search needs sorted data, the midpoint calculation, worked searches through short lists, and the strengths of each.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe how a binary search works.

Key term

How is the middle position calculated in a binary search?

Key term

A binary search looks for 23 in [2, 5, 9, 14, 23, 31, 40]. Which items are compared with 23?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe how a linear search works.

    Model answer

    A linear search checks each item in a list in turn, starting with the first. The search stops when the target is found or every item has been checked.

  2. 2

    What must be true of the data before a binary search can be used?

    Model answer

    The data must already be sorted into order. A linear search has no such requirement and works on unsorted data.

  3. 3

    Describe how a binary search works.

    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.

  4. 4

    How is the middle position calculated in a binary search?

    Model answer

    The middle position is (low + high) DIV 2. The values low and high are the first and last positions still being searched. DIV discards any remainder, so the midpoint rounds down.

  5. 5

    In a binary search, what happens to low and high when the target is greater than the middle item?

    Model answer

    The value of low is set to mid + 1, and high stays the same. The middle item and everything below it are discarded.

  6. 6

    A linear search looks for 15 in [8, 3, 15, 6, 11]. Which items are checked?

    Model answer

    The search checks 8, then 3, then 15. The target is found at index 2 after three comparisons.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Systems Architecture
Memory and Storage
Computer Networks, Connections and Protocols
Network Security

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A bit is the unit of data.
smallestlargestmain

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

Integer means a whole number with no fractional part.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

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