Worst-Case Analysis of Linear Search

Worst-Case Analysis of Linear Search

Verified Sources
Sep 11, 2026

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:

  1. The target item is found.
  2. Every element has been checked and the target is absent.

For an array containing nn 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 nn elements, giving a worst-case running time of O(n)O(n).2

Footnotes

  1. Linear search - Defines linear search and gives its best, average, and worst comparison counts.

  2. Linear Search Algorithm - Describes sequential checking, O(1)O(1) best case, and O(n)O(n) worst case.

Linear Search: Best, Average, and Worst Cases

Why the Last Element and “Not Found” Are Equivalent

Consider the array:

Index01234
Value12725419

Suppose the algorithm searches from left to right.

  • Searching for 1212 requires 11 comparison.
  • Searching for 2525 requires 33 comparisons.
  • Searching for 1919 requires 55 comparisons.
  • Searching for 3030, which is absent, requires 55 comparisons.

Thus, both “target at the last index” and “target absent” require the maximum possible number of comparisons: nn.2

Worst-case analysis focuses on the input that causes the greatest number of operations, not merely on whether the search succeeds.

Footnotes

  1. Linear search - Defines linear search and gives its best, average, and worst comparison counts.

  2. What Is Linear Search? - Explains why a final-position or absent target requires scanning the entire collection.

Determine the Worst Case

  1. 1
    Step 1

    Standard linear search begins at the first array element and moves toward the last.

  2. 2
    Step 2

    The algorithm performs one comparison for each inspected element.

  3. 3
    Step 3

    If the target is the last element, all nn elements must be inspected before the match is discovered.

  4. 4
    Step 4

    If the target is absent, the algorithm must inspect all nn elements before it can conclude that no match exists.

  5. 5
    Step 5

    Both situations require nn comparisons, so they jointly describe the worst case.

  6. 6
    Step 6

    The correct option is (iv): the item is the last element in the array or is not there at all.

Complexity Comparison

Let nn be the number of elements in the array.

CaseTarget position or conditionComparisonsComplexity
Best caseFirst element11O(1)O(1)
Average caseTypically somewhere in the interiorApproximately (n+1)/2(n+1)/2 for equally likely successful searchesO(n)O(n)
Worst caseLast elementnnO(n)O(n)
Worst caseTarget absentnnO(n)O(n)

The average number of comparisons for a successful search, assuming each position is equally likely, is:

1+2++nn=n+12\frac{1+2+\cdots+n}{n} = \frac{n+1}{2}

Even though the average and worst cases are both classified as O(n)O(n), 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 nn and (n+1)/2(n+1)/2 are classified as O(n)O(n).

Footnotes

  1. Linear search - Defines linear search and gives its best, average, and worst comparison counts.

  2. Linear Search in C - Discusses average-case performance and comparison behavior.

1linearSearch(array, target): 2 for i from 0 to length(array) - 1: 3 if array[i] == target: 4 return i 5 return -1
  1. 1
    Step 1

    Use the array [8,3,11,6,14][8, 3, 11, 6, 14] and search for 1414.

  2. 2
    Step 2

    Compare 88 with 1414. They differ, so continue.

  3. 3
    Step 3

    Compare 33 with 1414. They differ, so continue.

  4. 4
    Step 4

    Compare 1111 with 1414. They differ, so continue.

  5. 5
    Step 5

    Compare 66 with 1414. They differ, so continue.

  6. 6
    Step 6

    Compare 1414 with 1414. The target is found after 55 comparisons.

  7. 7
    Step 7

    Because the array contains n=5n=5 elements, the search used nn 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

1 / 5
Question · Term

What is the correct answer?

Click to reveal
Answer · Definition

Option (iv): the item is the last element in the array or is not there at all.

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 nn elements, the algorithm performs nn comparisons, so the worst-case time complexity is O(n)O(n).2

Footnotes

  1. Linear search - Defines linear search and gives its best, average, and worst comparison counts.

  2. Linear Search Algorithm - Describes sequential checking, O(1)O(1) best case, and O(n)O(n) worst case.

Knowledge Check

Question 1 of 4
Q1Single choice

In which situation does linear search perform the maximum number of comparisons?