这里先提供一段二分查找的代码:
def binarysearch(alist, item): first = 0 last =len(alist)-1 found = false while first<=lastand not found:midpoint = (first + last)//2if alist[midpoint] == item: found = trueelse: if item lst[mid]: #找后半部分 #print(lst[mid:]) returnbinsearch(lst[mid:], item) else: returnbinsearch(lst[:mid], item) #找前半部分
测试通过。
更多关于python相关内容可查看本站专题:《python正则表达式用法总结》、《python数据结构与算法教程》、《python socket编程技巧总结》、《python函数使用技巧总结》、《python字符串操作技巧汇总》、《python入门与进阶经典教程》及《python文件与目录操作技巧汇总》
希望本文所述对大家python程序设计有所帮助。
