单选题:时间复杂度分析(递归调用1次,参数减半)
时间复杂度分析
以下算法的时间复杂度为 ▁▁▁▁▁。
c
int foo(int n)
{
int s;
if (n <= 1)
{
s = 1;
}
else
{
s = foo(n / 2) + 1;
}
return s;
}
A.$$O(1)$$
B.$$O(\log_{2}n)$$
C.$$O(n)$$
D.$$O(n ^ 2)$$
答案:B
以下算法的时间复杂度为 ▁▁▁▁▁。
c
int foo(int n)
{
int s;
if (n <= 1)
{
s = 1;
}
else
{
s = foo(n / 2) + 1;
}
return s;
}
A.$$O(1)$$
B.$$O(\log_{2}n)$$
C.$$O(n)$$
D.$$O(n ^ 2)$$
答案:B