C#常用算法

冒泡排序

 

 1 namespace BubbleSorter
 2 
 3 {
 4 
 5     public class BubbleSorter
 6 
 7     {
 8 
 9         public void Sort(int[] list)
10 
11         {
12 
13             int i, j, temp;
14 
15             bool done = false;
16 
17             j = 1;
18 
19             while ((j < list.Length) && (!done))
20 
21             {
22 
23                 done = true;
24 
25                 for (i = 0; i < list.Length - j; i++)
26 
27                 {
28 
29                     if (list[i] > list[i + 1])
30 
31                     {
32 
33                         done = false;
34 
35                         temp = list[i];
36 
37                         list[i] = list[i + 1];
38 
39                         list[i + 1] = temp;
40 
41                     }
42 
43                 }
44 
45                 j++;
46 
47             }
48 
49         }
50 
51     }
52 
53     public class MainClass
54 
55     {
56 
57         public static void Main()
58 
59         {
60 
61             int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
62 
63             BubbleSorter sh = new BubbleSorter();
64 
65             sh.Sort(iArrary);
66 
67             for (int m = 0; m < iArrary.Length; m++)
68 
69                 Console.Write("{0}", iArrary[m]);
70 
71             Console.WriteLine();
72 
73         }
74 
75     }
76 
77 }

 

希尔排序

希尔排序是将组分段,进行插入排序.

 

 1 namespace ShellSorter
 2 
 3 {
 4 
 5     public class ShellSorter
 6 
 7     {
 8 
 9         public void Sort(int[] list)
10 
11         {
12 
13             int inc;
14 
15             for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
16 
17             for (; inc > 0; inc /= 3)
18 
19             {
20 
21                 for (int i = inc + 1; i <= list.Length; i += inc)
22 
23                 {
24 
25                     int t = list[i - 1];
26 
27                     int j = i;
28 
29                     while ((j > inc) && (list[j - inc - 1] > t))
30 
31                     {
32 
33                         list[j - 1] = list[j - inc - 1];
34 
35                         j -= inc;
36 
37                     }
38 
39                     list[j - 1] = t;
40 
41                 }
42 
43             }
44 
45         }
46 
47     }
48 
49     public class MainClass
50 
51     {
52 
53         public static void Main()
54 
55         {
56 
57             int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
58 
59             ShellSorter sh = new ShellSorter();
60 
61             sh.Sort(iArrary);
62 
63             for (int m = 0; m < iArrary.Length; m++)
64 
65                 Console.Write("{0}", iArrary[m]);
66 
67             Console.WriteLine();
68 
69         }
70 
71     }
72 
73 } 

 

 

插入排序

 1 namespace InsertionSorter
 2 
 3 {
 4 
 5     public class InsetionSorter
 6 
 7     {
 8 
 9         public void Sort(int[] list)
10 
11         {
12 
13             for (int i = 1; i < list.Length; i++)
14 
15             {
16 
17                 int t = list[i];
18 
19                 int j = i;
20 
21                 while ((j > 0) && (list[j - 1] > t))
22 
23                 {
24 
25                     list[j] = list[j - 1];
26 
27                     --j;
28 
29                 }
30 
31                 list[j] = t;
32 
33             }
34 
35         }
36 
37     }
38 
39     public class MainClass
40 
41     {
42 
43         public static void Main()
44 
45         {
46 
47             int[] iArrary = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
48 
49             InsertionSorter ii = new InsertionSorter();
50 
51             ii.Sort(iArrary);
52 
53             for (int m = 0; m < iArrary.Length; m++)
54 
55                 Console.Write("{0}", iArrary[m]);
56 
57             Console.WriteLine();
58 
59         }
60 
61     }
62 
63 } 
64 
65  

 

选择排序

 

 1 namespace SelectionSorter
 2 
 3 {
 4 
 5     public class SelectionSorter
 6 
 7     {
 8 
 9         private int min;
10 
11         public void Sort(int[] list)
12 
13         {
14 
15             for (int i = 0; i < list.Length - 1; i++)
16 
17             {
18 
19                 min = i;
20 
21                 for (int j = i + 1; j < list.Length; j++)
22 
23                 {
24 
25                     if (list[j] < list[min])
26 
27                         min = j;
28 
29                 }
30 
31                 int t = list[min];
32 
33                 list[min] = list[i];
34 
35                 list[i] = t;
36 
37             }
38 
39         }
40 
41     }
42 
43     public class MainClass
44 
45     {
46 
47         public static void Main()
48 
49         {
50 
51             int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
52 
53             SelectionSorter ss = new SelectionSorter();
54 
55             ss.Sort(iArrary);
56 
57             for (int m = 0; m < iArrary.Length; m++)
58 
59                 Console.Write("{0}", iArrary[m]);
60 
61             Console.WriteLine();
62 
63         }
64 
65     }
66 
67 }

 

 

posted @ 2013-01-20 13:36  sidecore  阅读(453)  评论(0编辑  收藏  举报