0

I would like to get all possible combinations of numbers which are stored in an array.

For example:

Using a first array of {1,2,3,4} return

1,2,3,4
1,3,4,2
1,4,2,3
2,3,4,1
2,4,1,3 etc

How could I do this?

Just to clarify - Take an array of numbers: {1,2,3,4}, rearrange the sequence of numbers, return a new array (add to a list), and repeat until all possible combinations have been found.

Thanks

dave88
  • 312
  • 5
  • 14

2 Answers2

1

Here is a answer that contains exactly what you want. If you read the other answers there are several other approaches that work faster, but they are a little more complicated to use.

This is the relevant code, not my own.

public static IEnumerable<IEnumerable<T>> QuickPerm<T>(this IEnumerable<T> set)
{
    int N = set.Count();
    int[] a = new int[N];
    int[] p = new int[N];

    var yieldRet = new T[N];

    List<T> list = new List<T>(set);

    int i, j, tmp; // Upper Index i; Lower Index j

    for (i = 0; i < N; i++)
    {
        // initialize arrays; a[N] can be any type
        a[i] = i + 1; // a[i] value is not revealed and can be arbitrary
        p[i] = 0; // p[i] == i controls iteration and index boundaries for i
    }
    yield return list;
    //display(a, 0, 0);   // remove comment to display array a[]
    i = 1; // setup first swap points to be 1 and 0 respectively (i & j)
    while (i < N)
    {
        if (p[i] < i)
        {
            j = i%2*p[i]; // IF i is odd then j = p[i] otherwise j = 0
            tmp = a[j]; // swap(a[j], a[i])
            a[j] = a[i];
            a[i] = tmp;

            //MAIN!

            for (int x = 0; x < N; x++)
            {
                yieldRet[x] = list[a[x]-1];
            }
            yield return yieldRet;
            //display(a, j, i); // remove comment to display target array a[]

            // MAIN!

            p[i]++; // increase index "weight" for i by one
            i = 1; // reset index i to 1 (assumed)
        }
        else
        {
            // otherwise p[i] == i
            p[i] = 0; // reset p[i] to zero
            i++; // set new index value for i (increase by one)
        } // if (p[i] < i)
    } // while(i < N)
}

Using this extension method, you could do array.QuickParm.Select(innerEnum => innerEnum.ToArray()).ToArray() to get the result from this as an array of arrays.

Community
  • 1
  • 1
tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
-1
Byte[,] square4 = new Byte[,] { { 1, 2, 3, 4 }, { 1, 3, 4, 2 }, { 2, 3, 4, 1 }, { 2, 4, 1, 3 } };
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • Hey down vote. Look at the question before edits. And the comment. – paparazzo Sep 18 '14 at 13:12
  • 3
    Was not me, but I think the OP wants to use `{1, 2 ,3 4}` as *input* to *generate* programmatically what you have "hard coded". – crashmstr Sep 18 '14 at 13:45
  • @crashmstr I get that now that the OP has updated. But it is not fair to vote me down when in the original question it was not clear if this was just about the syntax to load a two dimensional array. – paparazzo Sep 18 '14 at 14:08
  • 1
    @Blam Why is your answer still here if it doesn't answer the question as currently written? If you want to get rid of a downvote, delete the answer. – tom.dietrich Sep 18 '14 at 18:35
  • @Blam so your position is that an answer that you know is incorrect should continue to exist on a question and not receive downvotes, and that viewers of the site should look at the history of the question to figure out why this incorrect answer is on this question and not downvote it? Just delete your answer, or correct it so it actually is a correct answer. Your rep will be restored. – tom.dietrich Sep 18 '14 at 18:51
  • @tom.dietrich But the question got the downvote before the edit. My position is the question is still not clear. If nothing else this shows the question if not about array syntax. Can you tell me what "return a new array (add to a list)" means. Even the syntax is still not clear. – paparazzo Sep 18 '14 at 19:01
  • To be fair I never mentioned anything about wanting to set up a byte. I amended the question after I found out the thing I'm looking at was permutations. – dave88 Sep 18 '14 at 19:04
  • @Blam it doesn't really matter at this point, does it? Just delete your answer. It isn't adding anything of any measurable value. – tom.dietrich Sep 18 '14 at 19:05
  • @dave88 Byte is is a number. "The array will be added to a list. ... How can I do this?" – paparazzo Sep 18 '14 at 19:20
  • That clearly wasn't the primary query. That was for clarity as to what I would be doing with the new array to avoid the inevitable question "why in an array?". Besides, how is what you posted even correct for adding an array to a list? arrList.add(newArr) – dave88 Sep 18 '14 at 20:10
  • Exactly "return a new array (add to a list)" is not clear. Let it go. I did not vote you down. – paparazzo Sep 18 '14 at 21:04