C# 學習筆記_委託(三)基於冒泡排序的拓展

class Program
    {
        /// <summary>
        /// 冒泡排序練習
        /// </summary>
        /// <param name="sortArray"></param>
        static void Sort(int[] sortArray)
        {
            bool swapped = true;
            int index = 0;

            do
            {
                index++;
                Console.WriteLine("---------第" + index + "次排序---------");
                foreach (var item in sortArray)
                {
                    Console.Write(item + ",");
                }
                Console.WriteLine("");
                swapped = false;
                for (int i = 0; i < sortArray.Length - 1; i++)
                {
                    if (sortArray[i] > sortArray[i + 1])
                    {
                        int temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }
            } while (swapped);

        }
        /// <summary>
        /// 冒泡排序拓展
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sortArray">排序數組</param>
        /// <param name="compareMethod">排序判斷</param>
        static void CommenSort<T>(T[] sortArray,Func<T,T,bool> compareMethod)
        {
            bool swapped = true;
            do
            {
                swapped = false;
                for (int i = 0; i < sortArray.Length - 1; i++)
                {
                    if (compareMethod(sortArray[i],sortArray[i+1]))
                    {
                        T temp = sortArray[i];
                        sortArray[i] = sortArray[i + 1];
                        sortArray[i + 1] = temp;
                        swapped = true;
                    }
                }
            } while (swapped);

        }
        static void Main(string[] args)
        {
            //int[] sortArray = new int[] { 3, 23, 42, 63, 45, 84, 105 };
            //Sort(sortArray);
            //foreach (var item in sortArray)
            //{
            //    Console.Write(item + "");
            //}

            Employee[] employees = new Employee[]
                {
                    new Employee ("a",123),
                    new Employee ("b",12),
                    new Employee ("c",1223),
                    new Employee ("d",23),
                    new Employee ("ab",423),
                    new Employee ("abc",313)
                };
            CommenSort<Employee>(employees, Employee.Compare);
            foreach (Employee item in employees)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadKey();
        }
    }

    class Employee
    {
        public string Name { get;private  set; }
        public int Salary { get; private set; }

        //構造方法
        public Employee(string name, int salary)
        {
            this.Name = name;
            this.Salary = salary;
        }

        public override string ToString()
        {
            return Name+":"+Salary;
        }
        /// <summary>
        /// Employee的排序判定
        /// </summary>
        public static bool Compare(Employee e1, Employee e2)
        {
            if (e1.Salary > e2.Salary) return true;

            return false;
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章