1: public static class Permutation
2:
3: {
4:
5: #region Public Static Functions
6:
7:
8:
9: /// <summary>
10:
11: /// Finds all permutations of the items within the list
12:
13: /// </summary>
14:
15: /// <typeparam name="T">Object type in the list</typeparam>
16:
17: /// <param name="Input">Input list</param>
18:
19: /// <returns>The list of permutations</returns>
20:
21: public static List<List<T>> Permute<T>(List<T> Input)
22:
23: {
24:
25: List<T> Current=new List<T>();
26:
27: Current.AddRange(Input);
28:
29: List<List<T>> ReturnValue = new List<List<T>>();
30:
31: int Max = MathHelper.Factorial(Input.Count - 1);
32:
33: for (int x = 0; x < Input.Count; ++x)
34:
35: {
36:
37: int z = 0;
38:
39: while (z < Max)
40:
41: {
42:
43: int y = Input.Count - 1;
44:
45: while (y > 1)
46:
47: {
48:
49: T TempHolder = Current[y - 1];
50:
51: Current[y - 1] = Current[y];
52:
53: Current[y] = TempHolder;
54:
55: --y;
56:
57: List<T> TempList = new List<T>();
58:
59: TempList.AddRange(Current);
60:
61: ReturnValue.Add(TempList);
62:
63: ++z;
64:
65: if (z == Max)
66:
67: break;
68:
69: }
70:
71: }
72:
73: if (x + 1 != Input.Count)
74:
75: {
76:
77: Current.Clear();
78:
79: Current.AddRange(Input);
80:
81: T TempHolder2 = Current[0];
82:
83: Current[0] = Current[x + 1];
84:
85: Current[x + 1] = TempHolder2;
86:
87: }
88:
89: }
90:
91: return ReturnValue;
92:
93: }
94:
95:
96:
97: #endregion
98:
99: }