Enumerating All Permutations

Using library functions or recursion.

Using C++ next_permutation:

sort(arr.begin(), arr.end())
do {
    // process current permutation
} while (next_permutation(arr.begin(), arr.end()))

Using Python itertools:

from itertools import permutations
for perm in permutations(arr):
    # process perm

Manual recursion: Use the backtracking template with used array.

Time: O(n!n)O(n! \cdot n). There are n!n! permutations, each taking O(n)O(n) to process.

When feasible: n10n \leq 10 gives 3.6×106\leq 3.6 \times 10^6 permutations.