What is the time complexity of the following function?

后端 未结 3 1703
走了就别回头了
走了就别回头了 2020-12-17 04:00
    int func(int n){
       if(n==1)
         return 0;
       else
         return sqrt(n);
    }

Where sqrt(n) is a C math.h library function.

3条回答
  •  时光说笑
    2020-12-17 04:34

    Let n=2^m
    Given T(n)=T(sqrt(n))+1
    T(2^m)=T(2^m-1)+1
    Let T(2^m)=S(m)
    then,
    S(m)=2S(m/2)+1
    using master theorem-
    S(m)=theta(m)
    =theta(log(n))
    Hence, time complexity is theta(log(n)).

提交回复
热议问题