码迷,mamicode.com
首页 > 其他好文 > 详细

委托的一个应用---冒泡

时间:2014-09-30 16:25:39      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   c   on   

直接上代码,代码比较简单

 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     }

 

委托的一个应用---冒泡

标签:style   blog   color   ar   for   sp   div   c   on   

原文地址:http://www.cnblogs.com/hanbird/p/4001892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!