private static void Permute(PermutationSpace space, byte[] word, int start, int end) { if (start == end) { space.AddWord(word); return; } for (int i = start; i <= end; i++) { PermutationSpace.SwapWord(word, start, i, 1); Permute(space, word, start + 1, end); PermutationSpace.SwapWord(word, start, i, 1); } }
private static void RunTestCase(string word) { Console.Error.WriteLine($"== Running test case \"{word}\""); Stopwatch watch = new Stopwatch(); byte[] encodedWord = Encoding.ASCII.GetBytes(word); Console.Error.WriteLine("== Creating Permutation Space"); PermutationSpace space = new PermutationSpace(encodedWord.Length); Console.Error.WriteLine("== Creating Permutations"); watch.Restart(); Permute(space, encodedWord); watch.Stop(); Console.Error.WriteLine($"== Time to permute: {watch.Elapsed.TotalMilliseconds}ms"); Console.Error.WriteLine("== Sorting & Printing"); watch.Restart(); space.PrintPermutations(printDuplicates: false); watch.Stop(); Console.Error.WriteLine($"== Time to sort and print: {watch.Elapsed.TotalMilliseconds}ms"); }
private static void Permute(PermutationSpace space, byte[] word) { Permute(space, word, 0, word.Length - 1); }