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[] 12345 }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
-11);
            
return sb.ToString();
        }

    }

}



output:
10 Combinations of Array "1,2,3,4,5":

 

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

Download the Source Project:
http://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!
Posted on 2006-11-25 16:21 Adrian H. 阅读(1697) 评论(9)  编辑 收藏 所属分类: Algorithm

Feedback

#1楼    回复  引用    

2006-12-14 22:25 by Stef [未注册用户]
Hello there!

thnx for that code.. is easy to include in other projects and works perfectly! Great work!

#2楼    回复  引用    

2007-02-07 22:28 by hiber [未注册用户]
if there are some duplicate items, Does your permutation algorithm still work?

for example:
ShowPermutations<string>(new string[] { "dog", "dog", "dog", "cat" }, 4);

I thinks you should pay more attention to this issue.

#3楼    回复  引用  查看    

2007-02-10 16:56 by Adrian      
To Hiber:
No, it assumes all the items are all different, if there are some duplicate items, the algorithm will output duplicates too.

#4楼    回复  引用  查看    

2007-03-18 12:53 by 农夫三拳      
偶以为自己写的组合,排列生成,寒-_-#

#5楼    回复  引用    

2007-04-14 16:00 by 方理军 [未注册用户]
这里真好!
这个程序帮了我的忙了!
希望能与更多计算机的朋友讨论算法!!
http://hi.baidu.com/fanglijun
E-mail:zixinde99@163.com

#6楼    回复  引用    

2007-04-14 16:01 by 方理军 [未注册用户]
great!!!

#7楼    回复  引用    

2008-03-14 11:12 by happy100 [未注册用户]
不错,但是注释太少了.

#8楼    回复  引用  查看    

2008-07-10 12:23 by 林子      
GOOD

#9楼    回复  引用    

2008-08-22 09:51 by liuyibao [未注册用户]
很好.帮我了一个忙.高手呀.我的QQ86883127

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-11-25 16:23 编辑过
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: