Time complexity of a recursive algorithm

前端 未结 5 2150
礼貌的吻别
礼貌的吻别 2020-11-29 01:19

How can I calculate the time complexity of a recursive algorithm?

int pow1(int x,int n) {
    if(n==0){
        return 1;
    }
    else{
        return x *          


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 02:12

    So I'm guessing you're raising x to the power n. pow1 takes O(n).

    You never change the value of x but you take 1 from n each time until it gets to 1 (and you then just return) This means that you will make a recursive call n times.

提交回复
热议问题