Worst-Case Analysis of Linear Search
The correct answer is (iv) the item is the last element in the array or is not there at all.
Linear search examines elements from the beginning of an array, one at a time. It stops when either:
- The target item is found.
- Every element has been checked and the target is absent.
For an array containing elements, the greatest number of comparisons occurs when the target is at the final position or when it does not appear in the array. In both cases, the algorithm examines all elements, giving a worst-case running time of .2
Footnotes
-
Linear search - Defines linear search and gives its best, average, and worst comparison counts. ↩
-
Linear Search Algorithm - Describes sequential checking, best case, and worst case. ↩
Linear Search: Best, Average, and Worst Cases
Why the Last Element and “Not Found” Are Equivalent
Consider the array:
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 12 | 7 | 25 | 4 | 19 |
Suppose the algorithm searches from left to right.
- Searching for requires comparison.
- Searching for requires comparisons.
- Searching for requires comparisons.
- Searching for , which is absent, requires comparisons.
Thus, both “target at the last index” and “target absent” require the maximum possible number of comparisons: .2
Worst-case analysis focuses on the input that causes the greatest number of operations, not merely on whether the search succeeds.
Footnotes
-
Linear search - Defines linear search and gives its best, average, and worst comparison counts. ↩
-
What Is Linear Search? - Explains why a final-position or absent target requires scanning the entire collection. ↩
Determine the Worst Case
- 1Step 1
Standard linear search begins at the first array element and moves toward the last.
- 2Step 2
The algorithm performs one comparison for each inspected element.
- 3Step 3
If the target is the last element, all elements must be inspected before the match is discovered.
- 4Step 4
If the target is absent, the algorithm must inspect all elements before it can conclude that no match exists.
- 5Step 5
Both situations require comparisons, so they jointly describe the worst case.
- 6Step 6
The correct option is (iv): the item is the last element in the array or is not there at all.
Complexity Comparison
Let be the number of elements in the array.
| Case | Target position or condition | Comparisons | Complexity |
|---|---|---|---|
| Best case | First element | ||
| Average case | Typically somewhere in the interior | Approximately for equally likely successful searches | |
| Worst case | Last element | ||
| Worst case | Target absent |
The average number of comparisons for a successful search, assuming each position is equally likely, is:
Even though the average and worst cases are both classified as , the worst case is specifically determined by the maximum number of comparisons.2
Big-O notation ignores constant factors and lower-order terms. Therefore, both and are classified as .
Footnotes
-
Linear search - Defines linear search and gives its best, average, and worst comparison counts. ↩
-
Linear Search in C - Discusses average-case performance and comparison behavior. ↩
Trace a Worst-Case Search
- 1Step 1
Use the array and search for .
- 2Step 2
Compare with . They differ, so continue.
- 3Step 3
Compare with . They differ, so continue.
- 4Step 4
Compare with . They differ, so continue.
- 5Step 5
Compare with . They differ, so continue.
- 6Step 6
Compare with . The target is found after comparisons.
- 7Step 7
Because the array contains elements, the search used comparisons. This is a worst-case successful search.
Number of Comparisons in a Five-Element Array
The last-element and absent-target cases both require the maximum number of comparisons.
Exam Strategy
When a question asks for the worst case of linear search, look for the situations that force the algorithm to inspect every element: the target is last or the target is absent.
Do Not Choose Only the Last Element
Option (iii) is incomplete. An unsuccessful search also examines every element, so the complete answer is option (iv).
Common Questions and Clarifications
Linear Search Worst-Case Review
Final Answer
(iv) The item is the last element in the array or is not there at all.
Both cases force linear search to inspect every element. For elements, the algorithm performs comparisons, so the worst-case time complexity is .2
Footnotes
-
Linear search - Defines linear search and gives its best, average, and worst comparison counts. ↩
-
Linear Search Algorithm - Describes sequential checking, best case, and worst case. ↩
Knowledge Check
In which situation does linear search perform the maximum number of comparisons?
Explore Related Topics
Merge Sort: Working, Example, and Complexity Analysis
Shortest-Path Algorithms for a City Road Network
A city road network is modeled as a graph, and the appropriate single‑source shortest‑path algorithm depends on the edge‑weight properties.
- All edges equal → use BFS, which finds minimum‑edge paths in time.
- Non‑negative varying weights → use Dijkstra’s algorithm, greedy with a min‑priority queue, running in .
- Some negative weights → use Bellman–Ford, relaxing all edges times () and detecting negative cycles.
- The selection rule:
- A reachable negative cycle means no finite shortest path exists.
Belady's Anomaly and Why LRU and Optimal Page Replacement Avoid It