Permutation

  • 394
  • 0
  • 2014-08-09

Permutation

{
    var keys = "ABC123".ToCharArray();
    var buf = new char[3];
    Perm(buf, 0, keys, a => Console.WriteLine(new string(a)));
    Console.ReadLine();
}
static void Swap<T>(IList<T> list, int i1, int i2)
{
    if (i1 == i2) return;
    var o = list[i1];
    list[i1] = list[i2];
    list[i2] = o;
}
static void Perm<T>(T[] buf, int index, IList<T> list, Action<T[]> callback)
{
    if (index == buf.Length)
    {
        callback(buf);
        return;
    }
    for (var i = index; i < list.Count; i++)
    {
        Swap(list, index, i);
        buf[index] = list[index];
        Perm(buf, index+1, list, callback);
        Swap(list, index, i);
    }
}