StudyVault
Home Unit Overview Exam Technique Revision Techniques ← Previous Lesson Next Lesson →

← Back to Dashboard

Overlay:
0:00 / 0:00 Narration generated using AI text-to-speech.

Searching: Linear and Binary

Why Searching Matters

Searching is one of the most fundamental operations in computing. Every time you look up a contact on your phone, query a database, or ask a search engine a question, a search algorithm is running in the background. Your exam covers two searching algorithms: linear search and binary search. Understanding when to use each one, and being able to trace them by hand, are core exam skills.

Linear Search

Linear search (also called sequential search) is the simplest possible search algorithm. It checks each item in the list from the first to the last, one at a time, until it either finds the target or runs out of items. It works on any list — sorted or unsorted.

Below is a linear search in pseudocode. It searches a list called names for the value stored in target:

SET found = FALSE
SET i = 0
WHILE i < LENGTH(names) AND found = FALSE
  IF names[i] = target THEN
    SET found = TRUE
    OUTPUT “Found at position ” & i
  END IF
  SET i = i + 1
END WHILE
IF found = FALSE THEN
  OUTPUT “Not found”
END IF
Key Fact

Linear search may check every item in the list in the worst case — if the target is last or not present at all. For a list of n items, the worst case is n comparisons. Binary search, by halving the list each time, needs about \(\log_2 n\) comparisons.

List: [3, 7, 12, 5, 9]. Target: 12.

StepIndex checkedValueMatch?
103No
217No
3212Yes — found!

The algorithm stops after three comparisons. If the target were 99 (not in the list), it would check all five items before reporting “Not found.”

Binary Search

Binary search is much faster than linear search on large lists, but it has one critical requirement: the list must be sorted before binary search can be applied. Binary search works by repeatedly halving the search space. It finds the middle item, compares it to the target, and discards the half of the list that cannot contain the target.

Here is the binary search algorithm in pseudocode:

SET low = 0
SET high = LENGTH(list) - 1
SET found = FALSE
WHILE low <= high AND found = FALSE
  SET mid = (low + high) DIV 2
  IF list[mid] = target THEN
    OUTPUT “Found at position ” & mid
    SET found = TRUE
  ELSE IF target < list[mid] THEN
    SET high = mid - 1
  ELSE
    SET low = mid + 1
  END IF
END WHILE
IF found = FALSE THEN
  OUTPUT “Not found”
END IF
Key Fact

Binary search requires the list to be sorted in ascending (or descending) order. Without sorting, comparing the midpoint to the target gives no information about which half of the list to discard, so the halving strategy breaks down entirely.

Sorted list (indices 0–6): [2, 5, 8, 12, 16, 23, 38]. Target: 23.

Passlowhighmidlist[mid]Action
10631223 > 12 → low = 4
246523Match! Found at index 5

Only two comparisons were needed to find the target in a list of seven items. A linear search would have needed six comparisons to reach index 5.

Comparing Linear and Binary Search

The table below summarises the key differences. In exam “compare” questions, you must make explicit contrasts — say “unlike linear search…” rather than describing each in a separate paragraph.

FeatureLinear searchBinary search
List must be sorted?NoYes
Worst-case comparisons (n items)nlog₂n
Best-case comparisons1 (target is first)1 (target is midpoint)
Works on small lists?Yes — often simplerYes, but overhead of sorting may not be worth it
Works on large lists?Slow — may check millions of itemsFast — 30 comparisons suffices for 1 billion items

Choosing Between Linear and Binary Search

The right algorithm depends entirely on two questions: is the list already sorted, and how often will you search it? If the data arrives in arbitrary order and only needs to be searched once, the cost of sorting first is wasted, so a linear search wins. If the same dataset is searched repeatedly, the one-off sorting cost is paid back many times over because every subsequent binary search runs in logarithmic time. Long-lived data structures — phone books, dictionaries, sorted database indexes — are designed around this trade-off.

Binary search also assumes you can jump straight to any position in the data. An array lets you do that, because you can read any index directly. If a question asks why binary search is unsuitable in a scenario, the reason is almost always that the data is not sorted.

Practice Questions

Test yourself on this lesson. Pick a question, write your answer, then get it marked.

AI feedback
AI feedback is guidance, not a final exam mark. If you disagree, your teacher's view is always the tie-breaker.
← Back to unit
Video

© 2026 StudyVault · About · FAQ · Copyright & IP · Privacy · Disclaimers

StudyVault Education Ltd · registered in England and Wales, company no. 17490559 · 71–75 Shelton Street, London WC2H 9JQ