Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bin_search.py #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions bin_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,29 @@ def binarySearch(lst, value,low,high): #low,high是lst的查找范围
return mid

#也可以不用递归方法,而采用循环,如下:

def bsearch(l, value):
lo, hi = 0, len(l)-1
while lo <= hi:
mid = (lo + hi) / 2
if l[mid] < value:


def bisect_left(a, x):
lo = 0
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < x:
lo = mid + 1
elif value < l[mid]:
hi = mid - 1
else:
return mid
return -1
hi = mid
return lo


def bisect_right(a, x):
lo=0
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
return lo


"""
对于python,不能忽视其强大的标准库。经查阅,发现标准库中就有一个模块,名为:bisect。其文档中有这样一句话:
Expand Down