Thuật toán Tìm kiếm
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| Searching/ˈsɜːtʃɪŋ/ | Tìm kiếm |
| linear search/ˈlɪnɪə sɜːtʃ/ | tìm kiếm tuyến tính |
| binary search/ˈbaɪnəri sɜːtʃ/ | Tìm kiếm nhị phân |
| sorted/ˈsɔːtɪd/ | Đã sắp xếp |
Finding a value
- Searching 查找 means locating a target value in a collection.
- Two standard algorithms: linear search 线性查找 and binary search 二分查找.
- Both return the index where the target sits — or a "not found" signal (often
-1). - Which one you may use depends on whether the data is sorted.
Linear search
- Check each element from the start, one by one, until you find the target.
for (int i = 0; i < a.length; i++) if (a[i] == target) return i;- Works on any array — sorted or not.
- Worst case: it looks at every element (
nchecks).

Linear search checks every element in turn until the target is found

Binary search halves the range at each step
Binary search
- Needs a sorted 已排序 array. Look at the middle element each time.
- If the middle is the target, done. If the target is smaller, search the left half; if larger, the right half.
- Each step halves the range that's left to search.
- Far faster on large sorted arrays — about
log₂ nchecks, notn.
Why binary search is fast
- Linear search of a million items: up to a million checks.
- Binary search of a million sorted items: about 20 checks.
- The catch: the array must already be sorted.
- Halving repeatedly is the big idea behind
O(log n).
Binary search only works on a SORTED array — running it on unsorted data gives wrong answers. It also compares to the middle and throws away half the range each step; a linear search compares from the start and drops just one element. If you're not sure the data is sorted, you must use linear search (or sort first).
Binary search for 7 in [1, 3, 5, 7, 9]:
- Middle is
5(index 2). 7 > 5, so search the right half. - Right half is
[7, 9]; middle is7. Found at index 3. - Two checks instead of four — the range halved each time.
Linear search checks elements from the start (works on any array, up to n checks). Binary search needs a sorted array, compares to the middle, and halves the search range each step (about log₂ n checks). Both return the index found, or a "not found" signal like -1.
Tìm kiếm tuyến tính so với tìm kiếm nhị phân
Tìm kiếm nhị phân chia đôi phạm vi đã sắp xếp ở mỗi bước.
Tìm kiếm tuyến tính...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tuyến tính = từ đầu đến cuối; hoạt động trên mọi mảng.
Tìm kiếm nhị phân yêu cầu mảng phải được...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tìm kiếm nhị phân chỉ hoạt động trên dữ liệu đã sắp xếp.
Mỗi bước của tìm kiếm nhị phân...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Nó so sánh với phần tử ở giữa và giữ lại một nửa.
Tìm kiếm nhị phân trong [1,3,5,7,9] cho giá trị 7: bao nhiêu phép so sánh (so sánh với phần tử giữa mỗi lần)?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
So sánh với 5, sau đó so sánh với 7 — hai phép so sánh.
Tìm kiếm nhị phân đưa ra kết quả đúng trên mảng KHÔNG SẮP XẾP.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Nó dựa vào thứ tự; dữ liệu không sắp xếp sẽ phá hủy thuật toán.
Ghép nối mỗi loại tìm kiếm với thuộc tính của nó.
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Tuyến tính nói chung nhưng chậm hơn; nhị phân nhanh nhưng cần thứ tự.