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();
            }
        }
    }
  • 相关阅读:
    60)模板类去派生新的子类
    linux ssh搭建
    javascript学习6-练习之3二分查找算法
    javascript学习5-练习之2冒泡排序算法
    javascript学习4-练习之1转置矩阵
    javascript学习3-自定义函数
    计算机网络学习1-网络层次
    javascript学习-安全初探之沙箱
    html学习1-html5基础学习
    jquery学习之1.23-ajax使用
  • 原文地址:https://www.cnblogs.com/jiangxiaoming/p/12986648.html
Copyright © 2011-2022 走看看