How do linear search and binary search work, and when is each one appropriate?
Standard searching algorithms: linear search and binary search, how each works step by step, the requirement that binary search needs a sorted list, and the comparison of their efficiency.
An OCR J277 2.1.3 answer on the two standard searching algorithms: how linear search and binary search work step by step, why binary search needs a sorted list, and how their efficiency compares.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
OCR wants you to describe the two standard searching algorithms, linear search and binary search, step by step, to know that binary search needs a sorted list, and to compare their efficiency. You must be able to trace each on a given list and say which is appropriate for a situation. This is examined in Paper 2, often as a description or a comparison.
Linear search
In OCR Exam Reference Language, a linear search of a list data for a target:
found = false
i = 0
while i < data.length AND found == false
if data[i] == target then
found = true
position = i
endif
i = i + 1
endwhile
Binary search
Comparing efficiency
The key comparison is how the work grows as the list gets bigger. Linear search checks items one by one, so doubling the list size roughly doubles the work in the worst case. Binary search halves the remaining items each step, so doubling the list size adds only one extra step. On a list of a million items, linear search may make up to a million comparisons, while binary search makes about twenty. The trade-off is that binary search needs the list to be sorted first, which takes time, so for a small list, an unsorted list, or a one-off search, linear search can be the better choice.
Try this
Q1. State one condition that must be true for a binary search to work. [1 mark]
- Cue. The list must be sorted (in order).
Q2. In the worst case, how many items does a linear search check in a list of 50 items? [1 mark]
- Cue. 50 (it may have to check every item).
Q3. Explain why binary search is faster than linear search on a large sorted list. [2 marks]
- Cue. Binary search discards half the remaining items at each step, so the number of items left shrinks very quickly, whereas linear search checks items one at a time.
Exam-style practice questions
Practice questions written in the style of OCR exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
OCR 20214 marksDescribe how a binary search would find the value 23 in the sorted list 4, 8, 15, 16, 23, 42. State the value examined at each step.Show worked answer →
Binary search repeatedly checks the middle item and discards half the list. The list has 6 items (indexes 0 to 5).
Step 1: the middle is taken (index 2 or the lower middle of 2 and 3). Checking index 2 gives 15. 23 > 15, so discard the left half and search 16, 23, 42 (indexes 3 to 5).
Step 2: the middle of indexes 3 to 5 is index 4, which is 23. This matches, so the search succeeds and returns the position.
Markers reward the method (check the middle, compare, discard the half that cannot contain the target, repeat) and the correct values examined. Stating that the list must be sorted for binary search to work is often credited.
OCR 20226 marksCompare linear search and binary search. Your answer should explain how each works and discuss their relative efficiency and when each is appropriate.Show worked answer →
A 6-mark question, so a structured comparison, not two separate descriptions.
Linear search: checks each item in turn from the start until it finds the target or reaches the end. It works on any list, sorted or not, but is slow on large lists because in the worst case it checks every item.
Binary search: requires a sorted list. It checks the middle item, and because the list is sorted it can discard the half that cannot contain the target, repeating on the remaining half. It is much faster on large lists because each step halves the number of items left.
Comparison: binary search is far more efficient on large sorted lists (it halves the search each time), but it only works if the list is sorted, and sorting first has a cost. Linear search is simpler and works on unsorted or small lists. Markers reward how each works, the sorted-list requirement, and a clear efficiency comparison with a conclusion on when each is appropriate.
Related dot points
- Standard sorting algorithms: bubble sort, insertion sort and merge sort, how each works step by step, and how they compare in approach and efficiency.
An OCR J277 2.1.3 answer on the three standard sorting algorithms: how bubble sort, insertion sort and merge sort each put a list in order step by step, and how they compare in method and efficiency.
- Producing algorithms using pseudocode and flowcharts to solve a problem, identifying the inputs, processes and outputs, and interpreting, correcting and refining algorithms others have written.
An OCR J277 2.1.2 answer on designing algorithms with pseudocode and flowcharts: identifying inputs, processes and outputs, the OCR Exam Reference Language, the standard flowchart symbols, and interpreting, correcting and refining algorithms.
- The principles of computational thinking: abstraction, decomposition and algorithmic thinking, and how each is used to analyse a problem and design a solution.
An OCR J277 2.1.1 answer on the principles of computational thinking: abstraction (removing unnecessary detail), decomposition (breaking a problem into smaller parts) and algorithmic thinking (a clear sequence of steps), with examples of how each is applied to a problem.
- Using trace tables to determine the output of an algorithm and to follow how the values of variables change, and determining the purpose of a simple algorithm.
An OCR J277 2.1.2 answer on using trace tables to follow an algorithm step by step, record how variable values change, find the output, and determine the purpose of a simple algorithm.
- Using one-dimensional and two-dimensional arrays, the use of records to store structured data, and basic SQL (SELECT, FROM, WHERE) to search records in a database.
An OCR J277 2.2.3 answer on storing structured data: one-dimensional and two-dimensional arrays, records, and using basic SQL (SELECT, FROM, WHERE) to search records in a database table.
Sources & how we know this
- OCR GCSE (9-1) Computer Science (J277) specification — OCR (2020)