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 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 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
Test yourself on Algorithms

5 quick GCSE Computer Science questions, marked instantly, so you know what stuck. Free account, no card needed.

Start the free quiz โ†’
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

Computational Thinking Sorting Algorithms

โ† All GCSE Computer Science notes