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#内部很多地方都要用到

 

转载于:https://www.cnblogs.com/yayaxxww/p/3745867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值