python 二分查找和快速排序实例详解,python排序实例详解
python 二分查找和快速排序实例详解,python排序实例详解
思想简单,细节颇多;本以为很简单的两个小程序,写起来发现bug频出,留此纪念。
#usr/bin/env python def binary_search(lst,t): low=0 height=len(lst)-1 quicksort(lst,0,height) print lst while low<=height: mid = (low+height)/2 if lst[mid] == t: return lst[mid] elif lst[mid]>t: height=mid-1 else: low=mid+1 return -1 def quicksort( lst, left , right): low=left high=right key=lst[left] if left>=right: return 0 while low<high: while low<high and key<lst[high]: high=high-1 lst[low]=lst[high] while low<high and key>lst[low]: print lst[low] low=low+1 lst[high]=lst[low] lst[low]=key quicksort( lst , left ,low-1) quicksort( lst , low+1 , right) if __name__=='__main__': print binary_search([4,8,1,5,10,2,12,3,6,9],4)
总结
以上所述是小编给大家介绍的python 二分查找和快速排序实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对帮客之家网站的支持!
相关内容
- python出现"IndentationError: unexpected indent"错误解决办法
- 使用Python操作excel文件的实例代码,
- python中import reload __import__的区别详解,python__import_
- python中文件变化监控示例(watchdog),
- python利用paramiko连接远程服务器执行命令的方法,pyth
- Python使用文件锁实现进程间同步功能【基于fcntl模块】
- Python实现扩展内置类型的方法分析,python实现内置类型
- python版简单工厂模式,python工厂模式
- 基于Python和Scikit-Learn的机器学习探索,pythonscikit-learn
- Python通过future处理并发问题,pythonfuture
评论关闭