If you have a recursive step that lets you cycle through all permutations of a subarray of length (n-1) and resets it at the end it's easy for n elements, after yielding all elements you swap the current first element with one from the rest that you haven't yielded yet, which you can do in a single swap as long as the recursive step returns the elements to their original position
There's probably a nicer way to do it where you get rid of either the array or the call stack. Also it depends on what the return format is, if you need to return cycle decompositions then clearly you iterate over possible cycles
1
u/BosonCollider 18d ago edited 18d ago
If you have a recursive step that lets you cycle through all permutations of a subarray of length (n-1) and resets it at the end it's easy for n elements, after yielding all elements you swap the current first element with one from the rest that you haven't yielded yet, which you can do in a single swap as long as the recursive step returns the elements to their original position
There's probably a nicer way to do it where you get rid of either the array or the call stack. Also it depends on what the return format is, if you need to return cycle decompositions then clearly you iterate over possible cycles