Study Guides
OxfordAQA IGCSE Computer Science (9210): Searching algorithms – Study Guide
Study guide to OxfordAQA IGCSE Computer Science 9210 section 3.1.3: linear search and iterative binary search, pseudocode, traces and a comparison.
- Subject
- Computer Science
- Level
- IGCSE
- Topic
- Topic 1 – Algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA IGCSE Computer Science (9210), For teaching from September 2017, exams from May/June 2019. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA IGCSE Computer Science.
Syllabus points this page covers
9210
- 3.1.3 Searching algorithms
This page covers 1 of the 4 subtopics in topic 1, Algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for IGCSE Computer Science (9210).
This guide covers section 3.1.3 Searching algorithms of OxfordAQA International GCSE Computer Science (9210). You must “understand and explain how the linear search algorithm works” and “how the binary search algorithm works”, and “compare and contrast” the two. The additional information is specific. For linear search you must “know the mechanics of the algorithm and be able to follow and write pseudo-code for it”. For binary search you must know the mechanics of the iterative version and be able to follow and write pseudocode for it. You also need the advantages and disadvantages of both. The wider ideas of efficiency are in the efficiency of algorithms study guide.
What is a searching algorithm?
A searching algorithm finds out whether a particular value, the target, is in a collection of data, and often
where. In both algorithms below the data is held in a one-dimensional array called data, whose indexes start at 0
and finish at LEN(data) - 1. The result is either the position of the target, or a statement that it is
not present.
Linear search
Linear search looks at each item in turn, starting from the first, and compares it with the target. It stops when the item is found, or when it has been through the whole list.
position ← -1
i ← 0
WHILE i < LEN(data) AND position = -1
IF data[i] = target THEN
position ← i
ENDIF
i ← i + 1
ENDWHILE
IF position = -1 THEN
OUTPUT "Not found"
ELSE
OUTPUT "Found at " + INT_TO_STRING(position)
ENDIF
The value -1 is used because it can never be a valid array index, so it can mean “not found yet”. The loop stops
for one of two reasons: the target was found (so position is no longer -1), or i has reached the end of the
list.
Tracing a linear search
Use the sorted list of 15 numbers that is also used for the binary search later:
data = [3, 8, 12, 17, 21, 26, 30, 35, 41, 47, 52, 58, 63, 70, 76]
Searching for the target 47 examines the items from index 0 upwards, and finds it at index 9 after 10 comparisons. Searching for 28, which is not in the list, compares every item, which is 15 comparisons.
The key points about linear search are these:
- It works on unsorted data as well as sorted data.
- It is easy to write and easy to check.
- On a list of
nitems, the best case is 1 comparison (the target is first) and the worst case isncomparisons (the target is last, or not there at all).
Binary search
Binary search is much faster on a large list, but it only works if the list is already sorted. It repeatedly compares the target with the middle item and throws away the half that cannot contain the target.
The iterative version uses three index variables:
low, the index of the first item still being considered;high, the index of the last item still being considered;mid, the index of the middle of the range.
low ← 0
high ← LEN(data) - 1
found ← False
WHILE low ≤ high AND NOT found
mid ← (low + high) DIV 2
IF data[mid] = target THEN
found ← True
ELSE IF data[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
ENDIF
ENDWHILE
IF found THEN
OUTPUT "Found at " + INT_TO_STRING(mid)
ELSE
OUTPUT "Not found"
ENDIF
How each step works:
- Work out the middle index using integer division. If the range has an even number of items,
DIVpicks the lower of the two middle items. - If
data[mid]is the target, stop: it has been found. - If
data[mid]is smaller than the target, the target must be to the right, so every item up to and includingmidcan be discarded:low ← mid + 1. - If
data[mid]is larger, the target must be to the left, sohigh ← mid - 1. - Repeat while there is something left in the range (
low ≤ high). Whenlowbecomes greater thanhigh, the range is empty and the target is not present.
The + 1 and - 1 matter: mid has just been checked, so it must not stay in the range. Writing low ← mid
could make the loop repeat forever.
Trace 1: the target is present
data = [3, 8, 12, 17, 21, 26, 30, 35, 41, 47, 52, 58, 63, 70, 76], target 47.
| Pass | low | high | mid | data[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 14 | 7 | 35 | 35 < 47, so low ← 8 |
| 2 | 8 | 14 | 11 | 58 | 58 > 47, so high ← 10 |
| 3 | 8 | 10 | 9 | 47 | Found at index 9 |
Three comparisons found the item, compared with 10 for the linear search of the same list.
Trace 2: the target is absent
Target 28 in the same list.
| Pass | low | high | mid | data[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 14 | 7 | 35 | 35 > 28, so high ← 6 |
| 2 | 0 | 6 | 3 | 17 | 17 < 28, so low ← 4 |
| 3 | 4 | 6 | 5 | 26 | 26 < 28, so low ← 6 |
| 4 | 6 | 6 | 6 | 30 | 30 > 28, so high ← 5 |
Now low is 6 and high is 5, so low ≤ high is False and the loop ends. The output is “Not found” after 4
comparisons, where the linear search needed 15.
Why the list is halved
After one comparison, about half of the items are gone. After two, about a quarter are left, and so on. For a list of 15 items, at most 4 comparisons are needed. For 1,000 items the maximum is 10, and for 1,000,000 items it is 20. The value is the number of times the list can be halved before nothing is left.
Comparing linear and binary search
| Linear search | Binary search | |
|---|---|---|
| Data must be sorted? | No | Yes |
| Method | Checks items one by one | Compares with the middle, discards half |
| Worst case for 1,000 items | 1,000 comparisons | 10 comparisons |
| Simplicity | Very simple to write | More complex (three indexes, careful updates) |
| Best used when | The list is short, or unsorted, or searched only once | The list is long and already sorted, or searched many times |
Advantages of linear search: it works on unsorted data, it is easy to code and to test, and it needs no preparation. Disadvantages: it is slow on large lists, because the number of comparisons grows in step with the size of the list.
Advantages of binary search: it needs very few comparisons, even for huge lists. Disadvantages: the data must be sorted first; if it is not, the data must be sorted (which takes work), or the search will give wrong answers. It is also harder to write correctly.
A good conclusion is a conditional one: “Binary search is the better choice for a large sorted list. If the list is unsorted, linear search is the right choice unless it will be searched many times, when sorting it once and using binary search may be worth it.”
Worked example: choosing the algorithm
A school keeps a list of 2,000 student numbers in ascending order. The system looks up one student at a time.
Which is better? Binary search, because the data is already sorted and 2,000 items need at most 11 comparisons (2 to the power 11 is 2,048), against up to 2,000 for a linear search.
A teacher types in names of five pupils in the order they arrive, then searches for one of them. Which is better? Linear search, because five items is a very small list, the data is not sorted, and sorting would be more work than simply checking five items.
Writing the pseudocode in an exam
When a question asks you to write a linear or binary search, the marking normally rewards the same features, so check that your answer has them:
- a loop that stops when the item is found or the end is reached (linear), or when found or
low > high(binary); - correct indexing, starting at 0 and finishing at
LEN(data) - 1; midcalculated inside the loop, using integer division;- the correct updates
low ← mid + 1andhigh ← mid - 1; - an output or return that distinguishes “found” from “not found”.
Pseudocode may be in any clear form. If you use a language such as Python in your own lessons, you can still write
the pseudocode with ← and ENDWHILE as shown here.
Common mistakes
- Saying binary search works on any list. It requires the list to be sorted.
- Describing the middle item wrongly, or discarding the wrong half. If the middle item is too small, discard the lower part.
- Forgetting
+ 1or- 1, which can create an endless loop. - Stopping the linear search loop only at the end of the list, so that it carries on after the target is found.
- Claiming that linear search is always worse. It is better for short lists and for unsorted data.
- In a trace table, not recording
midanddata[mid]for each pass. Marks are lost when the working is not shown. - Giving the number of comparisons for the wrong case (best, worst or average) without saying which.
Exam technique
- Trace questions are marked on the sequence of mid values, so show
low,highandmidon each pass. - Explain how questions need the key idea: compare with the middle, then discard half.
- Compare questions need an advantage and a disadvantage of each algorithm, with the sorted condition stated clearly.
- Suggest questions about which search to use need a reason tied to the data (sorted or unsorted, long or short).
For a condensed summary and a self-test, use the Searching algorithms revision notes, and test yourself with the Searching algorithms practice questions.
Related resources
-
Practice questions
OxfordAQA IGCSE Computer Science (9210): Searching algorithms – Practice Questions
Original OxfordAQA IGCSE Computer Science 9210 practice questions with answers on linear search, iterative binary search, traces and comparing the two.
Computer Science · OxfordAQA · IGCSE
Try questions -
Revision notes
OxfordAQA IGCSE Computer Science (9210): Searching algorithms – Revision Notes
Revision notes for OxfordAQA IGCSE Computer Science 9210 section 3.1.3: linear and iterative binary search, pseudocode, a trace and a self-test.
Computer Science · OxfordAQA · IGCSE
Revise topic -
Study guides
OxfordAQA IGCSE Computer Science (9210): Sorting algorithms – Study Guide
Study guide to OxfordAQA IGCSE Computer Science 9210 section 3.1.4: bubble sort with a swap flag, recursive merge sort, worked traces and a comparison.
Computer Science · OxfordAQA · IGCSE
Read guide
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.