使用python实现二分查找
来源:互联网 发布:360解压软件下载 编辑:程序博客网 时间:2024/06/04 23:22
import mathdef binary_search(list0, item):#list是已知的数组,item是要在list中寻找的数 low = 0 high = len(list0) - 1 while low <= high: mid = math.floor((low + high) / 2)#Python2中有自动向下取整功能,python3中没有,所以要导入math模块中的向下取整方法math.floor guess = list0[mid] if guess == item: return mid if guess > item: high = mid -1 else: low = mid + 1 return Nonemy_list = [1, 3, 5, 7, 9]print(binary_search(my_list, 7))print(binary_search(my_list, 2))
最终运行结果:
3None
阅读全文
0 0
- 使用python实现二分查找
- Python实现二分查找
- Python实现二分查找
- python实现二分查找
- 二分查找 python实现
- python实现二分查找
- python实现二分查找
- Python实现二分查找
- python实现二分查找
- 用Python实现二分查找
- 用Python实现二分查找
- Python实现的二分查找
- Python实现二分查找法
- python的二分查找实现
- Python实现二分查找算法
- python实现的二分查找
- 二分查找及python实现
- 二分查找算法python实现
- NOIP2017提高组 模拟赛16(总结)
- Maven的Helloworld
- 凯儿得乐企业化系统平台开发
- 相反数
- 通知栏(Notification)的基本使用
- 使用python实现二分查找
- day11-Tomcat与Servlet入门
- new和newInstance() Constructor.newInstance()区别
- 展讯7731C_M Android6.0 充电指示灯实现
- 常用数据库连接描述符
- Android瘦身之tiny图片处理
- 用python 实现链表(实现__getitem__,__set__,__len__ 魔法方法)
- Wannafly模拟赛 矩阵 [矩阵hash+二分]
- Java