markdown
二分探索md 9dcb972
lecture/information/algorithm/search/binary-search.lecture.n.md
Download PDF

二分探索にぶんたんさく

date2026-07-14document_iddoc_0eab994965f14f286340001af35899a7description昇順に整列された配列に対する二分探索を、半開区間の不変条件、停止性、計算量から厳密に説明する。type講義statusactiverelateddata/lecture/information/algorithm/search/linear-search.lecture.n.md
algorithmsearchlecture
data/lecture/information/algorithm/search/linear-search.lecture.n.md

1導入どうにゅう

この講義こうぎでは、昇順しょうじゅん整列せいれつされた配列はいれつたいする二分探索にぶんたんさくを、探索区間たんさくくかん不変条件ふへんじょうけんから説明せつめいする。線形探索せんけいたんさく各要素かくようそ順次比較じゅんじひかくするが、二分探索にぶんたんさく整列順序せいれつじゅんじょ利用りようして、一回いっかい比較ひかく候補区間こうほくかん一方いっぽう除外じょがいする。

2問題設定もんだいせってい

n0 要素ようそ配列はいれつ A=(A0,A1,,An-1)

A0A1An-1

たす、すなわち昇順しょうじゅん整列せいれつされているとする。目標値もくひょうち xたいして、Ai=xたす添字そえじ i存在そんざいすればそのひとつをかえし、存在そんざいしなければ不在ふざいかえす。

重複要素ちょうふくようそ許容きょようするため、x複数ふくすう位置いち存在そんざいする場合ばあい、この算法さんぽうはそのうちいずれかひとつの添字そえじかえす。最初さいしょまたは最後さいご出現位置しゅつげんいちもとめる仕様しようではない。

3半開区間はんかいくかん中央ちゅうおう

候補こうほとなる添字そえじ半開区間はんかいくかん [l,r)あらわす。この区間くかんlふくみ、rふくまず、区間長くかんちょうr-l である。l<r のとき、中央添字ちゅうおうそえじ

m=l+r2

とする。このとき lm<r成立せいりつするため、Am現在げんざい候補区間内こうほくかんない存在そんざいする。

4不変条件ふへんじょうけん

反復はんぷく開始時点かいしじてんで、つぎ条件じょうけん維持いじする。

x配列はいれつ存在そんざいするなら、Ai=xたす添字そえじ[l,r)存在そんざいする。

4.1初期化しょきか

初期値しょきちl=0r=n とする。候補区間こうほくかん [0,n)配列全体はいれつぜんたいふくむため、不変条件ふへんじょうけん成立せいりつする。

4.2保存ほぞん

l<r のもとで中央ちゅうおう m計算けいさんし、Amx比較ひかくする。

  • Am=x なら、mかえして終了しゅうりょうする。
  • x<Am なら、昇順性しょうじゅんせいにより、すべての i[m,r) について AiAm>x である。したがって [m,r)一致位置いっちいちはなく、r=m更新こうしんできる。
  • Am<x なら、昇順性しょうじゅんせいにより、すべての i[l,m+1) について AiAm<x である。したがって [l,m+1)一致位置いっちいちはなく、l=m+1更新こうしんできる。

いずれの更新こうしんでも、一致位置いっちいちとなり添字そえじだけがあらたな候補区間こうほくかんのこるため、不変条件ふへんじょうけん保存ほぞんされる。

4.3終了しゅうりょう

l=r では候補区間こうほくかんくうである。不変条件ふへんじょうけんにより、x存在そんざいするなら一致位置いっちいちがこの空区間くうくかん存在そんざいするはずであり、これは不可能ふかのうである。したがって x配列はいれつ存在そんざいせず、不在ふざいかえしてよい。

5停止性ていしせい

更新前こうしんまえ区間長くかんちょうL=r-l>0 とする。r=m場合ばあいあらたなながさは m-ll=m+1場合ばあいr-m-1 である。lm<r から、いずれも 0L<Lたす。

したがって、非負整数ひふせいすうである区間長くかんちょう一致いっちによって終了しゅうりょうしない各反復かくはんぷく厳密げんみつ減少げんしょうする。このため反復はんぷく有限回ゆうげんかい終了しゅうりょうする。

6計算量けいさんりょう

一回いっかい更新後こうしんご区間長くかんちょう L

LL2

たす。n=0 なら反復はんぷくおこなわれない。n1 では、k かい更新後こうしんご区間長くかんちょう高々たかだか n/2k となる。2k>nたせば区間くかんくうになるため、反復回数はんぷくかいすう高々たかだか log2n+1 かいである。よって時間計算量じかんけいさんりょうO(log(n+1))追加領域ついかりょういきO(1) である。

7算法さんぽう要約ようやく

  1. [l,r)=[0,n) とする。
  2. l<rあいだm=(l+r)/2計算けいさんする。
  3. Am=x なら mかえす。x<Am なら r=mAm<x なら l=m+1 とする。
  4. l=r なら不在ふざいかえす。

二分探索にぶんたんさく正当性せいとうせいは、昇順性しょうじゅんせいによる候補こうほ除外じょがい不変条件ふへんじょうけん保存ほぞん、および区間長くかんちょう厳密減少げんみつげんしょうからみちびかれる。

Binary Search

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

1Introduction

This lecture explains binary search on an array sorted in nondecreasing order by means of a search-interval invariant. Linear search compares elements in sequence. Binary search instead uses the sorted order to exclude one part of the candidate interval with each comparison.

2Problem specification

Let A=(A0,A1,,An-1) be an array of n0 elements satisfying

A0A1An-1.

Thus, the array is sorted in nondecreasing order. Given a target x, the algorithm returns an index i satisfying Ai=x if such an index exists, and otherwise reports absence.

Duplicate elements are allowed. If x occurs at multiple indices, this algorithm may return any one of them; it does not promise the first or last occurrence.

3Half-open interval and midpoint

Represent the candidate indices by the half-open interval [l,r). It contains l but not r, and its length is r-l. When l<r, define

m=l+r2.

Then lm<r, so Am lies in the current candidate interval.

4Invariant

At the start of every iteration, maintain the following condition:

If x occurs in the array, an index i satisfying Ai=x occurs in [l,r).

4.1Initialization

Set l=0 and r=n. The candidate interval [0,n) contains the entire array, so the invariant holds.

4.2Preservation

Under l<r, compute the midpoint m and compare Am with x.

  • If Am=x, return m and terminate.
  • If x<Am, sortedness gives AiAm>x for every i[m,r). Hence no matching index lies in [m,r), and the update r=m is valid.
  • If Am<x, sortedness gives AiAm<x for every i[l,m+1). Hence no matching index lies in [l,m+1), and the update l=m+1 is valid.

In either update, every index that can still match remains in the new candidate interval. The invariant is therefore preserved.

4.3Termination condition

When l=r, the candidate interval is empty. If x occurred in the array, the invariant would require a matching index in this empty interval, which is impossible. Thus x is absent, and the algorithm may report absence.

5Termination

Let L=r-l>0 be the interval length before an update. After r=m, the new length is m-l; after l=m+1, it is r-m-1. Since lm<r, both cases satisfy 0L<L.

Consequently, the nonnegative integer interval length decreases strictly at every iteration that does not terminate with a match. The algorithm must therefore terminate after finitely many iterations.

6Complexity

After one update, the new interval length satisfies

LL2.

If n=0, the loop performs no iterations. If n1, then after k updates its length is at most n/2k. Once 2k>n, the interval is empty, so the algorithm performs at most log2n+1 iterations. Its time complexity is O(log(n+1)), and its auxiliary-space complexity is O(1).

7Algorithm summary

  1. Set [l,r)=[0,n).
  2. While l<r, compute m=(l+r)/2.
  3. If Am=x, return m. If x<Am, set r=m; if Am<x, set l=m+1.
  4. If l=r, report absence.

The correctness of binary search follows from candidate exclusion justified by sortedness, preservation of the invariant, and strict decrease of the interval length.

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