``` // Output the k! permutations of A in which the first k elements are permuted in all ways. // To get all permutations of A, use k := length of A. // // If k > length of A, will try to access A out of bounds. // If k <= 0 there will be no output (empty array has no permutations) procedure permutations(k : integer, A : array of any): if k = 1 then output(A) else // permutations with last element fixed permutations(k - 1, A) // permutations with last element swapped out for i := 0; i < k-1; i += 1 do if k is even then swap(A[i], A[k-1]) else swap(A[0], A[k-1]) end if permutations(k - 1, A) end for end if ```