Python实现二分查找
Python #二分查找 #算法2012-11-20 10:00
代码如下:
'fileName--BinarySearch.py' src = [] def BinarySearch(low, high, target, *src): '二分查找 http://yige.org ' while low <= high: mid = (low + high) // 2 midVal = src[mid] if target < midVal: high = mid - 1 elif target > midVal: low = mid + 1 else: return mid BinarySearch(low, high, target, *src) print('Please input 10 number:') for number in range(10): src.append(int(input('Num %d:' % number))) sortList = tuple(src) key = int(input('Please input key:')) location = BinarySearch(0, len(src) - 1, key, *sortList) if location != None: print('Find target at %d' % (location + 1)) else: print('No target!')
相关文章
- Python对文件批量随机重命名 2012/11/20
- Python做磁盘文件服务器 2012/11/20
- IIS7.0+Python环境搭建步骤 2012/11/19
- 在IIS中如何执行Python脚本 2012/11/19
- 谈Python集合运算 2012/11/17
- Python中lambda表达式使用方法 2012/11/17
- Python调用短信猫控件发短信 2012/11/17
- Python模块之StringIO 2012/11/16
- Python根据一个日期获得星期几 2012/11/16
- Python Django发送邮件的配置方法 2012/11/16