BacktrackingAnimated
Permutation Generation
Build arrangements by choosing an unused item at each position.
Time
O(n! · n)
Extra space
O(n)
Use it for
All orderings.
How it works
- Build the permutation one position at a time.
- At each position try every item that has not been used yet.
- When all positions are filled, record the permutation.
- Backtrack: put the last item back and try the next one. n! results.
Watch it run
Loading animation…