(第三季)205-int类型的冒泡排序 206-拓展的通用的冒泡排序方法

来源:互联网 发布:淘宝售后管理软件 编辑:程序博客网 时间:2024/05/16 00:53
/**/using System;namespace CatCry{    class MainClass    {        // int类型排序        static void Sort(int[] sortArray)        {            bool swap = false;            do            {                swap = 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;                        swap = true;                    }                }            } while (swap);        }        // 泛型方法        static void CommonSort<T>(T[] sortArray, Func<T, T, bool> compareMethod)        {            bool swap = false;            do            {                swap = 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;                        swap = true;                    }                }            } while (swap);        }        static void Main()        {            //int[] sortArray = new int[] {1, 2, 3, 6, 4, 7, 8, 9};            //Sort(sortArray);            //foreach (var item in sortArray)            //{            //    Console.WriteLine(item);            //}            //            Employee[] employees = new Employee[]                {                    new Employee("dsf", 12),                    new Employee("dsdf", 122),                    new Employee("dsf", 127),                    new Employee("dsaf", 127),                    new Employee("dsvgf", 124),                    new Employee("dfdsdsf", 143),                    new Employee("dsaggf", 133),                    new Employee("dssdfdsf", 132),                };            CommonSort<Employee>(employees, Employee.Compare);            foreach (var item in employees)            {                Console.WriteLine(item.ToString());            }            //            Console.ReadKey();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CatCry{    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;        }        // 如果e1 > e2 的话 返回true 否则返回false        public static bool Compare(Employee e1, Employee e2)        {            if (e1.Salary > e2.Salary)            {                return true;            }            return false;        }        public override string ToString()        {            return Name + ":" + Salary;        }    }}


0 0