Generates all possible permutations of the input array or string using [Heap's algorithm](https://en.wikipedia.org/wiki/Heap%27s_algorithm), which minimizes movement.