Intermediate

Searching Algorithms

AicademyAicademy
·OCR GCSE Computer Science·OCR J277·7 min
2.1.3 Searching and sorting algorithms

Why Searching Algorithms Matter

A searching algorithm finds whether a target value exists in a list, and if so, at which position. OCR J277 requires knowledge of two searching algorithms:

AlgorithmPre-requisiteBest for
Linear searchNone — works on any listShort or unsorted lists
Binary searchList must be sorted firstLong sorted lists

OCR J277 does not require you to write or memorise code for these algorithms. You must be able to apply them step by step to a given data set, and identify them if shown pseudocode or a description.

Choosing the right algorithm matters in practice: searching a database of 1,000,000 records with linear search could require 1,000,000 comparisons. Binary search reduces this to at most 20 comparisons (since ).

Linear Search — How It Works

A linear search works through a list one element at a time, from the first item to the last, comparing each element to the target.

Steps:

  1. Start at index 0.
  2. Compare the current element to the target.
  3. If they match, return the current index — the item is found.
  4. If they do not match, move to the next index.
  5. If the end of the list is reached without a match, return "not found".

(Extra context — OCR J277 does not require you to write or recall pseudocode for linear search; understanding the steps and applying them to a data set is what is assessed.)

PROCEDURE linearSearch(list, target)
    FOR i = 0 TO len(list) - 1
        IF list[i] == target THEN
            RETURN i
        END IF
    END FOR
    RETURN -1
END PROCEDURE

Linear search works on any list — sorted or unsorted. Its simplicity is its main advantage.

(Extra context — time complexity is not required by OCR J277: in the worst case, linear search performs comparisons for a list of items.)

Linear Search — Worked Example

Search for 9 in the list: [4, 7, 2, 9, 1, 5]

StepIndexValue checkedMatch?
104No
217No
322No
439Yes — found at index 3

The search stops as soon as the match is found. In this case, 4 comparisons were needed.

Search for 6 in the same list: [4, 7, 2, 9, 1, 5]

StepIndexValue checkedMatch?
104No
217No
322No
439No
541No
655No
End of listNot found

All 6 elements are checked before returning "not found". This is the worst case for a 6-element list.

Binary Search — Prerequisites and Method

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

Pre-requisite: the list must be sorted (ascending or descending) before binary search can be applied. If the list is unsorted, binary search gives incorrect results.

Steps:

  1. Set low = 0, high = last index.
  2. Calculate mid = (low + high) DIV 2 (integer division — round down).
  3. Compare list[mid] to the target.
    • If equal: item found at mid.
    • If target < list[mid]: target must be in the lower half — set high = mid - 1.
    • If target > list[mid]: target must be in the upper half — set low = mid + 1.
  4. Repeat from step 2 until found, or until low > high (not found).

Binary search halves the remaining search space with each comparison. A sorted list of 1,024 items requires at most 10 comparisons; 1,048,576 items require at most 20.

How much of this have you taken in?

Quiz yourself on this section — free, no card needed.

Test myself

Binary Search — Worked Example

Search for 23 in the sorted list: [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] (indices 0–9)

Passlowhighmidlist[mid]Action
10941623 > 16 → set low = 5
25975623 < 56 → set high = 6
35652323 == 23 → found at index 5

Only 3 comparisons needed to find 23 in a 10-element list.

Search for 10 in the same list:

Passlowhighmidlist[mid]Action
10941610 < 16 → set high = 3
2031510 > 5 → set low = 2
3232810 > 8 → set low = 3
43331210 < 12 → set high = 2
32low > high → not found

When low > high, the search space is empty — the target does not exist in the list.

Comparing Linear and Binary Search

FeatureLinear searchBinary search
Pre-requisiteNoneList must be sorted
Works on unsorted list?YesNo
Maximum comparisons (n items)
Best case1 comparison (first element)1 comparison (target is mid)
Worst case comparisons comparisons
Suitable for small lists?YesYes, but sorting overhead may negate benefit
Suitable for large sorted lists?InefficientMuch more efficient

Example: searching a list of 1,024 sorted items:

  • Linear search: up to 1,024 comparisons
  • Binary search: up to 10 comparisons ()

Use binary search when the list is already sorted and large. Use linear search for unsorted data or very small lists where the overhead of sorting is not worthwhile.

Common Exam Mistakes

1. Applying binary search to an unsorted list

Binary search only works correctly on a sorted list. If the list is not sorted and you apply binary search, you will get the wrong answer. The pre-requisite is frequently tested — always check whether the list is sorted before choosing binary search.

2. Calculating the midpoint incorrectly

Use integer division: mid = (low + high) DIV 2. For low = 5, high = 9: mid = 14 DIV 2 = 7, not 7.5. Always round down.

3. Not updating low/high correctly after the comparison

  • Target > list[mid]low = mid + 1 (not low = mid)
  • Target < list[mid]high = mid - 1 (not high = mid)

Failing to add/subtract 1 can cause an infinite loop.

4. Stopping the trace before checking the "not found" condition

A binary search ends when found or when low > high. Show the final row where low > high to confirm the target is absent — don't just stop at the last comparison.

MistakeCorrection
Using binary search on [7, 2, 9, 1]Sort to [1, 2, 7, 9] first, or use linear search
mid = (5 + 9) / 2 = 7.5mid = (5 + 9) DIV 2 = 7
Setting low = mid after "target > list[mid]"Set low = mid + 1

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

Designing and Refining Algorithms

Next

Sorting Algorithms

Related lessons

7 Slides

Lesson

Designing and Refining Algorithms

OCR GCSE Computer Science · OCR J277

2 days ago

8 Slides

Lesson

Sorting Algorithms

OCR GCSE Computer Science · OCR J277

2 days ago

7 Slides

Lesson

Testing Programs

OCR GCSE Computer Science · OCR J277

2 days ago