Searching
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Searching a list
- Searching means finding whether a value is in a list, and where.
- The usual answer is the index of the value, or
-1if it is missing. - Two classic methods are linear search and binary search.
日本語
リストの検索
- 検索とは、値がリストに含まれているかどうか、そしてその位置を特定することです。
- 通常の答えは値のインデックスであり、存在しない場合は
-1です。 - 2つの古典的な手法として、線形探索と二分探索があります。
English
Linear search
- Check each item in turn, from the start.
- Stop as soon as you find a match.
- It works on any list, sorted or not.
日本語
線形探索
- 先頭から順に各要素を確認します。
- 一致するものを見つけたら直ちに停止します。
- 並べ替え済みのリストでも未整序のリストでも機能します。
names = ["Sam", "Mia", "Leo"]
target = "Mia"
found = -1
for i in range(len(names)):
if names[i] == target:
found = i
break
print(found)
English
Binary search
- Binary search needs a sorted list.
- Look at the middle item. If it is the target, stop.
- If the target is smaller, search the left half; if bigger, the right half.
日本語
二分探索
- 二分探索には整序されたリストが必要です。
- 真ん中の要素を確認します。これがターゲットであれば停止します。
- ターゲットが小さい場合は左半分を、大きい場合は右半分を探索します。
data = [2, 4, 6, 8, 10]
target = 8
low = 0
high = len(data) - 1
found = -1
while low <= high:
mid = (low + high) // 2
if data[mid] == target:
found = mid
break
elif data[mid] < target:
low = mid + 1
else:
high = mid - 1
print(found)
English
Compare them
- Linear search may check every item — slow for a long list.
- Binary search throws away half the list each step, so it is much faster.
- But binary search only works if the data is already sorted.
日本語
比較
- 線形探索はすべての要素を確認することがあるため、長いリストでは遅いです。
- 二分探索はステップごとにリストの半分を除外するため、はるかに速いです。
- ただし、二分探索はデータが既に整序されている場合にのみ機能します。
English
In Cambridge pseudocode
- Note
DIVis whole-number division (Python's//).
日本語
Cambridge擬似コードにおける表現
DIVは整数除算(Pythonの//)であることを覚えておいてください。
// Linear search — stop at the first match
found ← -1
i ← 0
WHILE i < LENGTH(list) AND found = -1
IF list[i] = target THEN
found ← i
ENDIF
i ← i + 1
ENDWHILE
// Binary search (list must be sorted)
found ← -1
low ← 0
high ← LENGTH(list) - 1
WHILE low <= high AND found = -1
mid ← (low + high) DIV 2
IF list[mid] = target THEN
found ← mid
ELSE
IF list[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
ENDIF
ENDIF
ENDWHILE
English
Common mistakes
- Binary search needs a sorted list; it halves the range each step.
- Linear search works on any list but is slower.
日本語
よくあるミス
- 二分探索には整序されたリストが必要で、ステップごとに範囲を半分にします。
- 線形探索はどのようなリストでも機能しますが、速度は遅いです。
English
Now you try
- Return the index of the value, or
-1when it is not found. - Press Check answer to test your code.
日本語
あなたも試してみよう
- 値のインデックスを返すか、見つからない場合は
-1を返します。 - 回答を確認 を押してコードを試してください。
Explore · 探索
Linear vs binary search
Binary search halves the list each step — far fewer comparisons.
Write linear_search(items, target) that returns the index of target in items, or -1 if it is not there. Check the items one by one.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write binary_search(items, target) for a sorted list. Return the index of target, or -1 if it is missing. Halve the range each step.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Write first_negative(items) that scans the list and returns the index of the first number less than 0, or -1 if there are none.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。