标签:sort 大于 eth name ring namespace this ++ adk
//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(); } } }
标签:sort 大于 eth name ring namespace this ++ adk
原文地址:https://www.cnblogs.com/jiangxiaoming/p/12986648.html