Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Linear Search and Binary Search

3.1.3 Searching algorithms

Aligned to the AQA 8525 specification

Level
Intermediate
Reading time
6 min
Published
2 June 2026
Updated
1 July 2026
On this page
  1. 1.The Purpose of Searching Algorithms
  2. 2.Linear Search: How It Works
  3. 3.Linear Search: Worked Example
  4. 4.Binary Search: The Core Idea
  5. 5.Binary Search: Worked Example
  6. 6.Comparing Linear and Binary Search
  7. 7.Common Exam Mistakes

Key takeaways

  • Linear search checks each element in sequence from the first until the target is found or the list ends; it works on unsorted lists and needs up to n comparisons in the worst case.
  • Binary search requires the list to be sorted in ascending order, then repeatedly halves the search space by comparing the target to the middle element.
  • Binary search needs at most about log2(n) comparisons, so a 1,000-item list takes at most 10 and a 1,000,000-item list at most 20, versus n for linear search.
  • Binary search is not always the better choice: it requires sorted data, so on small or frequently-changing lists the cost of sorting first can exceed a simple linear search.
  • After a non-match in binary search, set low = mid + 1 or high = mid - 1; using mid itself rechecks the midpoint and can cause an infinite loop.

The Purpose of Searching Algorithms

A searching algorithm locates a specific value (the target) within a list and returns its position, or reports that it is absent. Searching is one of the most common operations in computing — looking up a contact, finding a record in a database, or checking whether a username already exists all require a search.

GCSE Computer Science requires two algorithms:

AlgorithmSorted list required?Worst-case comparisons
Linear searchNo (entire list)
Binary searchYes

For a list of 1,000 items: linear search may check all 1,000 elements; binary search needs at most 10 comparisons (since ).

A comparison is each time the algorithm checks whether an element matches the target. Fewer comparisons means a faster algorithm.

Linear Search: How It Works

Linear search checks each element in the list in sequence, starting from the first, until the target is found or the end of the list is reached.

Algorithm in pseudocode:

found ← False
i ← 0
WHILE i < length(list) AND found = False
    IF list[i] = target THEN
        found ← True
        OUTPUT i
    END IF
    i ← i + 1
END WHILE
IF found = False THEN
    OUTPUT "Not found"
END IF

Key properties:

  • Works on unsorted and sorted lists — no preprocessing required
  • Best case: target is the first element → 1 comparison
  • Worst case: target is the last element, or absent → comparisons
  • Average case: approximately comparisons

Linear search's simplicity is its strength: it requires no prior sorting and works on any list, regardless of order or data type.

Linear Search: Worked Example

Find the value 7 in the list [3, 9, 1, 7, 5, 2]:

StepIndex checkedValueMatch?
103No
219No
321No
437Yes

Result: found at index 3, in 4 comparisons.

Find the value 8 (not present) in [3, 9, 1, 7, 5, 2]:

StepIndex checkedValueMatch?
103No
219No
321No
437No
545No
652No

Result: not found, 6 comparisons — the worst case for a 6-element list.

Binary Search: The Core Idea

Binary search repeatedly halves the search space by comparing the target to the middle element of the current range.

Requirement: the list must be sorted in ascending order before binary search is applied. The algorithm assumes that if the target is less than the midpoint, it cannot be in the right half — this assumption is only valid on sorted data.

Logic at each step:

  1. Calculate the midpoint: mid = (low + high) ÷ 2 (round down if not whole)
  2. If list[mid] = target → found; return mid
  3. If target < list[mid] → target is in the left half; set high = mid − 1
  4. If target > list[mid] → target is in the right half; set low = mid + 1
  5. If low > high → target is not in the list

Each step eliminates half the remaining elements. The number of comparisons needed grows with : doubling the list size adds only one extra comparison.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

Binary Search: Worked Example

Find the value 14 in the sorted list [2, 5, 8, 11, 14, 19, 23, 30] (indices 0–7):

Steplowhighmidlist[mid]Decision
10731114 > 11 → search right half
24751914 < 19 → search left half
344414Match — found at index 4

Result: found at index 4, in 3 comparisons.

Find the value 6 (not present) in the same list:

Steplowhighmidlist[mid]Decision
1073116 < 11 → search left half
202156 > 5 → search right half
322286 < 8 → search left half
421low > high → Not found

Comparing Linear and Binary Search

FeatureLinear searchBinary search
Sorted list requiredNoYes
Best case1 comparison1 comparison
Worst case comparisons comparisons
Worst case for 1,000 items1,000≤ 10
Worst case for 1,000,000 items1,000,000≤ 20

Use linear search when:

  • The list is unsorted (or too small to justify sorting first)
  • The list changes frequently, making sorting expensive to maintain
  • Simplicity of implementation matters

Use binary search when:

  • The list is already sorted and large
  • Many repeated searches will be run on the same static dataset

Binary search is dramatically faster on large datasets, but the sorting requirement is a genuine cost. If a list must be sorted specifically to enable binary search, the total cost of (sort + search) may exceed a simple linear search on small or frequently-updated data.

Common Exam Mistakes

1. Applying binary search to an unsorted list

Binary search produces incorrect results on unsorted data. The algorithm assumes elements outside the search range can be discarded — this is only valid when the list is sorted. Always state or check that sorting is required.

2. Rounding the midpoint up instead of down

mid = (low + high) ÷ 2, rounded down (integer division). For low = 0, high = 7: mid = 3.5 → 3, not 4.

3. Setting low = mid or high = mid after a non-match

After eliminating the midpoint, set low = mid + 1 (right half) or high = mid − 1 (left half). Using mid instead of mid ± 1 means the midpoint is rechecked on the next step, which can cause an infinite loop.

4. Stopping a trace before confirming "not found"

When the target is absent, the algorithm must run until low > high — then output "not found." Students often stop at the last comparison without completing the termination check.

5. Claiming binary search is always the better choice

Binary search is faster per search, but it requires sorted data. On a small or frequently modified list, the overhead of sorting may make linear search the more practical option overall.

Key terms

Searching algorithm
A method that locates a specific target value within a list and returns its position, or reports that it is absent.
Target
The specific value a searching algorithm is trying to locate within a list.
Comparison
Each time the algorithm checks whether an element matches the target; fewer comparisons means a faster algorithm.
Linear search
An algorithm that checks each element in the list in sequence from the first until the target is found or the end is reached.
Binary search
An algorithm that repeatedly halves the search space by comparing the target to the middle element of the current range, requiring a sorted list.

Frequently asked questions

Linear search checks each element in order and works on any list, but may need up to n comparisons. Binary search requires a sorted list and repeatedly halves the search space, needing only about log2(n) comparisons, making it far faster on large datasets.

Binary search assumes that if the target is less than the midpoint it cannot be in the right half, so it discards that half. This assumption is only valid when the list is sorted in ascending order; on unsorted data binary search produces incorrect results.

The midpoint is mid = (low + high) divided by 2, rounded down using integer division. For low = 0 and high = 7, mid = 3.5 which rounds down to 3, not 4.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Computational Thinking and Representing Algorithms

Next

Bubble Sort

Related lessons

8 min

Lesson

Merge Sort

GCSE Computer Science · AQA 8525

2 months ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.