C# 内部的快速排序实现

 1 private void QuickSort(int[] map, int left, int right)
 2 {
 3     do
 4     {
 5         int index = left;
 6         int num2 = right;
 7         int num3 = map[index + ((num2 - index) >> 1)];
 8         do
 9         {
10             while ((index < map.Length) && (this.CompareKeys(num3, map[index]) > 0))
11             {
12                 index++;
13             }
14             while ((num2 >= 0) && (this.CompareKeys(num3, map[num2]) < 0))
15             {
16                 num2--;
17             }
18             if (index > num2)
19             {
20                 break;
21             }
22             if (index < num2)
23             {
24                 int num4 = map[index];
25                 map[index] = map[num2];
26                 map[num2] = num4;
27             }
28             index++;
29             num2--;
30         }
31         while (index <= num2);
32         if ((num2 - left) <= (right - index))
33         {
34             if (left < num2)
35             {
36                 this.QuickSort(map, left, num2);
37             }
38             left = index;
39         }
40         else
41         {
42             if (index < right)
43             {
44                 this.QuickSort(map, index, right);
45             }
46             right = num2;
47         }
48     }
49     while (left < right);
50 }

其中CompareKeys需要自己实现,也叫比较器,C#内部很多地方都要用到

 

posted @ 2014-05-22 16:12  王晓阳  阅读(319)  评论(0编辑  收藏  举报