levo

Algorithms

Linear and binary search

  • 3 things to know
  • J277/02

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.

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.

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

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.

A bit is the unit of data.
smallestlargestmain

Blank Blitz makes facts automatic

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

AThe sum 0 + 0 is 0, and 0 + 1 is 1.
BThe sum 1 + 1 is 0 carry 1.
CThe sum 1 + 1 + 1 is 1 carry 1.

Build It gets the exam shape right

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

Integer means a whole number with no fractional part.
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.