[英]Time complexity of the given function with constant loop and recursion
這個時間復雜度是多少,是 O(logn) 嗎?
fun(int n) {
if (n < 2)
return 1;
int counter = 0;
for (int i = 1; i <= 8; i++)
fun(n / 2);
for (int i = 1; i <= Math.pow(n, 3); i++)
counter++;
}
function的復雜度為:
T(n) = n^3 + 8*T(n/2)
n^3
來自最后一個循環,即從1
到n^3
8*T(n/2)
來自調用fun(n/2)
8 次(在第一個循環中) 要找到復雜度,可以使用主定理: a = 8, b = 2, f(n) = n^3
使用案例 2:
log_2(8) = 3
,實際上f(n)
在Theta(n^3)
中,因此 function 復雜度為O(n^3*logn)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.