我对以下算法的时间复杂度有些困惑。我知道插入到堆中的是底数为2的O(log n),但是嵌套在里面的while循环似乎是底数为3的O(log n)。时间复杂度是否为O(log n的底数3)? * O(以2为底的log n)?
i = 1
while i < len(L):
insert L[i] into heap
i = i*3
答案 0 :(得分:2)
由于i
呈指数增长,因此将floor(log3(L))
元素插入heap
。
假设插入为O(log(n))
,其中n
是堆中已经存在的元素数(不是数组L
的元素,则为{{ 1}}插入最初为空的堆将具有复杂性m
。
根据斯特林近似值,O(log 1 + log 2 + log 3 + ... log (m - 1)) = O(log([m - 1]!) = O(log(m!))
。
用插入数代替,而忽略了阴影的ln(n!) = n ln n - n + O(ln n)
字词,我们有:
O(n)