binary search DEMO
2015-07-09 08:55
309 查看
#quote from MIT 'introduction to computation and programming using python, Revised' def search(L, e): """Assumes L is a list, the elements of which are in ascending order. Returns True if e is in L and False otherwise""" def bSearch(L, e, low, high): #Decrements high - low if high == low: return L[low] == e mid = (low + high)//2 if L[mid] == e: return True elif L[mid] > e: if low == mid: #nothing left to search return False else: return bSearch(L, e, low, mid-1) else: return bSearch(L, e, mid + 1, high) if len(L) == 0: return False else: return bSearch(L, e, 0, len(L) - 1)
L = [1, 2, 10, 8, 0, 100, 23, 89, 6]
L.sort()
search(L, 8)
Out[121]: True
search(L, 4)
Out[122]: False
相关文章推荐
- C#初次使用NPOI导出Excel
- C++的内存管理
- 欢迎使用CSDN-markdown编辑器
- Spring 系列: Spring 框架简介
- linux0.12之内核代码之『深入追踪fork函数』
- Oracle442个应用场景---------PL/SQL基础
- matlab的cell数组
- Python: Queue class
- 淘宝数据产品
- 必须冒着可能付出惨痛代价的风险前进,否则你就只能永远做个井底之蛙
- Slave failed to initialize relay log info structure from the repository
- Teamcenter PSM(四)
- 给初学者的 6 个 Android 加密工具
- 链接器和加载器
- 手动编译cpp文件(window下)
- Python: Stack class
- 财商低的六种表现
- jquery实现表单验证并阻止非法提交
- memcache-命令-stats
- ubuntu下搭建gtk+编程环境