Searching Algorithms

GCSE Computer Science · Algorithms

Searching Algorithms

Searching algorithms find a specific item within a data structure. You need to know linear search and binary search for the exam.

---

Linear Search

Linear search checks each element in turn from the start until the target is found or the list ends.

Pseudocode

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

Key Properties

PropertyDetail
Time complexity (best)O(1) — target is the first element
Time complexity (worst)O(n) — target is last or absent
Time complexity (average)O(n)
Data requirementWorks on any list (sorted or unsorted)
Space complexityO(1) — no extra storage needed

How It Works (Walkthrough)

Given the list [5, 3, 8, 1, 9], searching for 8:

1. Compare 5 with 8 — no match

2. Compare 3 with 8 — no match

3. Compare 8 with 8 — match found at index 2

---

Binary Search

Binary search repeatedly halves the search space by comparing the target with the middle element. The list must be sorted first.

Pseudocode

FUNCTION binarySearch(list, target)
    low = 0
    high = LEN(list) - 1
    WHILE low <= high
        mid = (low + high) DIV 2
        IF list[mid] == target THEN
            RETURN mid
        ELSE IF list[mid] < target THEN
            low = mid + 1
        ELSE
            high = mid - 1
        END IF
    END WHILE
    RETURN -1
END FUNCTION

Key Properties

PropertyDetail
Time complexity (best)O(1) — target is the middle element
Time complexity (worst)O(log n) — halves each step
Time complexity (average)O(log n)
Data requirementList must be sorted
Space complexityO(1) iterative, O(log n) recursive

How It Works (Walkthrough)

Given the sorted list [1, 3, 5, 8, 9, 12, 15], searching for 9:

1. low=0, high=6, mid=3 → list[3]=8, 8 < 9 → low=4

2. low=4, high=6, mid=5 → list[5]=12, 12 > 9 → high=4

3. low=4, high=4, mid=4 → list[4]=9 — match found at index 4

---

Big O Notation

Big O notation describes how the time or space an algorithm uses grows as the input size (n) increases. It measures the worst-case scenario.

NotationNameExample
O(1)ConstantAccessing an array element by index
O(log n)LogarithmicBinary search
O(n)LinearLinear search
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort

Efficiency ranking (best to worst): O(1) → O(log n) → O(n) → O(n log n) → O(n²)

---

Comparison Table

FeatureLinear SearchBinary Search
Sorted data needed?NoYes
Best caseO(1)O(1)
Worst caseO(n)O(log n)
Speed on large dataSlowFast
ImplementationSimpleMore complex
When to useSmall or unsorted listsLarge sorted lists

---

Exam Tips

  • If asked to trace an algorithm, show each step clearly in a table with the values of all variables
  • Binary search is much faster on large datasets but requires sorting first — the sort itself costs time
  • Always state that binary search needs a sorted list — this is a commonly tested point
  • When comparing algorithms, discuss time complexity, data requirements, and suitability for the scenario given
Don't understand a part?

Sign in and ask our AI tutor to explain any passage in plain English.

Try AI explanations →

More on Algorithms

Searching Algorithms Sorting Algorithms Computational Thinking Sorting Algorithms

← All GCSE Computer Science notes