/// <summary> /// Testing the method <see cref="PermutationGenerator.Subset"/>. /// </summary> [Test] public void Subset() { int[] array = new int[100]; for (int i = 0; i < array.Length; i++) { array[i] = i; } // testing 30 < 100 / 2 ICollection subset = PermutationGenerator.Subset(array, 30); int lastItem = -1; foreach (int item in subset) { Assertion.Assert("#A00 The order of the elements is not preserved.", item > lastItem); lastItem = item; } // testing 60 > 100 / 2 subset = PermutationGenerator.Subset(array, 60); lastItem = -1; foreach (int item in subset) { Assertion.Assert("#A00 The order of the elements is not preserved.", item > lastItem); lastItem = item; } }
/// <summary> /// Gets an array of <c>k</c> indices picked uniformly drawn from the /// interval <c>[0, n-1]</c>. /// </summary> public static int[] Next(int n, int k) { if (n < 0 || k < 0) { throw new ArgumentException( "#E00 n and k must be non negative integer."); } if (k > n) { throw new ArgumentException( "#E01 n must be greater or equal to k."); } /* Current implementation relies of the permutation generator and * therefore requires a O(n*log(n)) computation time to run. * * This complexity could be significantly improved, especially for * small k. */ int[] permutation = PermutationGenerator.Next(n); int[] combination = new int[k]; for (int i = 0; i < k; i++) { combination[i] = permutation[i]; } return(combination); }
/// <summary> /// Testing the method <see cref="PermutationGenerator.Shuffle"/>. /// </summary> [Test] public void Suffle() { int[] array = new int[100]; for (int i = 0; i < array.Length; i++) { array[i] = i; } PermutationGenerator.Shuffle(array); Array.Sort(array); for (int i = 0; i < array.Length; i++) { Assertion.AssertEquals("#A00 Unexpected array value", i, array[i]); } }