Skip to content

Commit

Permalink
二分查找算法实现
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwsir committed Jun 25, 2014
1 parent bc0728f commit 42f02f1
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ list.index()无法应对大规模数据的查询,需要用其它方法解决

- 模块接受排序后的列表。
- 本模块同样适用于长列表项。因为它就是用二分查找方法实现的,有兴趣可以看其源码(源码是一个很好的二分查找算法的例子,特别是很好地解决了边界条件极端的问题.)
- 关于bisect模块的更多内容,可以参看官方文档
- 关于bisect模块的更多内容,可以参看[官方文档](https://docs.python.org/2/library/bisect.html)

下面演示这个模块的一个函数

Expand Down
3 changes: 2 additions & 1 deletion bin_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ def bsearch(l, value):
- 本模块同样适用于长列表项。因为它就是用二分查找方法实现的,有兴趣可以看其源码(源码是一个很好的二分查找算法的例子,特别是很好地解决了边界条件极端的问题.)
-关于本模块,可以查看官方文档:https://docs.python.org/2/library/bisect.html
"""
#下面演示这个模块

from bisect import *

def bisectSearch(lst, x):
i = bisect_left(lst, x)
i = bisect_left(lst, x) #bisect_left(lst,x)得到x在已经排序的lst中的位置
if i != len(lst) and lst[i] == x:
return i

Expand Down

0 comments on commit 42f02f1

Please sign in to comment.