Intermediate

Searching Algorithms

AicademyAicademy
·A-Level Computer Science·AQA 7517·5 min
4.3.4 Searching 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:

         47
        /  \
      23    68
     /  \   /
   10   35 52
  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.

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

7 Slides

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

7 days ago

7 Slides

Lesson

Sorting Algorithms

A-Level Computer Science · AQA 7517

10 hours ago

7 Slides

Lesson

Complexity and Big-O Notation

A-Level Computer Science · AQA 7517

10 hours ago