📄 Paper 1 · 4.3 Algorithms
⭐ Pro
4.3.4 Searching Algorithms
AQA 7517 · A-Level Computer Science · ~18 min read

Searching Algorithms

Searching algorithms locate a target value within a data structure. AQA 7517 requires knowledge of linear search and binary search.

Linear (Sequential) Search

Examines each element one by one from the start until the target is found or the end is reached.

FUNCTION linearSearch(arr, target)
  FOR i ← 0 TO len(arr) - 1
    IF arr[i] = target THEN
      RETURN i      // Found at index i
    END IF
  END FOR
  RETURN -1         // Not found
END FUNCTION

Linear Search Analysis

  • Best case: O(1) — target is first element
  • Worst case: O(n) — target is last or not present
  • Average case: O(n)
  • Prerequisite: None — works on unsorted data

Binary Search

Repeatedly halves the search space by comparing the target with the middle element. Requires the array to be sorted.

FUNCTION binarySearch(arr, target)
  left ← 0
  right ← len(arr) - 1
  WHILE left ≤ right
    mid ← (left + right) DIV 2
    IF arr[mid] = target THEN
      RETURN mid
    ELSE IF arr[mid] < target THEN
      left ← mid + 1    // Target in right half
    ELSE
      right ← mid - 1   // Target in left half
    END IF
  END WHILE
  RETURN -1             // Not found
END FUNCTION

Binary Search Trace

// Array: [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
// Target: 23
// Indices: 0  1  2   3   4   5   6   7   8   9

Step 1: left=0, right=9, mid=4, arr[4]=16 < 23 → left=5
Step 2: left=5, right=9, mid=7, arr[7]=56 > 23 → right=6
Step 3: left=5, right=6, mid=5, arr[5]=23 = 23 → FOUND at index 5 ✓

Binary Search Analysis

  • Best case: O(1) — target is middle element
  • Worst case: O(log n) — search space halved each time
  • Prerequisite: Array must be sorted

Comparison

FeatureLinear SearchBinary Search
Time complexity (worst)O(n)O(log n)
Requires sorted dataNoYes
Best forSmall/unsorted datasetsLarge sorted datasets
ImplementationSimpleMore complex
Comparisons (1000 items)Up to 1000Up to 10 (log₂1000≈10)
Exam tip: Know when each algorithm is appropriate. Binary search is much faster (O(log n) vs O(n)) but requires sorted data. Be able to trace binary search step-by-step showing left, right, and mid values. AQA may ask you to state the number of comparisons needed for binary search — remember: log₂(n) comparisons for n items (e.g. 1024 items → 10 comparisons max).
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.3.4 Searching Algorithms

8 questions · instantly marked · AQA 7517 standard

Q1State the worst-case time complexity of linear search and binary search.[2]
✅ Mark scheme
Mark scheme
Linear search: O(n) [1]; Binary search: O(log n) [1].
Q2Why must the data be sorted before binary search can be applied?[2]
✅ Mark scheme
Mark scheme
Binary search works by comparing the target with the middle element and eliminating half the search space [1]; this only works correctly if the data is sorted — an unsorted array would give incorrect decisions about which half to search [1].
Q3Trace binary search on the array [3, 7, 11, 15, 20, 25, 30] searching for 20. Show left, right, mid at each step.[4]
✅ Mark scheme
Mark scheme
Step 1: left=0, right=6, mid=3, arr[3]=15 < 20 → left=4 [1]; Step 2: left=4, right=6, mid=5, arr[5]=25 > 20 → right=4 [1]; Step 3: left=4, right=4, mid=4, arr[4]=20 = 20 → found at index 4 [2].
Q4A sorted array has 1024 elements. What is the maximum number of comparisons needed by binary search to find any element?[2]
✅ Mark scheme
Mark scheme
log₂(1024) = 10 [1]; so a maximum of 10 comparisons [1].
Q5Give one situation where linear search is preferable to binary search.[2]
✅ Mark scheme
Mark scheme
When the data is unsorted (and sorting would be too expensive) [1]; or when the dataset is very small (overhead of binary search not worthwhile) [1].
Q6Write pseudocode for a linear search that returns the index of a target value in an array, or −1 if not found.[3]
✅ Mark scheme
Mark scheme
FUNCTION linearSearch(arr, target) [1]: FOR i ← 0 TO len(arr)−1: IF arr[i] = target THEN RETURN i [1]; END FOR; RETURN −1 [1].
Q7Binary search on an array of 16 elements finds a target in 4 comparisons. Is this consistent with O(log n)? Show your working.[2]
✅ Mark scheme
Mark scheme
log₂(16) = 4 [1]; yes, 4 comparisons is exactly log₂(16) — consistent with O(log n) worst case [1].
Q8A student says "binary search is always better than linear search." Evaluate this claim.[3]
✅ Mark scheme
Mark scheme
Not always — binary search requires sorted data [1]; if data is unsorted, sorting first adds O(n log n) overhead which may not be worthwhile for a single search [1]; for small datasets or single searches on unsorted data, linear search is more appropriate [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Searching

10 questions · 10 minutes

← 4.3.3 Reverse Polish Notation
22 of 70 · AQA 7517
4.3.5 Sorting →