python等分列表_Python二等分列表

bisect模块跟踪列表,保持其排序,而不必在每次插入元素时求助。您需要实现的方法只需要在排序列表中搜索。

def bisect(sortedlist,targetvalue,firstindex=0,lastindex=None):

if(len(sortedlist)==0):

return None

if(len(sortedlist)==1):

if(sortedlist[0]==targetvalue):

return firstindex

else:

return None

center = int(round(len(sortedlist)/2))

if(sortedlist[center]==targetvalue):

return firstindex+center

if(targetvalue>sortedlist[center]):

return bisect(sortedlist[center+1:lastindex],targetvalue,center+1,lastindex)

else:

return bisect(sortedlist[0:center],targetvalue,firstindex,center-1)这基本上是二进制搜索。

传递索引以跟踪递归循环中进一步调用的原始列表的索引。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值