Linear Search
The simplest searching algorithm: check every element one by one until you find what you are looking for - or confirm it is not there. Includes an interactive step-through visualiser and full complexity analysis.
You perform a linear search every time you look for your keys. You check your coat pocket, then your bag, then the kitchen counter - one location at a time, in order, until you find them or run out of places to look. Computers do exactly the same thing when searching an unsorted list. It is not glamorous, but it is reliable, universal and the starting point for understanding all searching algorithms.
Linear search is straightforward: start at index 0 and check each element in turn. If the current element matches the target, return the index and stop. If you reach the end of the list without finding the target, return -1 (or a flag indicating "not found").
The algorithm works on any list, sorted or unsorted. This is its key advantage over binary search, which requires the list to be sorted first.
| Step | Action | Condition |
|---|---|---|
| 1 | Start at index 0 | - |
| 2 | Compare current element to target | If equal, return current index and stop |
| 3 | Move to next element (index + 1) | If we have reached the end of the list, go to step 4 |
| 4 (loop) | Repeat step 2 and 3 | Continue until found or end reached |
| 5 | Return -1 (not found) | Only reached if loop completed without finding target |
Search for 7 in the list [3, 9, 1, 7, 5, 2]:
Index 0: 3 = 7? No. Move on.
Index 1: 9 = 7? No. Move on.
Index 2: 1 = 7? No. Move on.
Index 3: 7 = 7? Yes! Return index 3.
The search took 4 comparisons out of 6 elements.
The pseudocode below implements a linear search as a function/subroutine that takes the list and the target value as parameters and returns the index where the target was found, or -1 if it was not found.
// Linear search - returns index of target or -1 (AQA) SUBROUTINE linearSearch(myList, target) found ← FALSE index ← -1 i ← 0 WHILE i < LEN(myList) AND found = FALSE IF myList[i] = target THEN found ← TRUE index ← i ENDIF i ← i + 1 ENDWHILE RETURN index ENDSUBROUTINE
# Linear search - returns index of target or -1 (OCR) function linearSearch(myList, target) found = false index = -1 i = 0 while i < len(myList) and found == false if myList[i] == target then found = true index = i end if i = i + 1 end while return index endfunction
Exam questions on linear search often ask you to: (1) Trace the algorithm with a given list and target, showing the value of i at each step. (2) State how many comparisons were needed. (3) Write or complete the pseudocode. Know that returning -1 is the conventional way to signal "not found" - returning 0 would be ambiguous (index 0 is a valid position). Also know that the algorithm exits the WHILE loop as soon as found becomes TRUE, so it does not continue checking remaining elements after the target is found.
Trace a linear search for target = 7 in myList = [4, 2, 9, 7, 1, 6]:
| i | myList[i] | myList[i] = 7? | found | index |
|---|---|---|---|---|
| 0 | 4 | No | FALSE | -1 |
| 1 | 2 | No | FALSE | -1 |
| 2 | 9 | No | FALSE | -1 |
| 3 | 7 | Yes | TRUE | 3 |
| Loop condition: found = TRUE, so loop exits. RETURN 3. | ||||
4 comparisons were needed to find the target at index 3. Note: the loop exits immediately when found = TRUE, so elements at indices 4 and 5 are never checked.
Now trace for target = 8 (not in the list):
| i | myList[i] | myList[i] = 8? | found | index |
|---|---|---|---|---|
| 0 | 4 | No | FALSE | -1 |
| 1 | 2 | No | FALSE | -1 |
| 2 | 9 | No | FALSE | -1 |
| 3 | 7 | No | FALSE | -1 |
| 4 | 1 | No | FALSE | -1 |
| 5 | 6 | No | FALSE | -1 |
| i reaches 6 = LEN(myList). Loop exits. RETURN -1 (not found). | ||||
All 6 comparisons were needed to confirm 8 is not in the list. This is the worst case: n comparisons for a list of n items.
Analysing an algorithm's efficiency means asking: how does the number of steps grow as the input size (n) increases? For linear search, this is straightforward.
| Case | Scenario | Comparisons needed | Example (n=100) |
|---|---|---|---|
| Best case | Target is at index 0 (first element) | 1 | 1 comparison |
| Average case | Target is somewhere in the middle | n / 2 | 50 comparisons |
| Worst case | Target is last, or not in the list | n | 100 comparisons |
Use linear search when:
- The list is small (the inefficiency does not matter)
- The list is unsorted (binary search requires a sorted list)
- The list is constantly changing (re-sorting for binary search would be expensive)
- You are searching for all occurrences, not just the first
Avoid linear search when:
- The list is large and sorted - binary search is dramatically faster
A very common exam question: "Compare linear search and binary search. State one advantage of linear search over binary search." The answer: linear search works on unsorted lists; binary search requires the list to be sorted. Also know: "State the worst-case number of comparisons for a linear search on a list of 50 items." Answer: 50.
Enter a target value and press Step to watch the algorithm check each element one by one. Orange = currently checking. Green = found.
Test yourself
1. A linear search is performed on a list of 40 items. The target is the very last element. How many comparisons are made?
2. What does a linear search return if the target is not found in the list?
3. Which statement about linear search is correct?
4. What is the average-case number of comparisons for a linear search on a list of 200 items?
5. A student says "linear search is useless because binary search is always better." Give one reason this is wrong.
A developer needs to search a list of 1,000,000 employee names to check if a given name exists. The names are stored in alphabetical order. They are deciding between linear search and binary search. Calculate the worst-case number of comparisons for each algorithm and explain which they should choose, and why the other might still be used in some circumstances.
Binary search worst case: log2(1,000,000) = approximately 20 comparisons (since 2^20 = 1,048,576).
Choice: Binary search is dramatically more efficient - 20 vs 1,000,000 comparisons in the worst case. Since the list is already sorted, binary search should be used.
When linear might still be used:
- If searching for all employees with a given name (binary search finds one match; linear search finds all)
- If the list is frequently updated with new employees in random positions (maintaining sort order adds overhead)
- If the list is very short and the simplicity of linear search is worth the slight performance cost
Practice what you have learned
Three levelled worksheets. Download, print and complete offline.