markdown
Binary Searchmd 9dcb972
lecture/information/algorithm/search/binary-search.lecture.n.md
Download PDF

Binary Search

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

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