简体   繁体   English

如何找到这个算法的复杂度?

[英]How to find the complexity of this algorithm?

I tried to find the complexity of this algorithm and am I right to say the complexity is O(N*log(N))我试图找到这个算法的复杂度,我说复杂度是 O(N*log(N)) 是对的

Can someone show me the full working on the complexity of this algorithm?有人可以向我展示有关该算法复杂性的全部工作吗?

def binary_min(arr):
    if len(arr) == 1:
        return arr[0]
    else:
        mid = len(arr)//2
        min1 = binary_min(arr[0:mid])
        min2 = binary_min(arr[mid:len(arr)])
        if min1 < min2:
            return min1
        else:
            return min2

除以 2 的复杂度(如二分查找)是log(N)并且会发生N次,所以复杂度等于Nlog(N)

假设这是一个二分搜索算法,时间复杂度为O(log n) ,最佳情况下的时间复杂度为O(1) ,其中mid索引与所需值匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM