探讨如何用委托处理排序

所属分类: 软件编程 / C#教程 阅读数: 133
收藏 0 赞 0 分享

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class 冒泡排序
    {
        //首先要了解冒泡排序,其实很简单就是索引前面的跟后面的比较,如果比后面的大2个值的位置就进行调换
        static void Main()
        {
            int[] str ={ 0, 14, 3, 6, 1, 30, 10, 9, 28 };
            for (int i = 0; i < str.Length; i++)
            {
                for (int j = i + 1; j < str.Length; j++)
                {
                    if (str[j] < str[i])
                    {
                        int index = str[i];
                        str[i] = str[j];
                        str[j] = index;
                    }
                }
            }
            for (int m = 0; m < str.Length; m++)
            {
                Console.WriteLine(str[m]);
            }
        }
    }
}


复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public delegate bool DelegateTest(object obj1, object obj2);
    class Class1
    {
        static void Main()
        {
            Employee[] Employees =
              {
                  new Employee("huguo",1000000),
                  new Employee("lili",20000),
                  new Employee("lulu",30000),
                  new Employee("xixi",50000),
                  new Employee("jianjian",10000),
                  new Employee("yoyo",9000)
              };
            //委托DelegateTest代理的方法是Greate
            DelegateTest MyTest = new DelegateTest(Employee.Greate);
            Sorter MySort = new Sorter();
            //冒泡算法中第一个参数是对应Employees数组信息,第二个参数是委托
            MySort.Sort(Employees, MyTest);
            for (int m = 0; m < Employees.Length; m++)
            {
                Console.WriteLine(Employees[m].ToString());
            }
        }
    }
    class Employee
    {
        public string Name;
        public int Salary;
        public Employee(string Name, int Salary)
        {
            this.Name = Name;
            this.Salary = Salary;
        }
        //用override重写string方法
        public override string ToString()
        {
            return string.Format(Name + ",{0:C},", Salary);
        }
        //定义一个方法,如果obj2传过来的 Salary大于obj1就返回true;
        public static bool Greate(object obj1, object obj2)
        {
            Employee Employee1 = (Employee)obj1;
            Employee Employee2 = (Employee)obj2;
            return (Employee2.Salary > Employee1.Salary) ? true : false;
        }
    }
    class Sorter
    {
        public void Sort(object[] ArrayObj, DelegateTest Test)
        {
            //下面就是冒泡算法啦
            for (int i = 0; i < ArrayObj.Length; i++)
            {
                for (int j = i + 1; j < ArrayObj.Length; j++)
                {
                    if (Test(ArrayObj[j], ArrayObj[i]))
                    {
                        object Temp = ArrayObj[i];
                        ArrayObj[i] = ArrayObj[j];
                        ArrayObj[j] = Temp;
                    }
                }
            }
        }
    }

}


更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多