• 冒泡排序


    //int类型的冒泡排序
    using
    System; namespace 冒泡排序 { class Program { static void Sort(int[] sortArray) { bool swapped = true; do { 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); } static void Main(string[] args) { int[] sortArray = new int[] { 1, 5, 486, 887, 313, 33 }; Sort(sortArray); foreach (var temp in sortArray) { Console.Write(temp + " "); } Console.ReadKey(); } } }

    拓展通用的冒泡排序

    namespace _007_冒泡排序拓展 {
        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;
            }
        }
        class Program {
            static void Sort(int[] sortArray)
            {
                bool swapped = true;
                do 
                {
                    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);
            }
    
            static void CommonSort<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) {  
                Employee[] employees = new Employee[]
                {
                    new Employee("dsf",12), 
                    new Employee("435dsf",234), 
                    new Employee("234dsf",14), 
                    new Employee("ds234f",234), 
                    new Employee("dssfdf",90)
                };
                CommonSort<Employee>(employees,Employee.Compare);
                foreach (Employee em in employees)
                {
                    Console.WriteLine(em);
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    再谈PHP、Python与Ruby
    php消息队列
    创业如同追女生:成功的创业者都是追女生好手
    Windows 下 Python easy_install 的安装
    常用Python第三方库 简介
    Highcharts使用手册
    sqoop java api
    redis 读写分离
    linux 安装redis
    hive 安装和部署
  • 原文地址:https://www.cnblogs.com/jiangxiaoming/p/12986648.html
Copyright © 2020-2023  润新知