zoukankan      html  css  js  c++  java
  • 冒泡排序

    //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();
            }
        }
    }
  • 相关阅读:
    Python基础5_字典,集合
    Python基础3_基本数据类型,字符串,for循环
    Python基础2_while循环,格式化输出,基本运算符,编码,
    Python基础1_初识,注释,变量,if语句
    编写高质量代码[读书笔记]
    php地方天气
    [head first php&mysql]读书笔记-基本的安全信息(第五章)
    上传本地图片
    检测IE
    underscore源码解析(实用的功能)
  • 原文地址:https://www.cnblogs.com/jiangxiaoming/p/12986648.html
Copyright © 2011-2022 走看看