I wrote two generic class to implement these two algorithms, so you can use these classes to generate permutations and combinations for some use, such as software testing.
Using the Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace CombinationAlgorithmic
{
class Program
{
static void Main(string[] args)
{
ShowCombinations<int>(new int[] { 1, 2, 3, 4, 5 }, 3);
ShowPermutations<string>(new string[] { "dog", "cat", "bird", "bat" }, 4);
}
static void ShowPermutations<T>(T[] array, int length)
{
Permutations<T> permutations = new Permutations<T>(array, length);
Console.WriteLine("{0} Permutations of Array \"{1}\":\n", permutations.Count, GetString<T>(array));
foreach (IList<T> perm in permutations)
{
Console.WriteLine(GetString<T>(perm) + "\n");
}
}
static void ShowCombinations<T>(T[] array, int length)
{
Combinations<T> combinations = new Combinations<T>(array, length);
Console.WriteLine("{0} Combinations of Array \"{1}\":\n", combinations.Count, GetString<T>(array));
foreach (IList<T> oneCom in combinations)
{
Console.WriteLine(GetString<T>(oneCom) + "\n");
}
}
static string GetString<T>(IList<T> list)
{
StringBuilder sb = new StringBuilder();
foreach (T item in list)
{
sb.Append(item.ToString() + ",");
}
sb.Remove(sb.Length-1, 1);
return sb.ToString();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace CombinationAlgorithmic
{
class Program
{
static void Main(string[] args)
{
ShowCombinations<int>(new int[] { 1, 2, 3, 4, 5 }, 3);
ShowPermutations<string>(new string[] { "dog", "cat", "bird", "bat" }, 4);
}
static void ShowPermutations<T>(T[] array, int length)
{
Permutations<T> permutations = new Permutations<T>(array, length);
Console.WriteLine("{0} Permutations of Array \"{1}\":\n", permutations.Count, GetString<T>(array));
foreach (IList<T> perm in permutations)
{
Console.WriteLine(GetString<T>(perm) + "\n");
}
}
static void ShowCombinations<T>(T[] array, int length)
{
Combinations<T> combinations = new Combinations<T>(array, length);
Console.WriteLine("{0} Combinations of Array \"{1}\":\n", combinations.Count, GetString<T>(array));
foreach (IList<T> oneCom in combinations)
{
Console.WriteLine(GetString<T>(oneCom) + "\n");
}
}
static string GetString<T>(IList<T> list)
{
StringBuilder sb = new StringBuilder();
foreach (T item in list)
{
sb.Append(item.ToString() + ",");
}
sb.Remove(sb.Length-1, 1);
return sb.ToString();
}
}
}
output:
10 Combinations of Array "1,2,3,4,5":
Download the Source Project:1,2,3
1,2,4
1,2,5
1,3,4
1,3,5
1,4,5
2,3,4
2,3,5
2,4,5
3,4,5
24 Permutations of Array "dog,cat,bird,bat":
dog,cat,bird,bat
dog,cat,bat,bird
dog,bird,cat,bat
dog,bat,cat,bird
dog,bird,bat,cat
dog,bat,bird,cat
cat,dog,bird,bat
cat,dog,bat,bird
bird,dog,cat,bat
bat,dog,cat,bird
bird,dog,bat,cat
bat,dog,bird,cat
cat,bird,dog,bat
cat,bat,dog,bird
bird,cat,dog,bat
bat,cat,dog,bird
bird,bat,dog,cat
bat,bird,dog,cat
cat,bird,bat,dog
cat,bat,bird,dog
bird,cat,bat,dog
bat,cat,bird,dog
bird,bat,cat,dog
bat,bird,cat,dog
https://files.cnblogs.com/Dah/PermutationCombination.rar
Note: If you find any bug or have some suggestion, please send E-Mail to me or leave your comments here, thanks!