C#算法(一)
一、冒泡排序(Bubble)
代码
1 public class BubbleSorter
2 {
3 public void Sort(int[] list)
4 {
5 int i, j, temp;
6 bool done = false;
7 j = 1;
8 while ((j < list.Length) && (!done))
9 {
10 done = true;
11 for (i = 0; i < list.Length - j; i++)
12 {
13 if (list[i] > list[i + 1])
14 {
15 done = false;
16 temp = list[i];
17 list[i] = list[i + 1];
18 list[i + 1] = temp;
19 }
20 }
21 j++;
22 }
23 }
24 }
25
26 public class MainClass
27 {
28 public static void Main()
29 {
30 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
31 BubbleSorter sh = new BubbleSorter();
32 sh.Sort(iArrary);
33 for (int m = 0; m < iArrary.Length; m++)
34 Console.Write("{0} ", iArrary[m]);
35 Console.WriteLine();
36 }
37 }
2 {
3 public void Sort(int[] list)
4 {
5 int i, j, temp;
6 bool done = false;
7 j = 1;
8 while ((j < list.Length) && (!done))
9 {
10 done = true;
11 for (i = 0; i < list.Length - j; i++)
12 {
13 if (list[i] > list[i + 1])
14 {
15 done = false;
16 temp = list[i];
17 list[i] = list[i + 1];
18 list[i + 1] = temp;
19 }
20 }
21 j++;
22 }
23 }
24 }
25
26 public class MainClass
27 {
28 public static void Main()
29 {
30 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
31 BubbleSorter sh = new BubbleSorter();
32 sh.Sort(iArrary);
33 for (int m = 0; m < iArrary.Length; m++)
34 Console.Write("{0} ", iArrary[m]);
35 Console.WriteLine();
36 }
37 }
二、选择排序(Selection)
代码
1 public class SelectionSorter
2 {
3 private int min;
4 public void Sort(int[] list)
5 {
6 for (int i = 0; i < list.Length - 1; i++)
7 {
8 min = i;
9 for (int j = i + 1; j < list.Length; j++)
10 {
11 if (list[j] < list[min])
12 min = j;
13 }
14 int t = list[min];
15 list[min] = list[i];
16 list[i] = t;
17 }
18 }
19 }
20
21 public class MainClass
22 {
23 public static void Main()
24 {
25 int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
26 SelectionSorter ss = new SelectionSorter();
27 ss.Sort(iArrary);
28 for (int m = 0; m < iArrary.Length; m++)
29 Console.Write("{0} ", iArrary[m]);
30 Console.WriteLine();
31 }
32 }
2 {
3 private int min;
4 public void Sort(int[] list)
5 {
6 for (int i = 0; i < list.Length - 1; i++)
7 {
8 min = i;
9 for (int j = i + 1; j < list.Length; j++)
10 {
11 if (list[j] < list[min])
12 min = j;
13 }
14 int t = list[min];
15 list[min] = list[i];
16 list[i] = t;
17 }
18 }
19 }
20
21 public class MainClass
22 {
23 public static void Main()
24 {
25 int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
26 SelectionSorter ss = new SelectionSorter();
27 ss.Sort(iArrary);
28 for (int m = 0; m < iArrary.Length; m++)
29 Console.Write("{0} ", iArrary[m]);
30 Console.WriteLine();
31 }
32 }
三、插入排序(InsertionSorter)
代码
1 public class InsertionSorter
2 {
3 public void Sort(int[] list)
4 {
5 for (int i = 1; i < list.Length; i++)
6 {
7 int t = list[i];
8 int j = i;
9 while ((j > 0) && (list[j - 1] > t))
10 {
11 list[j] = list[j - 1];
12 --j;
13 }
14 list[j] = t;
15 }
16 }
17 }
18
19 public class MainClass
20 {
21 public static void Main()
22 {
23 int[] iArrary = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
24 InsertionSorter ii = new InsertionSorter();
25 ii.Sort(iArrary);
26 for (int m = 0; m < iArrary.Length; m++)
27 Console.Write("{0}", iArrary[m]);
28 Console.WriteLine();
29 }
30 }
2 {
3 public void Sort(int[] list)
4 {
5 for (int i = 1; i < list.Length; i++)
6 {
7 int t = list[i];
8 int j = i;
9 while ((j > 0) && (list[j - 1] > t))
10 {
11 list[j] = list[j - 1];
12 --j;
13 }
14 list[j] = t;
15 }
16 }
17 }
18
19 public class MainClass
20 {
21 public static void Main()
22 {
23 int[] iArrary = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
24 InsertionSorter ii = new InsertionSorter();
25 ii.Sort(iArrary);
26 for (int m = 0; m < iArrary.Length; m++)
27 Console.Write("{0}", iArrary[m]);
28 Console.WriteLine();
29 }
30 }
四、希尔排序(ShellSorter)
代码
1
2 public class ShellSorter
3 {
4 public void Sort(int[] list)
5 {
6 int inc;
7 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
8 for (; inc > 0; inc /= 3)
9 {
10 for (int i = inc + 1; i <= list.Length; i += inc)
11 {
12 int t = list[i - 1];
13 int j = i;
14 while ((j > inc) && (list[j - inc - 1] > t))
15 {
16 list[j - 1] = list[j - inc - 1];
17 j -= inc;
18 }
19 list[j - 1] = t;
20 }
21 }
22 }
23 }
24
25 public class MainClass
26 {
27 public static void Main()
28 {
29 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
30 ShellSorter sh = new ShellSorter();
31 sh.Sort(iArrary);
32 for (int m = 0; m < iArrary.Length; m++)
33 Console.Write("{0} ", iArrary[m]);
34 Console.WriteLine();
35 }
36 }
2 public class ShellSorter
3 {
4 public void Sort(int[] list)
5 {
6 int inc;
7 for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
8 for (; inc > 0; inc /= 3)
9 {
10 for (int i = inc + 1; i <= list.Length; i += inc)
11 {
12 int t = list[i - 1];
13 int j = i;
14 while ((j > inc) && (list[j - inc - 1] > t))
15 {
16 list[j - 1] = list[j - inc - 1];
17 j -= inc;
18 }
19 list[j - 1] = t;
20 }
21 }
22 }
23 }
24
25 public class MainClass
26 {
27 public static void Main()
28 {
29 int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
30 ShellSorter sh = new ShellSorter();
31 sh.Sort(iArrary);
32 for (int m = 0; m < iArrary.Length; m++)
33 Console.Write("{0} ", iArrary[m]);
34 Console.WriteLine();
35 }
36 }