bisect算法的复杂度是多少?

编程入门 行业动态 更新时间:2024-10-27 12:25:18
本文介绍了bisect算法的复杂度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编写了代码,以了解在搜索列表中的元素时哪个更快.事实证明是二等分的.我不明白什么是bisect算法的复杂性,它使用Van Emde Boas树吗?

I wrote code to understand which of them is faster when it comes to search an element in a list. It turns out to be bisect. What I do not understand is what is complexity of bisect algorithm and does it use Van Emde Boas tree?

#python inbuilt list search using 'in' took 0.0702499200317 secs def mul3(): a = [1, 2, 4, 5, 6, 7, 8, 10, 12, 42, 55, 65, 69, 95, 96, 101, 156, 199] for x in a: if x in a: print x, "True" else: print x, "False" #using bisect took 0.0649611193601 def mul4(): a = [1, 2, 4, 5, 6, 7, 8, 10, 12, 42, 55, 65, 69, 95, 96, 101, 156, 199] import bisect for x in a: locate = bisect.bisect_left(a, x) if locate == len(a) or a[locate] != x: print False print True #using binary search took 0.0651483638284 a = [1, 2, 4, 5, 6, 7, 8, 10, 12, 42, 55, 65, 69, 95, 96, 101, 156, 199] for x in a: lo = 0 hi = 18 while lo < hi: mid = (lo+hi)//2 midval = a[mid] if midval < x: lo = mid+1 elif midval > x: hi = mid else: print True lo = hi

参考: docs.python/library/bisect.html

推荐答案

它使用二进制搜索,从而使其变为O(log n).

It uses binary search, which makes it O(log n).

更多推荐

bisect算法的复杂度是多少?

本文发布于:2023-11-30 05:12:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复杂度   算法   bisect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!