levo

Algorithms

Binary search

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

Overview

Recall questions on the binary search algorithm and its sorted-data precondition, tracing successful and unsuccessful searches, best and worst-case and space complexity, maximum comparisons, sorting before searching, and moving pointers past the middle.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Describe the binary search algorithm.

Key term

Trace a binary search for 19 in the list 3, 7, 11, 15, 19, 23, 29.

Key term

A sorted list has an even number of items so there is no exact middle. Does this break the algorithm?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Describe the binary search algorithm.

    Model answer

    Set a low pointer to the first position and a high pointer to the last. Find the middle position between them and compare the item there with the target. If it matches, the search succeeds. If the target is smaller, the high pointer moves to just below the middle; if it is larger, the low pointer moves to just above the middle. Repeat on the remaining half. If the low pointer passes the high pointer, the target is not present.

  2. 2

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

    Model answer

    It must be sorted. The algorithm's whole logic is that comparing with the middle item tells you which half the target must be in, and that conclusion only follows if the data is in order. Run on unsorted data it may discard the half that actually holds the target and wrongly report it as absent; it can stumble on the value by luck, but the result cannot be relied on.

  3. 3

    Trace a binary search for 19 in the list 3, 7, 11, 15, 19, 23, 29.

    Model answer

    The pointers span all seven items; the middle item is 15. Since 19 is larger, the search continues in 19, 23, 29, whose middle item is 23. Since 19 is smaller, the search continues in 19 alone, whose middle item is 19 itself, so the search succeeds. Three comparisons in total.

  4. 4

    Trace a binary search for 20 in the list 3, 7, 11, 15, 19, 23, 29, and say how the algorithm knows to stop.

    Model answer

    The middle item is 15, and 20 is larger, so continue in 19, 23, 29. The middle item is 23, and 20 is smaller, so continue in 19 alone. The middle item is 19, and 20 is larger, so the low pointer moves past the high pointer. The section left to search is now empty, which is how the algorithm knows 20 is absent, and it reports failure after three comparisons.

  5. 5

    What is the best case for a binary search?

    Model answer

    O(1), when the target happens to be the middle item of the whole list and is found on the very first comparison.

  6. 6

    Why is a binary search O(log n) in the worst case?

    Model answer

    Because each comparison discards half of the items still under consideration. Starting from n, the number remaining goes n, n over 2, n over 4 and so on down to 1, and the number of halvings needed is log to base 2 of n. Only that many comparisons can ever be made.

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.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram the night before, weakest topic first

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

If it matches, the search .
succeedsfailsrestarts

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.

A global variable means a variable reachable from anywhere in the program.
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.