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

Free forever · no card needed

Start free
Intermediate

Searching Algorithms

4.3.4 Searching algorithms

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
5 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Linear Search
  2. 2.Binary Search
  3. 3.Binary Search: Why O(log n)?
  4. 4.Binary Tree Search
  5. 5.Choosing a Search Algorithm
  6. 6.Common Exam Mistakes

Key takeaways

  • Linear search examines each element in turn on unsorted or sorted data, with best case O(1) and worst and average case O(n).
  • Binary search requires the array to be sorted in ascending order and runs in O(log n) because each comparison halves the remaining search space.
  • Binary search uses integer division for the midpoint, `mid ← (low + high) DIV 2`, and updates `low ← mid + 1` or `high ← mid - 1` after each comparison.
  • Binary tree search uses the BST ordering property (left subtree less than the root, right subtree greater) and is O(log n) average but O(n) for a degenerate unbalanced tree.
  • Binary search operates on a sorted array using index arithmetic, while BST search operates on a tree using node pointers; they share average complexity but are different algorithms.

Linear Search

Linear search (sequential search) examines each element in turn until the target is found or the list is exhausted.

FUNCTION linearSearch(arr, target)
    FOR i ← 1 TO LEN(arr)
        IF arr[i] = target THEN
            RETURN i
        ENDIF
    NEXT i
    RETURN -1    // not found
ENDFUNCTION

Worked example — search for 47 in [10, 23, 35, 47, 52, 68]:

StepIndexValueResult
111010 ≠ 47, continue
222323 ≠ 47, continue
333535 ≠ 47, continue
4447found at index 4

Complexity:

  • Best case: — target is the first element
  • Worst case: — target is last or not present (must check every element)
  • Average case:

Key property: works on unsorted data. No preprocessing required.

Binary Search

Binary search eliminates half the remaining search space at each step by comparing the target with the middle element.

Requirement: the array must be sorted in ascending order.

FUNCTION binarySearch(arr, target)
    low ← 1
    high ← LEN(arr)
    WHILE low <= high DO
        mid ← (low + high) DIV 2
        IF arr[mid] = target THEN
            RETURN mid
        ELSE IF arr[mid] < target THEN
            low ← mid + 1
        ELSE
            high ← mid - 1
        ENDIF
    ENDWHILE
    RETURN -1    // not found
ENDFUNCTION

Worked example — search for 52 in sorted array [10, 23, 35, 47, 52, 68, 79, 91] (indices 1–8):

StepLowHighMidarr[mid]Decision
11844747 < 52 → low = 5
25866868 > 52 → high = 5
355552found at index 5

Found in 3 comparisons instead of 5 with linear search.

Complexity: — each comparison halves the search space.

Binary Search: Why O(log n)?

Each step cuts the remaining elements in half. Starting with elements:

StepElements remaining
0
1
2

The search ends when , so . For , at most 10 comparisons are needed.

Comparison:

Array sizeLinear search (worst)Binary search (worst)
100100 comparisons7 comparisons
10001000 comparisons10 comparisons
1,000,0001,000,000 comparisons20 comparisons

Binary search is dramatically faster for large datasets — but only if the data is sorted. Sorting costs ; if you only search once, linear search on unsorted data may be faster overall.

Binary Tree Search

A binary tree search finds a value in a binary search tree (BST) using the BST ordering property:

  • All values in the left subtree are less than the root
  • All values in the right subtree are greater than the root
FUNCTION bstSearch(node, target)
    IF node = NULL THEN
        RETURN NOT_FOUND
    ELSE IF node.value = target THEN
        RETURN node
    ELSE IF target < node.value THEN
        RETURN bstSearch(node.left, target)
    ELSE
        RETURN bstSearch(node.right, target)
    ENDIF
ENDFUNCTION

Worked example — search for 35 in the BST:

  1. Root = 47. 35 < 47 → go left
  2. Node = 23. 35 > 23 → go right
  3. Node = 35. Found!

Complexity:

  • Average case: — balanced BST halves the search space at each node
  • Worst case: — degenerate (unbalanced) tree where all nodes form a single chain

Something not quite clicking?

Ask Aica to explain any part of this differently. Free, takes 30 seconds.

Ask Aica

Choosing a Search Algorithm

AlgorithmData requirementTime complexityWhen to use
Linear searchUnsorted or sortedSmall datasets; data changes frequently; data not sortable
Binary searchMust be sortedLarge sorted arrays; many searches on same dataset
BST searchMust be in a BST avg, worstDynamic datasets with frequent insertions and deletions

Decision guide:

  • Data unsorted and won't be sorted → linear search
  • Data sorted (or worth sorting for many searches) → binary search
  • Data is stored in a BST (or insertion/deletion needed) → BST search

Common Exam Mistakes

1. Applying binary search to unsorted data

Binary search only works on sorted data. Applying it to an unsorted array produces incorrect results without error. Always state the sorted precondition.

2. Mid calculation: integer division required

Mid must be an integer index: mid ← (low + high) DIV 2, not real division. Real division (/) gives 3.5 for indices 1 and 6, which is an invalid array index.

3. Off-by-one errors updating low and high

After a comparison: low ← mid + 1 (not mid) and high ← mid - 1 (not mid). Using mid instead of mid ± 1 creates an infinite loop when arr[mid] is never the target.

4. Claiming BST search is always O(log n)

BST search is only for a balanced tree. In the worst case (inserting elements in sorted order), the BST degenerates into a linked list and search is .

5. Confusing binary search and BST search

Binary search operates on a sorted array using index arithmetic. BST search operates on a tree structure using node pointers. They have the same average complexity but are different algorithms on different data structures.

Key terms

Linear search
A search (also called sequential search) that examines each element in turn until the target is found or the list is exhausted; works on unsorted data.
Binary search
A search that halves the remaining search space at each step by comparing the target with the middle element; requires a sorted array.
Binary tree search
A search that finds a value in a binary search tree by following the BST ordering property down left or right branches.
Binary search tree (BST)
A tree where all values in the left subtree are less than the root and all values in the right subtree are greater.
Best case
The most favourable input scenario, such as O(1) for linear search when the target is the first element.
Worst case
The least favourable input scenario, such as O(n) for linear search when the target is last or not present.
Balanced tree
A BST whose structure halves the search space at each node, giving O(log n) search; an unbalanced (degenerate) tree gives O(n).

Frequently asked questions

Binary search eliminates half the search space each step by comparing the target with the middle element and deciding which half to keep. This decision is only valid if the data is sorted; applied to an unsorted array it produces incorrect results without raising an error.

Each comparison cuts the remaining elements in half: from n to n/2 to n/4 and so on. The search ends when n/2^k = 1, so k = log₂ n. For n = 1000 at most 10 comparisons are needed.

No. BST search is O(log n) only for a balanced tree, where each node halves the search space. In the worst case, such as inserting elements in sorted order, the tree degenerates into a single chain like a linked list and search becomes O(n).

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

Reverse Polish Notation

Next

Sorting Algorithms

Related lessons

8 min

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

1 month ago

6 min

Lesson

Sorting Algorithms

A-Level Computer Science · AQA 7517

1 month ago

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

Start revising free

Free to start. No card needed.