5.4. 二分搜索¶
5.4. The Binary Search
如果我们巧妙地进行比较,就可以更好地利用有序列表。在顺序搜索中,当我们与第一个项进行比较时,最多还有
接下来我们可以对右半部分重复这个过程。先从中间项开始,将它与我们要找的项进行比较。同样地,我们要么找到它,要么再次将列表一分为二,从而排除一大部分可能的搜索范围。图3
展示了这个算法如何快速找到值 54。完整的函数如 CodeLens 3
所示。

活动: CodeLens 有序列表的二分搜索 (search3) | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
在进行分析之前,我们应该注意到这个算法是“分而治之”策略的一个极好例子。“分而治之”意味着我们将问题分解为更小的部分,以某种方式解决这些小部分,然后将整个问题重新组合起来得到结果。当我们对列表执行二分搜索时,首先检查中间项。如果我们要找的项小于中间项,我们可以简单地对原列表的左半部分执行二分搜索。同样地,如果该项更大,我们可以对右半部分进行二分搜索。无论哪种情况,这都是对二分搜索函数的递归调用,传递的是一个更小的列表。CodeLens 4
显示了递归版本的二分搜索。
活动: CodeLens 二分搜索--递归版本 (search4) | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
It is possible to take greater advantage of the ordered list if we are clever with our comparisons. In the sequential search, when we compare against the first item, there are at most
We can then repeat the process with the left half. Start at the middle item and compare it against what we are looking for. Again, we either find it or split the list in half, therefore eliminating another large part of our possible search space. Figure 3
shows how this algorithm can quickly find the value 54. The complete function is shown in CodeLens 3
.

Activity: CodeLens Binary Search of an Ordered List (search3) | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Before we move on to the analysis, we should note that this algorithm is a great example of a divide and conquer strategy. Divide and conquer means that we divide the problem into smaller pieces, solve the smaller pieces in some way, and then reassemble the whole problem to get the result. When we perform a binary search of a list, we first check the middle item. If the item we are searching for is less than the middle item, we can simply perform a binary search of the left half of the original list. Likewise, if the item is greater, we can perform a binary search of the right half. Either way, this is a recursive call to the binary search function passing a smaller list. CodeLens 4
shows this recursive version.
Activity: CodeLens A Binary Search--Recursive Version (search4) | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
5.4.1. 二分搜索的分析¶
5.4.1. Analysis of Binary Search
为了分析二分搜索算法,我们需要记住每次比较都会排除大约一半的剩余项。那么,这个算法最多需要多少次比较来检查整个列表呢?如果我们从 表3
帮助我们找到答案。
表3:二分搜索的表格分析
比较次数 | 剩余项的近似数量 |
---|---|
1 | |
2 | |
3 | |
... | |
当我们将列表分割足够多次时,最终会剩下一个项。这个项要么是我们要找的,要么不是。不管是哪种情况,我们的搜索都结束了。达到这个点所需的比较次数是
还有一个需要解决的分析问题。在上面的递归解决方案中,递归调用
binary_search_rec(a_list[:midpoint], item)
使用了切片操作符来创建列表的左半部分并将其传递给下一个调用(右半部分也是如此)。我们之前的分析假设切片操作是常数时间的。但实际上在 Python 中,它的时间复杂度是 列表3
中那样计算。我们将这个实现留作练习。
尽管二分搜索通常比顺序搜索更好,但需要注意的是,对于较小的
自检
假设你有如下排序列表 [3, 5, 6, 8, 11, 12, 14, 15, 17, 18],并使用递归二分搜索算法。以下哪组数字正确显示了查找键值 8 的比较顺序?
- 答案 a: 11, 5, 6, 8
- 答案 b: 12, 6, 11, 8
- 答案 c: 3, 5, 6, 8
- 答案 d: 18, 12, 6, 8
正确答案: b
- 反馈 a: 你可能犯了“差一”错误。记住列表的第一个位置是索引 0。
- 反馈 b: 二分搜索从中间开始,每次将列表对半分。
- 反馈 c: 二分搜索不会从开头开始顺序搜索,而是从中间开始,每次比较后将列表对半分。
- 反馈 d: 你似乎是从末尾开始并每次将列表对半分。
假设你有如下排序列表 [3, 5, 6, 8, 11, 12, 14, 15, 17, 18],并使用递归二分搜索算法。以下哪组数字正确显示了查找键值 16 的比较顺序?
- 答案 a: 11, 14, 17
- 答案 b: 18, 17, 15
- 答案 c: 14, 17, 15
- 答案 d: 12, 17, 15
正确答案: d
- 反馈 a: 你可能犯了“差一”错误。记住列表的第一个位置是索引 0。
- 反馈 b: 记住二分搜索从中间开始并每次将列表对半分。
- 反馈 c: 你可能有点偏差了,请注意你是否使用整数运算正确计算了中点。
- 反馈 d: 二分搜索从中间开始,每次将列表对半分。当列表为空时,搜索结束。
To analyze the binary search algorithm, we need to recall that each comparison eliminates about half of the remaining items from consideration. What is the maximum number of comparisons this algorithm will require to check the entire list? If we start with Table 3
helps us to see the answer.
Table 3: Tabular Analysis for a Binary Search
Comparisons | Approximate Number of Items Left |
---|---|
1 | |
2 | |
3 | |
... | |
When we split the list enough times, we end up with a list that has just one item. Either that is the item we are looking for or it is not. Either way, we are done. The number of comparisons necessary to get to this point is
One additional analysis issue needs to be addressed. In the recursive solution shown above, the recursive call
binary_search_rec(a_list[:midpoint], item)
uses slicing operator to create the left half of the list that is then passed to the next invocation (similarly for the right half as well). The analysis that we did above assumed that slicing takes constant time. However, we know that in Python it is actually Listing 3
. We leave this implementation as an exercise.
Even though a binary search is generally better than a sequential search, it is important to note that for small values of
Self Check
Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using the recursive binary search algorithm. Which group of numbers correctly shows the sequence of comparisons used to find the key 8.
- answer a: 11, 5, 6, 8
- answer b: 12, 6, 11, 8
- answer c: 3, 5, 6, 8
- answer d: 18, 12, 6, 8
correct: b
- feedback a: Looks like you might be guilty of an off-by-one error. Remember the first position is index 0.
- feedback b: Binary search starts at the midpoint and halves the list each time.
- feedback c: Binary search does not start at the beginning and search sequentially, its starts in the middle and halves the list after each compare.
- feedback d: It appears that you are starting from the end and halving the list each time.
Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using the recursive binary search algorithm. Which group of numbers correctly shows the sequence of comparisons used to search for the key 16?
- answer a: 11, 14, 17
- answer b: 18, 17, 15
- answer c: 14, 17, 15
- answer d: 12, 17, 15
correct: d
- feedback a: Looks like you might be guilty of an off-by-one error. Remember the first position is index 0.
- feedback b: Remember binary search starts in the middle and halves the list.
- feedback c: Looks like you might be off by one, be careful that you are calculating the midpont using integer arithmetic.
- feedback d: Binary search starts at the midpoint and halves the list each time. It is done when the list is empty.
创建日期: 2024年9月9日