3

How do I generate all possible permutations of a list of numbers in C?

As an example, [1, 8, 12] would generate

[1, 12, 8],
[12, 8, 1],
[12, 1, 8], ...
Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
pablouche
  • 6,961
  • 3
  • 16
  • 6
  • Do you mean all permutations? – Andreas Brinck Nov 24 '10 at 19:39
  • Are you asking for an algorithm to find all of the permutations of a list? All combinations? – Christian Mann Nov 24 '10 at 19:39
  • What exactly do you mean by "generate all the numbers possible"? What have you tried? What's going wrong? If this is homework, please use the 'homework' tag. – aschepler Nov 24 '10 at 19:40
  • I am not sure what exactly your problem is. You got a list of numbers and want to list all ways of ordering these numbers? Or combining them each other to tuples, or tripels or whatnot? Could you be a bit more specific on your problem, please? – Jan Thomä Nov 24 '10 at 19:40
  • possible duplicate of [Algorithm to generate all possible permutations of a list?](http://stackoverflow.com/questions/2710713/algorithm-to-generate-all-possible-permutations-of-a-list) – Mark Elliot Nov 24 '10 at 20:04

1 Answers1

2

Have a look at this Johnson-Trotter Algorithm and applet it is exactly what you want.

Vishal
  • 12,133
  • 17
  • 82
  • 128