1

I have an arraylist which contains some objects and i have to get permutation of that objects?How can i do that? Suppose MyList is an arraylist which contains 4 objects.

ArrayList myList = new ArrayList();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);

so arraylist count is 4 so i want 4!=24 I want 24 permutations of that objects. How can i do That in C#.Please help me.

Thanks!

3
  • 1
    Just Google for an example on permutations and combination's. Not that hard... this has been asked 100's of times before. Commented Jan 18, 2010 at 9:43
  • Let me google that for you: tinyurl.com/yhnwjd5 Commented Jan 18, 2010 at 9:44
  • it's good that you've marked the question with 'homework' tag Commented Jan 18, 2010 at 9:54

5 Answers 5

1

This Stanford lecture from the class "Programming Abstractions" explains a recursive solution really well.

http://www.youtube.com/watch?v=uFJhEPrbycQ#t=37m25s

Sign up to request clarification or add additional context in comments.

Comments

0

May be like this, not tested though.

public static IEnumerable<string> permute(string s){
    if (s.Count() > 1)
        return from c in s
               from p in permute(s.Remove(s.IndexOf(c), 1))
               select string.Format("{0}{1}", c, p);
    else
        return new string[] { s };
}

Comments

0

Here is a nice article going in depth of C++ implementation of next_permutation. Yes, it's in C++ but the syntax is not much different, and explanation is good enough. Cheers.

1 Comment

can't find that link anymore. That one seems to be a good one as well though: wordaligned.org/articles/next-permutation
0

You can do this with lovely lovely recursion.

The base case: the permutation of an array of size 1 being the array itself.

The recursive case: the permutation of the array of size n being, each permutation of size (n - 1) with the nth item added at each possible position.

Does that make sense?

Comments

0

Take a look into this library: Permutations, Combinations, and Variations using C# Generics

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.