c - c中的递归顺序

标签 c algorithm recursion complexity-theory

我编写了一个程序,使用回溯法打印字符串的所有排列。

# include <stdio.h>
/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string. */

void permute(char *a, int i, int n) 
{
   int j; 
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
              swap((a+i), (a+j));
              permute(a, i+1, n);
              swap((a+i), (a+j)); //backtrack
           }
   }
} 

/* Driver program to test above functions */
int main()
{
   char a[] = "ABC";  
   permute(a, 0, 2);
   getchar();
   return 0;
}

这里的时间复杂度是多少,不就是o(n2)吗。递归的情况下如何判断时间复杂度呢?如果我错了,请纠正我。

谢谢。

最佳答案

复杂度是O(N*N!),你有N!排列,你得到所有的排列。
此外,每个排列都需要您打印它,这是 O(N) - 所以总计 O(N*N!)

关于c - c中的递归顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14352924/

相关文章:

c - shellcode 中的地址在执行过程中发生变化

C 中整数指针的常量值分配适用于这种情况

python - 递归查找文件夹中文件的最后一次编辑

c++ - 用递归减去反向数字

c - 套接字编程 : recv/read issue

c - Makefile 多重定义错误

c# - 子串深度扫描

algorithm - 尝试了解 SCM 历史/修订/恢复算法,尤其是 GIT

algorithm - 反馈和 HRRN 调度算法?

javascript - Mongodb递归搜索