การค้นหา (Searching)
แนะนำใหม่
7:45
โครงสร้างข้อมูลและอัลกอริทึม
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| Linear search/ˈlɪnɪə sɜːtʃ/ | checking items one by one from the start until the target is found; O(n) |
| Binary search/ˈbaɪnəri sɜːtʃ/ | searching a sorted list by comparing with the middle item and discarding half each time; O(log n) |
| Interpolation search/ɪnˌtɜːpəˈleɪʃn sɜːtʃ/ | A search of sorted data that probes where the target's value should lie, estimated from the values at the ends of the range. |
| exponential search/ˌekspəˈnenʃl sɜːtʃ/ | Searching sorted data of unknown length by doubling the probe index until it passes the target, then binary-searching the last range. |
Two ways to look
- Linear search 线性查找 walks along the array: $O(n)$, with no conditions.
- Binary search 二分查找 halves a sorted range: compare with the middle value and keep one half. The data must be sorted; this is a requirement, not a preference.

Binary search requires —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
On unsorted data the halving argument fails, and the search gives wrong answers with no error.
The probe bound
- Each probe halves the candidates: after $k$ probes at most $\lceil n / 2^k \rceil$ remain. So the worst case is $\lceil \log_2(n+1) \rceil$ probes, which is $5$ for $n = 16$.
- On the sorted array $[3, 7, \ldots, 63]$ (16 slots), a search for $43$ probes indices $7, 11, 9, 10$, which hold $31, 47, 39, 43$. Four probes find it at index $10$. Linear search would read $11$ values to reach the same slot.
Four probes for 43
On the sorted 16-slot array 3, 7, …, 63, binary search for 43 probes indices 7, 11, 9 and 10. Switch to linear search and count how many more comparisons it needs.
The worst-case probes for binary search on 16 slots is —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Ceiling of log2 of 17 is 5: each probe halves what remains.
The miss that proves the bound
- Search for $64$, which is missing and larger than every value: binary search probes $7 (31), 11 (47), 13 (55), 14 (59), 15 (63)$. After five probes the range is empty, so it reports "absent". Linear search reads all $16$ for the same answer.
- Classic traps: binary search on unsorted data gives wrong answers without any error; setting
lo = midinstead ofmid + 1can loop for ever; in fixed-width languages compute the middle aslo + (hi - lo) // 2to avoid overflow.
Searching 43 on the course array probes —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Mid 7 reads 31 (go right), mid 11 reads 47 (go left), mid 9 reads 39, mid 10 finds.
Put one round of an iterative binary search in order.
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Checking the empty range first ends a miss; moving past mid with $\pm 1$ always shrinks the range, so the loop cannot run for ever.
Bisect on any monotone question
- Binary search works on any yes/no question whose answer changes only once along the range.
bisect_left([1, 3, 3, 3, 7], 3)returns $1$, the first index where $3$ could go;bisect_rightreturns $4$. So the number of $3$s is $4 - 1 = 3$, found in $O(\log n)$. - Bisection also finds answers to problems. $\sqrt{2}$ lies in $[1, 2]$; each test of $x^2 < 2$ halves the interval, so $10$ halvings reach width $1/1024 < 0.001$. The same idea finds the smallest truck capacity that ships every parcel on time.
What does bisect_left([2, 4, 4, 4, 4, 9], 4) return?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The first index where 4 could be inserted, keeping order, is 1.
How many halvings shrink the interval [0, 1000] to width at most 1?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
1000 / 2^10 = 0.98 is at most 1, but 1000 / 2^9 = 1.95 is not.
Guess the position from the value
- Interpolation search 插值查找 probes where the value should be: $lo + (x - a_{lo})(hi - lo)/(a_{hi} - a_{lo})$. On $[3, 7, \ldots, 63]$, a search for 43 gives $0 + 40 \times 15/60 = \mathbf{10}$: found in one probe.
- On evenly spread keys it needs about $O(\log\log n)$ probes on average. On unevenly spread keys it can fall to $O(n)$, so libraries keep binary search.
Interpolation search for 43 on $3, 7, \ldots, 63$ (16 values, indices 0 to 15): first probe index?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$0 + (43 - 3) \times 15/(63 - 3) = 10$.
Interpolation search is always faster than binary search.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Only on evenly spread keys; on skewed data it can take $O(n)$ probes.
Worked example: searching without knowing the end
- For a sorted stream of unknown length, exponential search 指数查找 probes indices 1, 2, 4, 8, ... until the value passes the target, then runs binary search on the last gap.
- A target at index 100: the probes 1 to 128 take 8 steps. Lower-midpoint binary search on indices 65 to 127 probes 96, 112, 104 and 100, for $\mathbf{12}$ probes in total. The cost is $O(\log i)$ for a target at index $i$.
Exponential search for a target at index 100: after probing indices 1, 2, 4, ..., 128, binary search indices 65–127 using the lower midpoint. How many probes in total?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The 8 doubling probes are followed by probes at 96, 112, 104 and 100, for 12 in total.
Marks that slip away
- Binary search on unsorted data. It returns a wrong answer and raises no error.
lo = midinstead ofmid + 1. Whenloandhiare neighbours the range never shrinks, and the loop never ends.- One probe short. The worst case is $\lceil \log_2(n+1) \rceil$: $5$ probes for $16$ items, not $4$.
Recap
- Linear search is $O(n)$ with no condition; binary search is $O(\log n)$ on sorted data.
| Worst case | Linear | Binary (sorted) |
|---|---|---|
| 16 items | 16 reads | 5 probes |
| a million | a million reads | 20 probes |
- Worst case $\lceil \log_2(n+1) \rceil$ probes: $5$ for $n = 16$.
- Use
lo = mid + 1, compute the middle aslo + (hi - lo) // 2, and bisect any monotone question.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้