Implementation of the Johnson–Trotter algorithm to generate permutations.