You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson compares the two standard searching algorithms — linear search and binary search — as required by OCR J277 Section 2.2. The OCR GCSE Computer Science exam frequently asks students to compare these algorithms, so you need to understand the strengths, weaknesses, and appropriate use cases for each.
| Feature | Linear Search | Binary Search |
|---|---|---|
| How it works | Checks each item one by one from the start | Repeatedly divides the sorted data in half |
| Data requirement | Works on sorted or unsorted data | Data must be sorted |
| Best case | O(1) — target is the first item | O(1) — target is the middle item |
| Worst case | O(n) — must check every item | O(log n) — divides the list each time |
| Average case | O(n/2) ≈ O(n) | O(log n) |
| Simplicity | Very simple to code and understand | More complex logic with low, high, mid |
| Memory | Minimal — just a loop counter | Needs variables for low, high, mid |
| Small datasets | Efficient enough; overhead is low | Overhead of sorting may not be worth it |
| Large datasets | Very slow | Very fast |
Consider searching a list of 1,000 items:
| Algorithm | Worst-Case Comparisons |
|---|---|
| Linear search | 1,000 |
| Binary search | ~10 (log₂ 1000 ≈ 10) |
For 1,000,000 items:
| Algorithm | Worst-Case Comparisons |
|---|---|
| Linear search | 1,000,000 |
| Binary search | ~20 (log₂ 1,000,000 ≈ 20) |
The difference is staggering — binary search is approximately 50,000 times faster than linear search for a million items in the worst case.
OCR Exam Tip: When asked to compare the efficiency of two algorithms, always refer to the number of comparisons or steps needed, not just say one is "faster". Use specific numbers where possible. For example: "For 1000 items, linear search may need up to 1000 comparisons whereas binary search needs at most 10."
Linear search is the better choice when:
Binary search is the better choice when:
A key consideration is whether the cost of sorting the data is worth it. If you have unsorted data and need to perform a single search:
In this case, linear search is actually faster overall. However, if you need to search the same data many times:
For large k, the binary search approach wins dramatically.
Question: State one advantage and one disadvantage of binary search compared to linear search. (2 marks)
Answer:
Question: A school stores 2,000 student records sorted alphabetically by surname. A teacher needs to find a student's record. Which search algorithm would be most appropriate? Explain your answer. (3 marks)
Answer: Binary search would be most appropriate because:
OCR Exam Tip: In scenario questions, always link your answer to the specific details given. Do not just state generic advantages — explain why they apply to the given situation.
OCR Exam Tip: A 4-mark comparison question typically expects two points about linear search and two about binary search. Structure your answer clearly, e.g. "Linear search... whereas binary search..." to make the comparison explicit.
For each scenario below, decide which search algorithm is more appropriate and justify your answer in one sentence.
Scenario A: A phone book lists 10,000 entries sorted alphabetically by surname. You want to find "Taylor". Best choice: Binary search. The data is sorted and the list is large; at most 14 comparisons are needed instead of up to 10,000.
Scenario B: A spreadsheet contains 30 pupils' scores in the order they were entered (unsorted). You need to find a score of 85. Best choice: Linear search. The data is unsorted and the list is small, so the overhead of sorting first is not worth it.
Scenario C: A website displays today's live scores (constantly changing) in order of fixture kick-off time. You want to find Chelsea's score. Best choice: Linear search. The data is not sorted by team name and changes frequently, so maintaining a sorted list would be expensive.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.