Fork me on GitHub
Fun<>,匿名方法,Lambda表达式 冒泡排序C#

冒泡排序C#实现,使用委托,包括三种方式:Fun<>,匿名方法,Lambda表达式

冒泡排序是一种简单的排序方法,适合于小量数字排序,对于大量数字(超过10个),还有更高效的排序方法。
这里的实现的冒泡排序,需实现功能:
不仅数字排序,还要对任意对象排序

示例:

  • 对People对象的Age(年龄)排序
  • 对Student对象的Score(分数)排序

People:

    public class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public People(string name, int age)
        {
            Name = name;
            Age = age;
        }
        public static bool CompareAge(People p1, People p2)
        {
            return p1.Age < p2.Age;
        }
        public override string ToString()
        {
            return string.Format("[people] name:{0},age:{1}", Name, Age);
        }
    } 

Student:

    public class Student
    {
        public string Name { get; set; }
        public float Score { get; set; }
        public Student(string name, float score)
        {
            Name = name;
            Score = score;
        }
        public static bool CompareScore(Student s1, Student s2)
        {
            return s1.Score < s2.Score;
        }
        public override string ToString()
        {
            return string.Format("[student] name:{0},score:{1}",Name,Score);
        }
    } 

冒泡排序的实现,如果对Fun<>的委托写法不懂,最后有说明。

    public static class Storter_Fun
    {
        public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparision)
        {
            bool swapped = true;
            do
            {
                swapped = false;
                for (int i = 0; i < sortArray.Count - 1; i++)
                {
                    if (comparision(sortArray[i + 1], sortArray[i]))
                    {
                        T temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }
            } while (swapped);
        }
    }  

控制台,使用排序:

        static void Main(string[] args)
        {
            //人按年龄排序
            People[] peoples = {new People("张三",43),
                               new People("李四",12),
                               new People("王五",50),
                               new People("吴六",21),
                               new People("陈七",33),};
            //Fun<>委托的写法
            BubbleSorter.Storter_Fun.Sort<People>(peoples, People.CompareAge);
            //匿名方法的写法
            BubbleSorter.Storter_Fun.Sort<People>(peoples, delegate(People p1, People p2)
            {
                return p1.Age < p2.Age;
            });
            //Lambdah表达式的写法
            BubbleSorter.Storter_Fun.Sort<People>(peoples, (People p1, People p2) =>
            {
                return p1.Age < p2.Age;
            });
            foreach (var people in peoples)
            {
                Console.WriteLine(people);
            }
            //学生按分数排序
            Student[] students = {new Student("张三",80.5F),
                               new Student("李四",85),
                               new Student("王五",88),
                               new Student("吴六",70),
                               new Student("陈七",95),};
            BubbleSorter.Storter_Fun.Sort<Student>(students, Student.CompareScore);
            foreach (var student in students)
            {
                Console.WriteLine(student);
            }
            Console.ReadKey();
        }  

委托:调用方法的参数一般情况是数值型的,例如int,string,DataTable等等,能不能将方法本身作为一个参数传递给另一个方法呢?委托就是解决这个问题的。
既然是参数,要就有类型。C#是强类型,方法的参数是int类型,就不能传string类型进去,否则在编辑阶段就报错了。
int,string等是已有的类型,委托的类型就需要自定义。
例如上面例子用到的:

  • Fun<t, t,="" bool=""> 代表方法是传入两个泛型参数,返回bool;
  • Fun<int,string,datatable,array> 代表方法是传入三个参数:int, string,DataTable,返回Array;
    就是最后一个参数为返回值类型;
    那么如果没有返回值呢?
  • Action<t,t> 代表方法是传入两个泛型参数,没返回值
  • Action<int,string> 代表方法是传入两个参数:int,string,没返回值
    定义了委托的类型,传入的方法必须要符合这个定义,即:传入参数,类型,个数,顺序,返回值要一致,否则编译不过,C#是强类型的原因。

刚实现的冒泡可以支持不同对象的排序,主要是将冒泡比较大小方法传递进来(委托),与交换数据的临时变量使用了泛型实现;

 
分类: C#
posted on 2015-04-28 22:47  HackerVirus  阅读(932)  评论(0编辑  收藏  举报