排序类

程序员在日程编程中常常碰到对数组进行各种各样的排序,但是又不可能记住每个排序算法的细节,因此不得不找资料、上网查实现细节,很是麻烦,若有时资料不在身边、不能上网,那就更麻烦,编程不得不中断。
因此本人以c#实现了一个排序类Sorter,能对不同类型的对象数组进行各种排序,其中用户可以选择不同的排序,有很大的扩充和灵活性。代码初步测试通过,朋友若在使用过程中发现错误,请不吝指正。代码如下:
namespace Yangxl
{
    /// <summary>
    /// 排序算法,以枚举方式列出可行的排序算法,现只实现冒泡法。
    /// </summary>
    public enum KindOfMethod { BubbleSort, m2, m3 };
    /// <summary>
    /// 排序方式的委托定义,规范排序方式的签名。
    /// </summary>
    /// <param name="left">左排序元素</param>
    /// <param name="right">右排序元素</param>
    /// <returns>大小判定结果</returns>
    public delegate bool SortStyleDelegate(object left, object right);
    /// <summary>
    /// 排序算法的委托定义。
    /// </summary>
    /// <param name="objs">被排序的数据</param>
    /// <param name="style">排序方式的委托对象,如升序、降序</param>
    public delegate void SortMethodDelegate(Array objs, SortStyleDelegate style);

    public class Sorter
    {
        /// <summary>
        /// 根据枚举变量得到排序算法的委托变量
        /// </summary>
        /// <param name="method">排序算法的枚举变量</param>
        /// <returns>排序算法委托变量</returns>
        private static SortMethodDelegate GetKindOfMethod(KindOfMethod method)
        {
            SortMethodDelegate sortMethod = null;
            switch (method)
            {
                case KindOfMethod.BubbleSort:
                    sortMethod = new SortMethodDelegate(BubbleSort);
                    break;
                case KindOfMethod.m2:
                    break;
                case KindOfMethod.m3:
                    break;
                default:
                    break;
            }
            return sortMethod;
        }

        /// <summary>
        /// 静态形式的排序实现
        /// </summary>
        /// <param name="objs">被排序的数据对象</param>
        /// <param name="method">排序算法的枚举对象</param>
        /// <param name="sortStyle">排序方式的委托对象,用户应在外部实现排序的方式,并定义排序方式的委托变量</param>
        public static void Sort(Array objs, KindOfMethod method,SortStyleDelegate sortStyle)
        {
            SortMethodDelegate sortMethod = GetKindOfMethod(method);
            sortMethod(objs, sortStyle);
        }

        /// <summary>
        /// 冒泡排序算法
        /// </summary>
        /// <param name="objs">别排序数组对象</param>
        /// <param name="style">排序方式的委托对象</param>
        private static void BubbleSort(Array objs, SortStyleDelegate style)
        {
            for (int i = 0; i < objs.Length; i++)
            {
                for (int j = i + 1; j < objs.Length; j++)
                {
                    if (!style(objs.GetValue(i), objs.GetValue(j)))
                    {
                        object tmp = objs.GetValue(i);
                        objs.SetValue(objs.GetValue(j), i);
                        objs.SetValue(tmp,j);
                    }
                }
            }
        }
    }
}
用例测试如下:
一个简单的测试类Account,实现了大小比较:
namespace SortTest
{
    class Account
    {
        public string name;
        public double balance;

        public Account(string name, double balance)
        {
            this.name = name;
            this.balance = balance;
        }

        public static bool Greater(object left, object right)
        {
            return ((Account)right).balance > ((Account)left).balance;
        }

        public override string ToString()
        {
            return string.Format("{0}:{1}", this.name, this.balance);
        }
    }
}
Main函数测试如下:
namespace SortTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Account[] accounts = new Account[5]{new Account("yangxl",500.0),new Account("chenwy",400.0),
                new Account("huangq",300.0),new Account("yaol",200.0),new Account("wuhq",100.0)};
            OutPut(accounts);
            SortStyleDelegate sortStyle = new SortStyleDelegate(Account.Greater);
            Sorter.Sort(accounts, KindOfMethod.BubbleSort, sortStyle);
            OutPut(accounts);
            Console.ReadLine();
            return;
        }

        private static void OutPut(Account[] objs)
        {
            for (int i = 0; i < objs.Length; i++)
            {
                Console.Write(objs[i].ToString());
                Console.Write(' ');
            }
            Console.WriteLine();
        }
    }
}
测试代码比较简单就不加注解。
posted @ 2007-05-18 17:31  寒江独钓客  阅读(179)  评论(0编辑  收藏  举报