据我所知,
以下是我正在运行的代码,尝试计算给定数组(大小为
std::next_permutation
算法的时间复杂度为O(n!)。有人能解释一下为什么吗?或者说我是否正确?以下是我正在运行的代码,尝试计算给定数组(大小为
n
)被排序前需要进行多少次排列操作:int permutationSort(int a[], int n)
{
int count = 0;
while (next_permutation(a, a + n))
{
count++;
}
return count;
}
std::next_permutation
的参考文献有帮助吗? - Some programmer dudenext_permutation
的时间复杂度和使用next_permutation
进行排序的时间复杂度。 - Jarod42n!
(第一个元素有n种可能性,第二个有n-1种,然后... ->n * (n-1) * .. * 2 * 1
->n!
)。在这里,您需要迭代直到序列排序,因此最多调用n!
次next_permutation
。 - Jarod42O(n * n!)
(需要n!
次调用next_permutation
),最好情况是O(n)
,即只需一次调用next_permutation
(当a
已经是最后一个排列时)。 - Holt