A searching algorithm is used to find an item in a list or dataset. The two searching algorithms you must know for Edexcel 1CP2 are linear search and binary search.
A linear search (also called a sequential search) works by checking each element of the list one by one from the start until the target item is found or the list is exhausted.
Example: Search for 7 in the list [3, 9, 2, 7, 5, 1]: check 3 (no), 9 (no), 2 (no), 7 (yes! found at index 3).
Best case: The item is the first element — only 1 comparison needed.
Worst case: The item is last or not present — n comparisons (where n is the list size).
Linear search works on unsorted and sorted lists.
A binary search works by repeatedly halving the search space. It requires the list to be sorted in ascending order first.
Example: Search for 7 in [1, 2, 3, 5, 7, 9, 11]:
Midpoint = 5 (index 3). Is 7 > 5? Yes → search right half [7, 9, 11].
Midpoint = 9. Is 7 < 9? Yes → search left half [7].
Midpoint = 7. Match! Found.
Best case: The target is the midpoint — 1 comparison.
Worst case: Logarithmic — about log₂(n) comparisons.
| Feature | Linear Search | Binary Search |
|---|---|---|
| Works on unsorted lists? | ✓ Yes | ✗ No — must be sorted |
| Speed on small lists | Acceptable | Fast |
| Speed on large lists | Slow (checks every item) | Very fast (halves each time) |
| Complexity | Linear — proportional to n | Logarithmic — proportional to log₂n |
| Simplicity | Simpler to implement | More complex |
8 Edexcel-style questions · instantly marked
| Term | Definition |
|---|
Timed exam-style test. No feedback until submission.