線形探索
1導入
この
2問題設定 と用語
と
3処理手順
の
- を
判定 する。 等式 が成立 するなら を返 して終了 する。等式 が成立 しないなら、次 の添字 へ移行 する。
すべての
4不変条件 による正当性
4.1初期化
4.2保存
4.3終了
5計算量
とする。
の
6適用範囲 と二分探索 への接続
7まとめ
線形探索 は、配列要素 を先頭 から順 に目標値 と比較 する。- ループ
不変条件 の初期化 、保存 、終了 により、最初 の一致位置 または探索失敗 を正 しく返 すことが証明 される。 最良時間計算量 は 、最悪時間計算量 は 、補助空間計算量 は である。
Linear Search
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
be an array of length , and let be a target value. If some index satisfies , the algorithm returns the smallest such index. If no such index exists, it reports failure.
Linear search compares with the target in this order and terminates at the first match. The number of comparisons is the number of evaluations of the equality .
3Procedure
For in order, perform the following steps.
- Evaluate .
- If the equality holds, return and terminate.
- 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
4.1Initialization
Immediately before the first comparison, . 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 . If , no smaller index contains a match, so is the smallest index satisfying the specification. If , none of equals . The invariant therefore holds immediately before the comparison at index .
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 without a match, the invariant shows that none of equals . Reporting failure is therefore correct.
Initialization, maintenance, and termination together prove that linear search satisfies its specification.
5Complexity
Assume . In the best case, the target equals the first element . The algorithm performs one comparison, so its best-case running time is .
If the target occurs only at the final element or does not occur in the array, the algorithm performs comparisons. Its worst-case running time is therefore . Apart from its index and return value, it uses no storage proportional to the input length, so its auxiliary-space complexity is .
When , 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.md7Summary
- 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 , its worst-case running time is , and its auxiliary-space complexity is .