C#写的几个基本的排序算法(二)
墨初 知识笔记 76阅读
快速排序:1类快速排序示例2 { 3 commonfunctionsint _ funcs=newCommonFunctionsint();45publicvoidSort(int[]values,intbegin,intention)6 { 7if(begin end)8 { 9 intp=Partition(values,begin,end);1011排序(值,begin,p-1);12排序(值,第一页,恩
d);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 }

标签: