CS3: Searching Algorithms
How linear search and binary search work, with step-by-step examples and comparison of when to use each.
How linear search and binary search work, with step-by-step examples and comparison of when to use each.
At GCSE, you need to know two searching algorithms:
FUNCTION linearSearch(list, target)
FOR i โ 0 TO LENGTH(list) - 1
IF list[i] = target THEN
RETURN i
ENDIF
NEXT i
RETURN -1
ENDFUNCTION
Target: 7
| Step | Index | Value | Match? |
|---|---|---|---|
| 1 | 0 | 3 | No โ move on |
| 2 | 1 | 1 | No โ move on |
| 3 | 2 | 7 | Yes! โ Return 2 |
Result: 7 found at index 2 after 3 comparisons.
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
ELSEIF list[mid] < target THEN
low โ mid + 1
ELSE
high โ mid - 1
ENDIF
ENDWHILE
RETURN -1
ENDFUNCTION
Target: 23 Low: 0 High: 9
| Step | Low | High | Mid | list[Mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 16 | 16 < 23 โ search right half โ low = 5 |
| 2 | 5 | 9 | 7 | 56 | 56 > 23 โ search left half โ high = 6 |
| 3 | 5 | 6 | 5 | 23 | 23 = 23 โ Found! Return 5 |
Result: 23 found at index 5 after only 3 comparisons!
| Step | Low | High | Mid | list[Mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 16 | 16 < 50 โ low = 5 |
| 2 | 5 | 9 | 7 | 56 | 56 > 50 โ high = 6 |
| 3 | 5 | 6 | 5 | 23 | 23 < 50 โ low = 6 |
| 4 | 6 | 6 | 6 | 38 | 38 < 50 โ low = 7 |
| 5 | 7 | 6 | - | - | low > high โ not found. Return -1 |
Result: 50 is not in the list. Only 4 comparisons needed to confirm this.
| Feature | Linear Search | Binary Search |
|---|---|---|
| Data requirement | Works on any list (sorted or unsorted) | Requires a sorted list |
| Time complexity | O(n) | O(log n) |
| Best case | 1 comparison | 1 comparison |
| Worst case (n=1000) | 1000 comparisons | 10 comparisons |
| Worst case (n=1,000,000) | 1,000,000 comparisons | 20 comparisons |
| Implementation | Simple - just loop through | More complex - needs low/high pointers |
| Adding items | Easy - just append | Must insert in correct position to maintain sort |
Scenario A: You have an unsorted list of 20 names and need to find one person.
โ Use linear search - the list is unsorted and small.
Scenario B: You have a sorted dictionary of 100,000 words and need to look up many words.
โ Use binary search - the list is large and sorted, and you're searching repeatedly.
| Mistake | Why It's Wrong | How to Fix It |
|---|---|---|
| Using binary search on unsorted data | Binary search relies on the order to eliminate half the list | Sort the list first, or use linear search instead |
| Forgetting to update low/high correctly | Setting low=mid instead of mid+1 can cause infinite loops | Use low โ mid + 1 and high โ mid - 1 |
| Using integer division incorrectly for mid | Floating-point mid values cause errors | Use mid โ (low + high) DIV 2 |
| Stopping too early in binary search | The item might be in the remaining half | Continue until low > high |
Q1: Describe how a linear search works.
Q2: Why must data be sorted before binary search can be used?
Q3: Perform a binary search for the value 72 in the list [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]. Show each step.
Q4: A sorted list contains 256 items. What is the maximum number of comparisons needed by binary search?
Q5: Explain one advantage and one disadvantage of linear search compared to binary search.
โ Thinking binary search works on unsorted data โ Binary search requires the data to be sorted first; it halves the search space by comparing to the middle element, which only works on ordered data.
โ Confusing linear search and binary search time complexities โ Linear search is O(n) โ checks each item in turn. Binary search is O(log n) โ halves the search space each step. Binary search is faster for large sorted datasets.
โ Believing binary search is always better than linear search โ Binary search only works on sorted data and has more complex logic; for small or unsorted datasets, linear search may be simpler and equally effective.
โ Forgetting that linear search works on any data order โ Linear search checks each item sequentially and works on both sorted and unsorted data โ it does not require the data to be in any particular order.
A sorted list contains 1024 student names. Compare the maximum number of comparisons needed by linear search and binary search to find a specific name. [4 marks]
Linear search: In the worst case, it would check all 1024 items, so the maximum number of comparisons is 1024 (O(n)). Binary search: Each comparison halves the search space: 1024 โ 512 โ 256 โ 128 โ 64 โ 32 โ 16 โ 8 โ 4 โ 2 โ 1. This takes a maximum of 10 comparisons (O(logโ n), since 2ยนโฐ = 1024). Binary search is significantly more efficient for this sorted dataset, requiring at most 10 comparisons compared to 1024 for linear search.
AO1 (Computational Thinking โ 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including searching algorithms: linear and binary search for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application โ 40%): Apply knowledge and understanding of computer science, including searching algorithms: linear and binary search to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation โ 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including searching algorithms: linear and binary search, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.