快速排序:
1class QuickSortSample
2 {
3 CommonFunctions<int> _funcs = new CommonFunctions<int>();
4
5 public void Sort(int[] values, int begin, int end)
6 {
7 if (begin < end)
8 {
9 int p = Partition(values, begin, end);
10
11 Sort(values, begin, p - 1);
12 Sort(values, p + 1, end);
13 }
14 }
15
16 public int Partition(int[] values, int begin, int end)
17 {
18 int i = begin - 1;
19
20 for (int j = begin; j < end ; j++)
21 {
22 if (values[j] < values[end])
23 {
24 i++;
25 _funcs.ExchangValues(ref values[j], ref values[i]);
26 }
27 }
28
29 i++;
30 _funcs.ExchangValues(ref values[i], ref values[end]);
31
32 return i;
33 }
34
35 static void Main()
36 {
37 int[] values = {4, 3, 7, 5, 8, 9, 0, 1, 2, 6 };
38
39 QuickSortSample quickSort = new QuickSortSample();
40 quickSort.Sort(values, 0, values.Length - 1);
41
42 foreach (int i in values)
43 {
44 Console.Write(i + " ");
45 }
46 }
47 }
2 {
3 CommonFunctions<int> _funcs = new CommonFunctions<int>();
4
5 public void Sort(int[] values, int begin, int end)
6 {
7 if (begin < end)
8 {
9 int p = Partition(values, begin, end);
10
11 Sort(values, begin, p - 1);
12 Sort(values, p + 1, end);
13 }
14 }
15
16 public int Partition(int[] values, int begin, int end)
17 {
18 int i = begin - 1;
19
20 for (int j = begin; j < end ; j++)
21 {
22 if (values[j] < values[end])
23 {
24 i++;
25 _funcs.ExchangValues(ref values[j], ref values[i]);
26 }
27 }
28
29 i++;
30 _funcs.ExchangValues(ref values[i], ref values[end]);
31
32 return i;
33 }
34
35 static void Main()
36 {
37 int[] values = {4, 3, 7, 5, 8, 9, 0, 1, 2, 6 };
38
39 QuickSortSample quickSort = new QuickSortSample();
40 quickSort.Sort(values, 0, values.Length - 1);
41
42 foreach (int i in values)
43 {
44 Console.Write(i + " ");
45 }
46 }
47 }