You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Searching algorithms are used to find a specific item within a data set. In GCSE Computer Science, you must understand two key searching algorithms: linear search and binary search. You need to know how each one works, be able to trace through them, and compare their efficiency.
A linear search (also called a sequential search) works by checking each item in the list one at a time, starting from the first item and moving through to the last. It continues until the item is found or the end of the list is reached.
FUNCTION linearSearch(list, target)
FOR i ← 0 TO LENGTH(list) - 1
IF list[i] = target THEN
RETURN i
END IF
END FOR
RETURN -1 // Item not found
END FUNCTION
List: [4, 7, 2, 9, 1, 5] Target: 9
| Step | i | list[i] | list[i] = target? | Action |
|---|---|---|---|---|
| 1 | 0 | 4 | NO | Continue |
| 2 | 1 | 7 | NO | Continue |
| 3 | 2 | 2 | NO | Continue |
| 4 | 3 | 9 | YES | Return 3 |
Result: Found at index 3
Exam Tip: Linear search is the only option if the data is unsorted. If asked when you would use a linear search, mention that the data does not need to be in any particular order.
A binary search works by repeatedly dividing the search space in half. It starts by checking the middle item of a sorted list. If the target is smaller, it searches the left half; if larger, it searches the right half. This process repeats until the item is found or the search space is empty.
Binary search only works on sorted data. If the data is not sorted, you must sort it first (which takes time) or use a linear search instead.
FUNCTION binarySearch(list, target)
low ← 0
high ← LENGTH(list) - 1
WHILE low <= high
mid ← (low + high) DIV 2
IF list[mid] = target THEN
RETURN mid
ELSE IF target < list[mid] THEN
high ← mid - 1
ELSE
low ← mid + 1
END IF
END WHILE
RETURN -1 // Item not found
END FUNCTION
Sorted list: [1, 3, 5, 7, 9, 11, 13, 15] Target: 11
| Step | low | high | mid | list[mid] | Comparison | Action |
|---|---|---|---|---|---|---|
| 1 | 0 | 7 | 3 | 7 | 11 > 7 | low = 4 |
| 2 | 4 | 7 | 5 | 11 | 11 = 11 | Found! |
Result: Found at index 5 (only 2 comparisons needed)
With a linear search, this would have taken 6 comparisons.
| Feature | Linear Search | Binary Search |
|---|---|---|
| Data must be sorted? | No | Yes |
| Best case | 1 comparison | 1 comparison |
| Worst case | n comparisons | log₂(n) comparisons |
| Average case | n/2 comparisons | log₂(n) comparisons |
| Simplicity | Very simple | More complex |
| Efficiency on large datasets | Slow | Very fast |
This demonstrates why binary search is vastly more efficient for large sorted datasets.
Exam Tip: A common exam question asks you to compare linear and binary search. Always mention: (1) binary search requires sorted data, (2) binary search is more efficient for large datasets, (3) linear search is simpler and works on unsorted data. If asked to "state one advantage of linear search," say it does not require the data to be sorted.
| Use Linear Search When... | Use Binary Search When... |
|---|---|
| The data is unsorted | The data is sorted |
| The dataset is small | The dataset is large |
| You need a simple solution | You need an efficient solution |
| The data changes frequently (constant re-sorting is expensive) | The data is static or already sorted |
Linear search checks each item one by one and works on any list, but is slow for large datasets. Binary search repeatedly halves the search space and is much faster, but requires sorted data. You must be able to write pseudocode for both algorithms, trace through them with example data, and compare their performance.
Searching is one of the cleanest illustrations of computational thinking in action. Both linear and binary search are built from the same four-pillar reasoning: decompose the search into "check one element" + "decide where to look next", abstract the data as an array, recognise the pattern of repeated comparison, and express the logic as an algorithm in pseudocode. This section develops that viewpoint with two worked examples and grade-banded model answers.
Any search algorithm can be decomposed into three sub-problems:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.