下面代码的复杂性是多少

What is the complexity of the code below?

本文关键字:多少 复杂性 代码      更新时间:2023-10-16

有人能帮我找出以下代码的复杂性吗?

#include<iostream>
#include<string.h>
using namespace std;
//finding the factorial value
int fact(int n)
{
    return (n<=1)?1 : n*fact(n-1);
}
int fun(char *str)
{
    int rank=1;
    int len=strlen(str);
    for (int i=0;str[i]!='';i++)
    {    
        int count=0;
        //finding characters smaller than str[i]
        for(int j=i+1;str[j]!='';j++)
        {
            if(str[j]<str[i])
            count++;
        }
        len--;
        //finding the rank
        rank+=count*fact(len);
    }
    return rank;
}
int main()
{
    char str[]="string";
    cout<<endl<<fun(str);
    return 0;
}

我认为是O(n^2(。

阶乘函数是O(n(,fun是O(n^2(,所以,实际上它总共是O(n^2(。