I'm studying time complexity of recursive algorithms and I want to know the complexity of the Heap's algorithm that generate all possible permutations of n objects.
procedure generate(k : integer, A : array of any):
if k = 1 then
output(A)
else
// Generate permutations with kth unaltered
// Initially k == length(A)
generate(k - 1, A)
// Generate permutations for kth swapped with each k-1 initial
for i := 0; i < k-1; i += 1 do
// Swap choice dependent on parity of k (even or odd)
if k is even then
swap(A[i], A[k-1]) // zero-indexed, the kth is at k-1
else
swap(A[0], A[k-1])
end if
generate(k - 1, A)
end for
end if
I think the time complexity of this is O(n * n!) but I'm not sure. All the help would be appreciated. Thank you.