Skip to content

Commit

Permalink
fix: 修复二分查找算法
Browse files Browse the repository at this point in the history
  • Loading branch information
dengshilong committed May 14, 2019
1 parent 360aa1d commit 3827dfe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
18 changes: 9 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1569,21 +1569,21 @@ def node(l1, l2):
```python

#coding:utf-8
def binary_search(list,item):
def binary_search(list, item):
low = 0
high = len(list)-1
while low<=high:
mid = (low+high)/2
high = len(list) - 1
while low <= high:
mid = (high - low) / 2 + low # 避免(high + low) / 2溢出
guess = list[mid]
if guess>item:
high = mid-1
elif guess<item:
low = mid+1
if guess > item:
high = mid - 1
elif guess < item:
low = mid + 1
else:
return mid
return None
mylist = [1,3,5,7,9]
print binary_search(mylist,3)
print binary_search(mylist, 3)

```

Expand Down

0 comments on commit 3827dfe

Please sign in to comment.