我曾使用过这个程序,如何计算回溯算法的时间复杂度?
/*
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 swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
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
}
}
}
n!
个n
个对象的排列,如果OP的算法具有其他复杂度,那将是一个相当大的惊喜。 - Victor Sorokin