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

  • 相关阅读:
    sys模块详解
    os模块详解2
    tyvj 1203 机器分配
    洛谷 P1496 火烧赤壁
    P1204 [USACO1.2]挤牛奶Milking Cows
    bzoj 2120 数颜色
    P2056 采花
    P1972 [SDOI2009]HH的项链
    9.20模拟赛
    P2709 小B的询问
  • 原文地址:https://www.cnblogs.com/yayaxxww/p/3745867.html
Copyright © 2020-2023  润新知