直接上代码,代码比较简单
1 class Program 2 { 3 private delegate string GetAString(); 4 static void Main(string[] args) 5 { 6 7 int[] a = { 0, 5, 6, 2, 1 }; 8 BubbleSorter.Sort(a, BubbleSorter.Compa); 9 foreach (var item in a) 10 { 11 Console.Write(item); 12 } 13 Console.ReadLine(); 14 } 15 16 public static void SortA(int[] sortArray) 17 { 18 bool swapped = true; 19 do 20 { 21 swapped = false; 22 for (int i = 0; i < sortArray.Length - 1; i++) 23 { 24 if (sortArray[i] < sortArray[i + 1]) 25 { 26 int temp = sortArray[i]; 27 sortArray[i] = sortArray[i + 1]; 28 sortArray[i + 1] = temp; 29 swapped = true; 30 } 31 } 32 } 33 while (swapped); 34 } 35 } 36 37 class BubbleSorter 38 { 39 static public void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison) 40 { 41 bool swapped = true; 42 do 43 { 44 swapped = false; 45 for (int i = 0; i < sortArray.Count - 1; i++) 46 { 47 if (comparison(sortArray[i + 1], sortArray[i])) 48 { 49 T temp = sortArray[i]; 50 sortArray[i] = sortArray[i + 1]; 51 sortArray[i + 1] = temp; 52 swapped = true; 53 } 54 } 55 } while (swapped); 56 } 57 58 public static bool Compa(int i, int j) 59 { 60 return i < j; 61 } 62 }