mirror of
https://github.com/krahets/hello-algo.git
synced 2025-02-02 22:43:50 +08:00
Add the section of binary_search_recur.
This commit is contained in:
parent
909daea105
commit
fc7bcb615d
@ -0,0 +1,39 @@
|
|||||||
|
"""
|
||||||
|
File: binary_search_recur.py
|
||||||
|
Created Time: 2023-07-17
|
||||||
|
Author: krahets (xisunyy@163.com)
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def dfs(nums: list[int], target: int, i: int, j: int) -> int:
|
||||||
|
"""二分查找:分治"""
|
||||||
|
# 若区间为空,代表未找到目标元素,则返回 -1
|
||||||
|
if i > j:
|
||||||
|
return -1
|
||||||
|
# 计算中点索引 m
|
||||||
|
m = (i + j) // 2
|
||||||
|
if nums[m] < target:
|
||||||
|
# 此情况说明 target 在区间 [m+1, j] 中,递归解决该子问题
|
||||||
|
return dfs(nums, target, m + 1, j)
|
||||||
|
elif nums[m] > target:
|
||||||
|
# 此情况说明 target 在区间 [i, m-1] 中,递归解决该子问题
|
||||||
|
return dfs(nums, target, i, m - 1)
|
||||||
|
else:
|
||||||
|
# 找到目标元素,返回其索引
|
||||||
|
return m
|
||||||
|
|
||||||
|
|
||||||
|
def binary_search(nums: list[int], target: int) -> int:
|
||||||
|
"""二分查找"""
|
||||||
|
n = len(nums)
|
||||||
|
return dfs(nums, target, 0, n - 1)
|
||||||
|
|
||||||
|
|
||||||
|
"""Driver Code"""
|
||||||
|
if __name__ == "__main__":
|
||||||
|
target = 6
|
||||||
|
nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35]
|
||||||
|
|
||||||
|
# 二分查找(双闭区间)
|
||||||
|
index: int = binary_search(nums, target)
|
||||||
|
print("目标元素 6 的索引 = ", index)
|
Binary file not shown.
After Width: | Height: | Size: 56 KiB |
118
docs/chapter_divide_and_conquer/binary_search_recur.md
Normal file
118
docs/chapter_divide_and_conquer/binary_search_recur.md
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
# 分治搜索策略
|
||||||
|
|
||||||
|
我们已经学过,搜索算法分为两大类:暴力搜索、自适应搜索。暴力搜索的时间复杂度为 $O(n)$ 。自适应搜索利用特有的数据组织形式或先验信息,可达到 $O(\log n)$ 甚至 $O(1)$ 的时间复杂度。
|
||||||
|
|
||||||
|
实际上,**$O(\log n)$ 的搜索算法通常都是基于分治策略实现的**,例如:
|
||||||
|
|
||||||
|
- 二分查找的每一步都将问题(在数组中搜索目标元素)分解为一个小问题(在数组的一半中搜索目标元素),这个过程一直持续到数组为空或找到目标元素为止。
|
||||||
|
- 树是分治关系的代表,在二叉搜索树、AVL 树、堆等数据结构中,各种操作的时间复杂度皆为 $O(\log n)$ 。
|
||||||
|
|
||||||
|
分治之所以能够提升搜索效率,是因为暴力搜索每轮只能排除一个选项,**而基于分治的搜索每轮可以排除一半选项**。
|
||||||
|
|
||||||
|
## 基于分治实现二分
|
||||||
|
|
||||||
|
接下来,我们尝试从分治策略的角度分析二分查找的性质:
|
||||||
|
|
||||||
|
- **问题可以被分解**:二分查找递归地将原问题(在数组中进行查找)分解为子问题(在数组的一半中进行查找),这是通过比较中间元素和目标元素来实现的。
|
||||||
|
- **子问题是独立的**:在二分查找中,每轮只处理一个子问题,它不受另外子问题的影响。
|
||||||
|
- **子问题的解无需合并**:二分查找旨在查找一个特定元素,因此不需要将子问题的解进行合并。当子问题得到解决时,原问题也会同时得到解决。
|
||||||
|
|
||||||
|
在之前章节中,我们基于递推(迭代)实现二分查找。现在,我们尝试基于递归分治来实现它。
|
||||||
|
|
||||||
|
问题定义为:**在数组 `nums` 的区间 $[i, j]$ 内查找元素 `target`** ,记为 $f(i, j)$ 。
|
||||||
|
|
||||||
|
设数组长度为 $n$ ,则二分查找的流程为:从原问题 $f(0, n-1)$ 开始,每轮排除一半索引区间,递归求解规模减小一半的子问题,直至找到 `target` 或区间为空时返回。
|
||||||
|
|
||||||
|
下图展示了在数组中二分查找目标元素 $6$ 的分治过程。
|
||||||
|
|
||||||
|
![二分查找的分治过程](binary_search_recur.assets/binary_search_recur.png)
|
||||||
|
|
||||||
|
如下代码所示,我们声明一个递归函数 `dfs()` 来求解问题 $f(i, j)$ 。
|
||||||
|
|
||||||
|
=== "Java"
|
||||||
|
|
||||||
|
```java title="binary_search_recur.java"
|
||||||
|
[class]{binary_search_recur}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{binary_search_recur}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C++"
|
||||||
|
|
||||||
|
```cpp title="binary_search_recur.cpp"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Python"
|
||||||
|
|
||||||
|
```python title="binary_search_recur.py"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binary_search}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Go"
|
||||||
|
|
||||||
|
```go title="binary_search_recur.go"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "JavaScript"
|
||||||
|
|
||||||
|
```javascript title="binary_search_recur.js"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "TypeScript"
|
||||||
|
|
||||||
|
```typescript title="binary_search_recur.ts"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C"
|
||||||
|
|
||||||
|
```c title="binary_search_recur.c"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "C#"
|
||||||
|
|
||||||
|
```csharp title="binary_search_recur.cs"
|
||||||
|
[class]{binary_search_recur}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{binary_search_recur}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Swift"
|
||||||
|
|
||||||
|
```swift title="binary_search_recur.swift"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Zig"
|
||||||
|
|
||||||
|
```zig title="binary_search_recur.zig"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "Dart"
|
||||||
|
|
||||||
|
```dart title="binary_search_recur.dart"
|
||||||
|
[class]{}-[func]{dfs}
|
||||||
|
|
||||||
|
[class]{}-[func]{binarySearch}
|
||||||
|
```
|
@ -208,8 +208,9 @@ nav:
|
|||||||
- 12. 分治:
|
- 12. 分治:
|
||||||
- chapter_divide_and_conquer/index.md
|
- chapter_divide_and_conquer/index.md
|
||||||
- 12.1. 分治算法(New): chapter_divide_and_conquer/divide_and_conquer.md
|
- 12.1. 分治算法(New): chapter_divide_and_conquer/divide_and_conquer.md
|
||||||
- 12.2. 构建树问题(New): chapter_divide_and_conquer/build_binary_tree_problem.md
|
- 12.2. 分治搜索策略(New): chapter_divide_and_conquer/binary_search_recur.md
|
||||||
- 12.3. 汉诺塔问题(New): chapter_divide_and_conquer/hanota_problem.md
|
- 12.3. 构建树问题(New): chapter_divide_and_conquer/build_binary_tree_problem.md
|
||||||
|
- 12.4. 汉诺塔问题(New): chapter_divide_and_conquer/hanota_problem.md
|
||||||
- 13. 回溯:
|
- 13. 回溯:
|
||||||
- chapter_backtracking/index.md
|
- chapter_backtracking/index.md
|
||||||
- 13.1. 回溯算法: chapter_backtracking/backtracking_algorithm.md
|
- 13.1. 回溯算法: chapter_backtracking/backtracking_algorithm.md
|
||||||
|
Loading…
Reference in New Issue
Block a user