Skip to content

Commit 2993e21

Browse files
Merge pull request wangzheng0822#259 from dreamkong/master
二分查找递归实现 Python
2 parents 615fbab + f90eb8f commit 2993e21

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Author: dreamkong
3+
"""
4+
5+
from typing import List
6+
7+
8+
def bsearch(nums: List[int], target: int) -> int:
9+
return bsearch_internally(nums, 0, len(nums)-1, target)
10+
11+
12+
def bsearch_internally(nums: List[int], low: int, high: int, target: int) -> int:
13+
if low > high:
14+
return -1
15+
16+
mid = low+int((high-low) >> 2)
17+
if nums[mid] == target:
18+
return mid
19+
elif nums[mid] < target:
20+
return bsearch_internally(nums, mid+1, high, target)
21+
else:
22+
return bsearch_internally(nums, low, mid-1, target)

0 commit comments

Comments
 (0)