| English | 意味 |
|---|---|
| linear search/ˈlɪnɪə sɜːtʃ/ | 線形探索 |
| binary search/ˈbaɪnəri sɜːtʃ/ | 二分探索 |
Twenty questions for a million names
- A phone book holds a million names. Checking them one at a time, you would expect half a million comparisons before finding the one you want.
- Open it in the middle instead, decide which half the name is in, and throw the other half away. Repeat. You reach any name in twenty comparisons.
- Half a million against twenty is not a small saving; it is the difference between a program that works and one that cannot be used. And it costs one thing: the list must already be in order.
- This lesson is linear search 线性查找 and binary search 二分查找, how each performs, and how to choose.
Linear search
FOR i ← 1 TO n
IF A[i] = target THEN
RETURN i
ENDIF
NEXT i
RETURN -1 // not found
- It walks from the start, comparing each element with the target, and stops when it finds a match or reaches the end.
- It works on any list, sorted or not, and on any structure that can be stepped through.
- Worst case: the target is last or absent, so all $n$ elements are compared, which is $O(n)$. On average, about half.

One at a time, from the beginning
線形探索とは:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
線形探索は事前準備が不要で、いかなるリストでも動作し、最悪のケースでは O(n) です。
データが次のような場合に線形探索がより適しています:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
利用可能な順序がなく(あるいはリストが極めて小さい場合)、線形探索はまずソートするコストを回避します。
Binary search
low ← 1 ; high ← n
WHILE low <= high DO
mid ← (low + high) DIV 2
IF A[mid] = target THEN
RETURN mid
ENDIF
IF A[mid] < target THEN
low ← mid + 1
ELSE high ← mid - 1
ENDWHILE
RETURN -1
- It requires the data to be sorted. Compare the middle element with the target: if it matches, stop; if the target is larger, discard the lower half; otherwise discard the upper half.
- Each comparison halves the range still to be searched, so the number of comparisons is $O(\log_2 n)$.
- That is why a million items need about twenty comparisons: $2^{20}$ is just over a million.
探索アルゴリズム
バイナリ探索は各ステップで範囲を半分にする
線形探索はすべてのアイテムをチェックする。バイナリ探索はソートされたリストを半分にしていくため、比較回数が大幅に少ない。
二項探索の最悪の時間計算量は:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
各ステップで範囲を半分にするため、比較回数は対数個となります。
百万個の整列済みアイテムに対して、バイナリサーチにはおおよそ何回の比較が必要か?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
$\log_2(1\,000\,000) \approx 20$ — およそ20回の比較です。
二項探索は、整列されているかどうかに関係なく、いかなるリストでも使用できます。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
中央の要素と比較してどの半分を破棄するかを決定しますが、これはデータに順序がある場合にのみ意味があります。
二項探索は O(log n) である理由は、各比較が ____ 残りの検索範囲を半分にすることだからです。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
百万を1にするには20回半分に分割すればよく、そのため 2^20 が百万よりわずかに大きいことが記憶すべき数値となる。
Worked example: trace a binary search
- The sorted list is 2, 5, 8, 12, 16, 23, 38, 56, 72, 91. Trace the search for 23.
lowis 1,highis 10, somidis 5, holding 16. 16 is less than 23, so discard the lower half:lowbecomes 6.low6,high10, somidis 8, holding 56. 56 is greater than 23, sohighbecomes 7.low6,high7, somidis 6, holding 23. Found, in three comparisons where a linear search would have taken six.- Show
low,high,midand the value at each step. Most of the marks are in the trace, not the answer.
整列リスト 2, 5, 8, 12, 16, 23, 38, 56, 72, 91 において、二項探索で 23 を見つけるために必要な比較回数は何回ですか?
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
mid 5 は 16(小さすぎる)、mid 8 は 56(大きすぎる)、mid 6 は 23。線形探索であれば6回必要でした。
Choosing between them
| linear | binary | |
|---|---|---|
| data must be sorted | no | yes |
| comparisons, worst case | $n$ | $\log_2 n$ |
| a million items | up to 1,000,000 | about 20 |
| suits | unsorted or small lists, linked lists | large sorted arrays, searched repeatedly |
- Sorting first costs more than one linear search, so binary search pays only when the list is already sorted or will be searched many times.
- Binary search also needs direct access to the middle element, which an array has and a linked list does not.
Worked example: justify the choice
- A program searches an unsorted list of 50 records once. Linear search: sorting the list first would cost far more than the 50 comparisons the search needs.
- A program searches a sorted array of a million records thousands of times a second. Binary search: the data is already sorted and each search costs about 20 comparisons instead of up to a million.
- A program searches a linked list. Linear search: binary search needs to jump straight to the middle element, and a linked list can only be followed from the start.
- Name the algorithm, then the property of the data that decides it.
各探索とその主要な事実を対応させてください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
二項探索は O(log n) で遥かに高速ですが、整列されたデータでのみ機能します。線形探索は O(n) でどこでも動作します。
線形探索がより適切な選択肢となるのはいつですか?適用されるものをすべて選択してください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
最後のケースこそが二項探索が勝つケースです。最初にソートするコストは単一の線形探索よりも高いため、多くの検索を行う場合に限り得策となります。
The cost of keeping the file sorted
- Binary search is only available on a sorted list, and that sorting is not free. A question that asks you to justify a choice is asking you to price it.
- If the data is searched often and changed rarely, sort it once and every later search is $\log_2 n$. That is the case for a dictionary or a lookup table.
- If the data changes constantly, every insertion has to keep the order, which costs a shift of the later elements. A linear search over unsorted data can then be the cheaper total.
- Numbers make the argument concrete: a million records need up to a million comparisons linearly, but only 20 by binary search, since $2^{20} > 10^6$.
- So the marked answer names both halves: how often it is searched, and how often it changes.
探索アルゴリズムを選択する理由の記述を正しい順序に並べ替えてください。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
"理由付け"問題は勝敗ではなくトレードオフを問います。頻繁に変化するリストに対する二項探索は、総計としては線形探索よりコストがかかる可能性があります。
Marks that slip away
- Binary search requires sorted data. Saying "it is faster" without that condition loses the mark.
- Each step halves the range, which is where the $\log_2 n$ comes from. Give the reason, not just the notation.
- Both searches must be able to report not found, which is what the
-1and the loop condition are for. - Binary search needs direct access, so it does not apply to a linked list even if the list is sorted.
You've got it
- linear search compares each element from the start, works on any list, and is $O(n)$
- binary search needs sorted data with direct access, compares the middle and halves the range each time, giving $O(\log_2 n)$: about 20 comparisons for a million items
- trace a binary search by showing
low,high,midand the value at each step - choose from the data: unsorted, small or a linked list means linear; large, sorted and searched often means binary