1. 程式人生 > >c#-氣泡排序與通用委託

c#-氣泡排序與通用委託

    class Employee
    {
        public string name;
        public int salary;

        public static bool SortSalary(Employee e0,Employee e1)//把這個方法作為委託傳到排序方法中可實現通用排序
        {
            if (e0.salary<e1.salary)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public Employee(string name , int salary)
        {
            this.name = name;
            this.salary = salary;
        }
        public override string ToString()
        {
            return name + ":" + salary;
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 氣泡排序的優化
{
    /// <summary>
    /// 通過新增一個do...while迴圈
    /// 利用迴圈中發生了交換就改變swapper為真,否則為false來防止當排完序後不會又重複迴圈
    /// 升級通用排序,思想是利用傳遞一個委託方法作為引數,放在if中的條件判斷中來比較物件之間的排序
    /// 在這裡舉個例子新增一個employee類,有name和salary屬性,利用salary進行排序
    /// 多播委託就是一個委託可以攜帶多個方法一起呼叫,就是廣播的性質一樣
    /// 呼叫一個委託可以呼叫方法鏈,但是如果方法鏈中某一個方法丟擲了異常,在呼叫終止
    /// 方法鏈中只有最後一個方法的返回值可以被返回,不過一般都為void
    /// 呼叫可以利用GetInvocationList返回一個委託陣列遍歷分開來一個一個呼叫,還可以傳形參
    /// </summary>
    class Program
    {
        private static bool swapper = true;
        static void sortArray(int[] arrays)
        {
            do
            {
                swapper = false;
                for (int i = 0; i < arrays.Length-1; i++)
                {
                    if (arrays[i]>arrays[i+1])
                    {
                        var temp = arrays[i];
                        arrays[i] = arrays[i+1];
                        arrays[i + 1] = temp;
                        swapper = true; 
                    }
                }
            } while (swapper);
        }

        static void genericSortArray<T>(T[] arrays,Func<T,T,bool> compareMethod)
        {
            do
            {
                swapper = false;
                for (int i = 0; i < arrays.Length - 1; i++)
                {
                    if (compareMethod(arrays[i],arrays[i+1]))
                    {
                        var temp = arrays[i];
                        arrays[i] = arrays[i + 1];
                        arrays[i + 1] = temp;
                        swapper = true;
                    }
                }
            } while (swapper);
        }

        /// <summary>
        /// 多播委託
        /// </summary>

        public static void printName()
        {
            Console.WriteLine("Miku");
          //  throw new Exception();//程式會中斷,什麼都輸入不了
        }
        public static void printotherName()
        {
            Console.WriteLine("軒墨");
        }
                
        static void Main(string[] args)
        {
            //int[] myArrays = new int[] { 12, 4, 1525, 242, 1 };
            //sortArray(myArrays);
            //foreach (var i in myArrays)
            //{
            //    Console.Write(i + " ");
            //}
            Employee[] myEmployees = new Employee[]
            {
                new Employee("ice",100),
                new Employee("icecold",200),
                new Employee("icecold",20),
                new Employee("icecold",30),
            };
            genericSortArray<Employee>(myEmployees,Employee.SortSalary);
            foreach (var e in myEmployees)
            {
                Console.WriteLine(e);
            }
            Action temp;
            temp = printName;
            temp += printotherName;
            //temp();
            Delegate[] dels = temp.GetInvocationList();
            foreach (Delegate del in dels)
            {
                del.DynamicInvoke();//利用這個分開呼叫多播委託
            }
            Console.ReadKey();
        }
    }
}