Skip to content

Commit

Permalink
Merge pull request wangzheng0822#259 from dreamkong/master
Browse files Browse the repository at this point in the history
二分查找递归实现 Python
  • Loading branch information
wangzheng0822 authored Mar 6, 2019
2 parents 615fbab + f90eb8f commit 2993e21
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python/15_bsearch/bsearch_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Author: dreamkong
"""

from typing import List


def bsearch(nums: List[int], target: int) -> int:
return bsearch_internally(nums, 0, len(nums)-1, target)


def bsearch_internally(nums: List[int], low: int, high: int, target: int) -> int:
if low > high:
return -1

mid = low+int((high-low) >> 2)
if nums[mid] == target:
return mid
elif nums[mid] < target:
return bsearch_internally(nums, mid+1, high, target)
else:
return bsearch_internally(nums, low, mid-1, target)

0 comments on commit 2993e21

Please sign in to comment.