markdown
線形探索md 0eb95a9
lecture/information/algorithm/search/linear-search.lecture.n.md
Download PDF

線形探索せんけいたんさく

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

1導入どうにゅう

この講義こうぎでは、配列はいれつ先頭せんとうからじゅん比較ひかくする線形探索せんけいたんさくについて、処理手順しょりてじゅん正当性せいとうせい計算量けいさんりょう説明せつめいする。任意にんい配列はいれつたいして等値比較とうちひかくだけを使用しようし、整列せいれつ補助的ほじょてき検索構造けんさくこうぞう仮定かていしない場合ばあい、ある要素ようそとの比較結果ひかくけっかからほか要素ようそ一括いっかつして除外じょがいすることはできない。このため、各要素かくようそ個別こべつ検査けんさする方法ほうほう基本きほんとなる。

2問題設定もんだいせってい用語ようご

ながn配列はいれつ

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

目標値もくひょうち x入力にゅうりょくとする。添字そえじ iAi=xたすなら、そのような添字そえじのうち最小さいしょうのものをかえす。該当がいとうする添字そえじ存在そんざいしないなら、探索失敗たんさくしっぱいかえす。

線形探索せんけいたんさくLinear searchとは、A0,A1,,An-1 をこの順序じゅんじょ目標値もくひょうち比較ひかくし、最初さいしょ一致いっち終了しゅうりょうする探索法たんさくほうである。比較回数ひかくかいすうNumber of comparisonsとは、等式とうしき Ai=x判定はんていした回数かいすうである。

3処理手順しょりてじゅん

i=0,1,,n-1じゅんに、つぎ処理しょり実行じっこうする。

  1. Ai=x判定はんていする。
  2. 等式とうしき成立せいりつするなら iかえして終了しゅうりょうする。
  3. 等式とうしき成立せいりつしないなら、つぎ添字そえじ移行いこうする。

すべての添字そえじにおいて一致いっちしなければ、探索失敗たんさくしっぱいかえす。この手順てじゅん配列はいれつ整列状態せいれつじょうたい依存いぞんしない。

4不変条件ふへんじょうけんによる正当性せいとうせい

ループ不変条件ふへんじょうけんLoop invariantとは、反復処理はんぷくしょり所定しょてい時点じてんつね成立せいりつする命題めいだいである。添字そえじ i比較直前ひかくちょくぜんに、つぎ不変条件ふへんじょうけん設定せっていする。

不変条件ふへんじょうけんは「A0,A1,,Ai-1 のいずれも x一致いっちしない」である。

4.1初期化しょきか

最初さいしょ比較直前ひかくちょくぜんでは i=0 である。すで比較ひかくした要素ようそ存在そんざいしないため、くう添字集合そえじしゅうごうたいして不変条件ふへんじょうけん成立せいりつする。

4.2保存ほぞん

添字そえじ i比較直前ひかくちょくぜん不変条件ふへんじょうけん成立せいりつすると仮定かていする。Ai=x なら、i よりちいさい添字そえじでは一致いっちしないため、i条件じょうけんたす最小さいしょう添字そえじである。Aix なら、既検査部分きけんさぶぶん A0,,Ai のいずれも x一致いっちしない。したがって、i+1比較直前ひかくちょくぜんにも不変条件ふへんじょうけん成立せいりつする。

4.3終了しゅうりょう

一致いっちによって終了しゅうりょうする場合ばあい保存ほぞん議論ぎろんにより、返却へんきゃくする添字そえじ最初さいしょ一致位置いっちいちである。一致いっちせずに i=n到達とうたつする場合ばあい不変条件ふへんじょうけんから A0,,An-1 のいずれも x一致いっちしない。ゆえに、探索失敗たんさくしっぱいという返却結果へんきゃくけっかただしい。

初期化しょきか保存ほぞん終了しゅうりょう三段階さんだんかいにより、線形探索せんけいたんさく仕様しようたす。

5計算量けいさんりょう

n>0 とする。目標値もくひょうち先頭要素せんとうようそ A0一致いっちする最良さいりょう場合ばあい比較回数ひかくかいすう1 であり、時間計算量じかんけいさんりょうΘ(1) である。

目標値もくひょうち末尾要素まつびようそとのみ一致いっちする場合ばあい、または配列内はいれつない存在そんざいしない場合ばあい比較回数ひかくかいすうn である。したがって、最悪時間計算量さいあくじかんけいさんりょうΘ(n) である。ループの添字そえじ返却値へんきゃくちのぞいて入力長にゅうりょくちょう比例ひれいする記憶領域きおくりょういき使用しようしないため、補助空間計算量ほじょくうかんけいさんりょうΘ(1) である。

n=0場合ばあい比較ひかく実行じっこうせず、ただちに探索失敗たんさくしっぱいかえす。

6適用範囲てきようはんい二分探索にぶんたんさくへの接続せつぞく

線形探索せんけいたんさくは、要素ようそ順次取得じゅんじしゅとくして等値比較とうちひかくできれば適用てきようでき、整列せいれつ必要ひつようとしない。一方いっぽう整列済せいれつず配列はいれつでは、順序情報じゅんじょじょうほうにより複数ふくすう候補こうほ一括いっかつして除外じょがいできる。つぎ講義こうぎでは、この性質せいしつ利用りようする二分探索にぶんたんさく説明せつめいする。

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

7まとめ

  • 線形探索せんけいたんさくは、配列要素はいれつようそ先頭せんとうからじゅん目標値もくひょうち比較ひかくする。
  • ループ不変条件ふへんじょうけん初期化しょきか保存ほぞん終了しゅうりょうにより、最初さいしょ一致位置いっちいちまたは探索失敗たんさくしっぱいただしくかえすことが証明しょうめいされる。
  • 最良時間計算量さいりょうじかんけいさんりょうΘ(1)最悪時間計算量さいあくじかんけいさんりょうΘ(n)補助空間計算量ほじょくうかんけいさんりょうΘ(1) である。

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

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
タブを全て閉じる