I am a beginner at C# and I am looking for a code to return all possible permutations of a set, for example {1, 1, 2}, without repetition: {112, 121, 211}. I found the following link but I do not know how to use it.
http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G
The code I have tried is as follows. When I try to get permutations of "111" for example, it returns all possible permutations with repetition, i.e. six 111s. But I am looking for something providing permutations without repetition. In more details, 111 is just one permutation not six.
class Program
{
private static void Swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public static void GetPer(char[] list)
{
int x = list.Length - 1;
GetPer(list, 0, x);
}
private static void GetPer(char[] list, int k, int m)
{
if (k == m)
{
Console.Write(list);
}
else
for (int i = k; i <= m; i++)
{
Swap(ref list[k], ref list[i]);
GetPer(list, k + 1, m);
Swap(ref list[k], ref list[i]);
}
}
static void Main()
{
string str = "sagiv";
char[] arr = str.ToCharArray();
GetPer(arr);
}
}