I'm having trouble figuring out how to implement this algorithm. The program I'm working on currently works fine but I need to implement this algorithm to get my data processing optimal.

What I'm trying to do is simply iterate every possible order combination of a given set of objects. So let's say I have 5 tiles (A, B, C, D, E) and they are arranged sequentially. I want to loop over the number of different order combinations that are possible using these 5 tiles (or 'n' tiles in algorithmic form).

So you'd get an output like this:

A, B, C, D, E
B, C, D, E, A
C, D, E, A, B
E, D, C, B, A

... and so on.

Picture the tiles as Enums or Objects within a program and their order position as an index into an array. I'm not interested in duplicating anything (A, A, A, B, B) - it's literally a shuffling of existing tiles so they remain unique.

I've been pulling my hair out on this all day - can anyone solve this?

Thanks.