C++ 下面代码的复杂性是什么?

C++ 下面代码的复杂性是什么?,c++,time-complexity,C++,Time Complexity,有人能帮我找出下面代码的复杂性吗 #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 (i

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

#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]!='\0';i++)
    {    
        int count=0;
        //finding characters smaller than str[i]
        for(int j=i+1;str[j]!='\0';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;
}
#包括
#包括
使用名称空间std;
//寻找阶乘值
整数事实(整数n)
{

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

而fun是O(n^2),这在嵌套循环中很明显,在外循环中fact()被调用了n次。所以,最多是O(n^2),其中n是字符串的长度。thnk u zan.你能一步一步地解释一下O(n^2)是如何首先是主循环吗()只调用fun()一次。fun()取一个大小为n的字符串。因此,我们需要找到fun()的时间复杂度,在n中,fun()有两个循环。第一个循环运行n次,第二个循环运行n-1次。因此,总共n*(n-1)是O(n^2),但在fun()中,我们调用fact()n次,fact()只是O(2^log2(n))=O(n).所以总的来说应该是O(n^2)