Search for a value; report its position
A search checks whether a target value appears in a collection. The target is what we want to find; an index is a position in the collection. In these examples the first position is index 0. Finding value 9 at index 2 means the third item stores 9; it does not mean index 9 was inspected.
The search methods in these examples return a matching index, or -1 when no match exists. That -1 is an explicitly chosen result outside the valid indices, not a rule that every search or language must use. Index 0 is a successful result. Returning an index does not display it unless the caller separately requests output.
Linear search inspects successive items and works even if they are unsorted. Binary search uses the ordering to discard impossible halves. Before using binary search, establish that the values are ordered by the same comparison rule; the search does not sort them automatically.
Linear search: inspect until a match or exhaustion
Worked checkpoint example: the codes are [14, 5, 9, 3, 12] and the target is 9. Keep the list in its supplied order. Start at index 0: 14 is unequal, so advance to 1. Value 5 is unequal, so advance to 2. Value 9 is equal: return index 2 and stop. The final two values are not inspected.
For a separate search for absent target 8 in the same list, all five candidates fail. After index 4, the index advances to 5; index < 5 is now false, so the routine returns -1 without reading an out-of-range item. One mismatch did not prove absence; exhausting the valid positions did.
An initially empty list has no valid index. Testing the bound before reading an element allows the routine to return -1 immediately. In a list with duplicates, this particular left-to-right routine returns the first match because it stops on equality.
| Index inspected | Value | Target 9 | Next action |
|---|---|---|---|
| 0 | 14 | Unequal | Advance to index 1 |
| 1 | 5 | Unequal | Advance to index 2 |
| 2 | 9 | Equal | Return index 2; stop |
Binary search: change a bound, then recalculate the middle
Use this worked locker register: [4, 9, 15, 22, 31, 47, 58, 63, 79]. These are locker numbers in ascending order. low and high are the first and last positions that could still match. Both ends are included: low 0 and high 8 cover nine items.
This example chooses the lower of two possible middle positions. Calculate middle as (low + high) DIV 2, where DIV gives the integer quotient. With low 0 and high 8, middle is 4; the value stored there is 31. Do not confuse the index 4 with the value 31.
Search for 63. Since 63 is greater than 31, index 4 and every lower position are too small; set low to 4 + 1 = 5. Recalculate from low 5 and high 8: middle becomes 6, whose value is 58. The target is greater again, so low becomes 7. With low 7 and high 8, the next middle is 7, value 63. Equality returns index 7 and stops.
Only one boundary changes after a mismatch. The other stays fixed. Adding or subtracting one excludes the already-compared position and ensures the candidate range shrinks. Changing low to the value 58 would confuse data with positions.
| low before comparison | high before comparison | middle index | Value inspected | Action after comparison |
|---|---|---|---|---|
| 0 | 8 | 4 | 31 | Target greater: low becomes 5 |
| 5 | 8 | 6 | 58 | Target greater: low becomes 7 |
| 7 | 8 | 7 | 63 | Equal: return index 7 |
Prove absence when the range becomes empty
Reset the full locker register and search for 34. First compare index 4, value 31: the target is greater, so low becomes 5. The next middle is index 6, value 58: the target is smaller, so high becomes 5. Now low and high are both 5. There is still one candidate, value 47, and it must be checked.
34 is smaller than 47, so high becomes 4 while low stays 5. low > high now means there are no possible indices. The supplied routine returns -1. It does not compute a new middle or read another element after this stop test.
The table records each range before its comparison, and its final row records the stop test. No inspection in the final row means no NEW comparison. An implementation variable may still hold an earlier value until reassigned; do not confuse that retained value with a newly inspected candidate. Read the trace question's recording convention.
| low before comparison | high before comparison | middle index | Value inspected | Action after comparison |
|---|---|---|---|---|
| 0 | 8 | 4 | 31 | Target greater: low becomes 5 |
| 5 | 8 | 6 | 58 | Target smaller: high becomes 5 |
| 5 | 5 | 5 | 47 | Target smaller: high becomes 4 |
| 5 | 4 | Not calculated | No inspection | Range empty: not found |
Continue with your exam board
The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.
Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.