欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

探讨如何用委托处理排序

程序员文章站 2023-12-19 13:11:46
复制代码 代码如下:using system;using system.collections.generic;using system.text; namespace...

复制代码 代码如下:

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;
                    }
                }
            }
        }
    }

}


上一篇:

下一篇: