binary search DEMO
来源:互联网 发布:鹿晗经纪公司 知乎 编辑:程序博客网 时间:2024/06/05 08:33
#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
0 0
- binary search DEMO
- Binary search
- Binary Search
- Binary Search
- binary search
- binary search
- Binary Search
- binary search
- binary search
- binary search
- binary search
- binary search
- binary search
- Binary Search
- Binary search
- Binary Search
- Binary Search
- Binary Search
- 欢迎使用CSDN-markdown编辑器
- C++的内存管理
- C#初次使用NPOI导出Excel
- OpenH264 Intro - Compile on win7
- linux0.12之内核代码之『深入追踪fork函数』
- binary search DEMO
- Spring 系列: Spring 框架简介
- NYOJ 24 素数距离问题
- C#笔记 值类型 引用类型
- ajaxfileupload异步上传附件增加参数的方法
- EBS常用开发整理
- questions
- 构造函数的变量命名
- CodeChef PrimeDST【点分治】【FFT】