markdown
Linear Searchmd 0eb95a9
lecture/information/algorithm/search/linear-search.lecture.n.md
Download PDF

Linear Search

date2026-07-14document_iddoc_6066f99fe87843aebe3be0e2b0b1dbdedescription線形探索の定義、ループ不変条件による正当性証明、最良・最悪計算量を日英両言語で説明する。type講義statusactiverelateddata/lecture/information/algorithm/search/binary-search.lecture.n.md
algorithmsearchlecture

1Introduction

This lecture explains the procedure, correctness, and complexity of linear search, which compares array elements in order from the beginning. For an arbitrary array, when the algorithm uses only equality comparisons and assumes neither sorted order nor an auxiliary lookup structure, a comparison with one element cannot eliminate other elements collectively. Each element must therefore be examined individually.

2Problem statement and terminology

Let

A=(A0,A1,,An-1)

be an array of length n, and let x be a target value. If some index i satisfies Ai=x, the algorithm returns the smallest such index. If no such index exists, it reports failure.

Linear search compares A0,A1,,An-1 with the target in this order and terminates at the first match. The number of comparisons is the number of evaluations of the equality Ai=x.

3Procedure

For i=0,1,,n-1 in order, perform the following steps.

  1. Evaluate Ai=x.
  2. If the equality holds, return i and terminate.
  3. Otherwise, proceed to the next index.

If no index produces a match, report failure. This procedure does not depend on whether the array is sorted.

4Correctness by a loop invariant

A loop invariantLoop invariant is a proposition that holds at a designated point of every iteration. Immediately before the comparison at index i, use the invariant

[PARSE ERROR: Undefined("Command(\"boxed\")")]A0,A1,,Ai-1arealldifferentfromx.

4.1Initialization

Immediately before the first comparison, i=0. No element has been compared, so the invariant holds over the empty set of preceding indices.

4.2Maintenance

Assume that the invariant holds immediately before the comparison at index i. If Ai=x, no smaller index contains a match, so i is the smallest index satisfying the specification. If Aix, none of A0,,Ai equals x. The invariant therefore holds immediately before the comparison at index i+1.

4.3Termination

If the algorithm terminates at a match, the maintenance argument shows that the returned index is the first matching position. If it reaches i=n without a match, the invariant shows that none of A0,,An-1 equals x. Reporting failure is therefore correct.

Initialization, maintenance, and termination together prove that linear search satisfies its specification.

5Complexity

Assume n>0. In the best case, the target equals the first element A0. The algorithm performs one comparison, so its best-case running time is Θ(1).

If the target occurs only at the final element or does not occur in the array, the algorithm performs n comparisons. Its worst-case running time is therefore Θ(n). Apart from its index and return value, it uses no storage proportional to the input length, so its auxiliary-space complexity is Θ(1).

When n=0, the algorithm performs no comparisons and reports failure immediately.

6Applicability and connection to binary search

Linear search requires only sequential access to elements and equality comparison; it does not require sorted order. In a sorted array, however, order information can eliminate multiple candidates at once. The next lecture explains binary search, which uses this property.

data/lecture/information/algorithm/search/binary-search.lecture.n.md

7Summary

  • Linear search compares array elements with the target in order from the beginning.
  • Initialization, maintenance, and termination of the loop invariant prove that the algorithm correctly returns the first matching position or reports failure.
  • Its best-case running time is Θ(1), its worst-case running time is Θ(n), and its auxiliary-space complexity is Θ(1).
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる