.NET源碼的內部排序實現

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   tar   

使用JetBrains的DotPeek工具可以方便地查看.net的部分源碼。於是看了一下.NET的內部是如何?排序的演算法。

在System.Collections.Generic 命名空間下可以看到ArraySortHelper<T>的實現。

public void Sort(T[] keys, int index, int length, IComparer<T> comparer)      {        try        {          if (comparer == null)            comparer = (IComparer<T>) Comparer<T>.Default;          if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)            ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer);          else            ArraySortHelper<T>.DepthLimitedQuickSort(keys, index, length + index - 1, comparer, 32);        }        catch (IndexOutOfRangeException ex)        {          IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer((object) comparer);        }        catch (Exception ex)        {          throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), ex);        }      } 


發現在.NET4.5以上的版本,開始使用一種叫做 Introspective Sort的排序方法。

 internal static void IntrospectiveSort(T[] keys, int left, int length, IComparer<T> comparer)      {        if (length < 2)          return;        ArraySortHelper<T>.IntroSort(keys, left, length + left - 1, 2 * IntrospectiveSortUtilities.FloorLog2(keys.Length), comparer);      }        private static void IntroSort(T[] keys, int lo, int hi, int depthLimit, IComparer<T> comparer)      {        for (; hi > lo; {          int num;          hi = num - 1;        }        )        {          int num = hi - lo + 1;          if (num <= 16)          {            if (num == 1)              break;            if (num == 2)            {              ArraySortHelper<T>.SwapIfGreater(keys, comparer, lo, hi);              break;            }            else if (num == 3)            {              ArraySortHelper<T>.SwapIfGreater(keys, comparer, lo, hi - 1);              ArraySortHelper<T>.SwapIfGreater(keys, comparer, lo, hi);              ArraySortHelper<T>.SwapIfGreater(keys, comparer, hi - 1, hi);              break;            }            else            {              ArraySortHelper<T>.InsertionSort(keys, lo, hi, comparer);              break;            }          }          else if (depthLimit == 0)          {            ArraySortHelper<T>.Heapsort(keys, lo, hi, comparer);            break;          }          else          {            --depthLimit;            num = ArraySortHelper<T>.PickPivotAndPartition(keys, lo, hi, comparer);            ArraySortHelper<T>.IntroSort(keys, num + 1, hi, depthLimit, comparer);          }        }      }        private static int PickPivotAndPartition(T[] keys, int lo, int hi, IComparer<T> comparer)      {        int index = lo + (hi - lo) / 2;        ArraySortHelper<T>.SwapIfGreater(keys, comparer, lo, index);        ArraySortHelper<T>.SwapIfGreater(keys, comparer, lo, hi);        ArraySortHelper<T>.SwapIfGreater(keys, comparer, index, hi);        T obj = keys[index];        ArraySortHelper<T>.Swap(keys, index, hi - 1);        int i = lo;        int j = hi - 1;        while (i < j)        {          do            ;          while (comparer.Compare(keys[++i], obj) < 0);          do            ;          while (comparer.Compare(obj, keys[--j]) < 0);          if (i < j)            ArraySortHelper<T>.Swap(keys, i, j);          else            break;        }        ArraySortHelper<T>.Swap(keys, i, hi - 1);        return i;      }  


而.NET4.5以下使用的是另一種排序的方案。

在排序的數字小於16個的時候,直接使用插入排序。

private static void InsertionSort(T[] keys, int lo, int hi, IComparer<T> comparer)      {        for (int index1 = lo; index1 < hi; ++index1)        {          int index2 = index1;          T x;          for (x = keys[index1 + 1]; index2 >= lo && comparer.Compare(x, keys[index2]) < 0; --index2)            keys[index2 + 1] = keys[index2];          keys[index2 + 1] = x;        }      }  

而如果大於16個的時候,且當遞迴深度在32次之內的話(也就是數字小於4GB的數量時),使用快速排序。

internal static void DepthLimitedQuickSort(T[] keys, int left, int right, IComparer<T> comparer, int depthLimit)      {        while (depthLimit != 0)        {          int index1 = left;          int index2 = right;          int index3 = index1 + (index2 - index1 >> 1);          ArraySortHelper<T>.SwapIfGreater(keys, comparer, index1, index3);          ArraySortHelper<T>.SwapIfGreater(keys, comparer, index1, index2);          ArraySortHelper<T>.SwapIfGreater(keys, comparer, index3, index2);          T obj1 = keys[index3];          do          {            while (comparer.Compare(keys[index1], obj1) < 0)              ++index1;            while (comparer.Compare(obj1, keys[index2]) < 0)              --index2;            if (index1 <= index2)            {              if (index1 < index2)              {                T obj2 = keys[index1];                keys[index1] = keys[index2];                keys[index2] = obj2;              }              ++index1;              --index2;            }            else              break;          }          while (index1 <= index2);          --depthLimit;          if (index2 - left <= right - index1)          {            if (left < index2)              ArraySortHelper<T>.DepthLimitedQuickSort(keys, left, index2, comparer, depthLimit);            left = index1;          }          else          {            if (index1 < right)              ArraySortHelper<T>.DepthLimitedQuickSort(keys, index1, right, comparer, depthLimit);            right = index2;          }          if (left >= right)            return;        }        ArraySortHelper<T>.Heapsort(keys, left, right, comparer);      }  

而如果大於4GB的數量時,使用堆排序。

private static void Heapsort(T[] keys, int lo, int hi, IComparer<T> comparer)      {        int n = hi - lo + 1;        for (int i = n / 2; i >= 1; --i)          ArraySortHelper<T>.DownHeap(keys, i, n, lo, comparer);        for (int index = n; index > 1; --index)        {          ArraySortHelper<T>.Swap(keys, lo, lo + index - 1);          ArraySortHelper<T>.DownHeap(keys, 1, index - 1, lo, comparer);        }      }        private static void DownHeap(T[] keys, int i, int n, int lo, IComparer<T> comparer)      {        T x = keys[lo + i - 1];        for (; i <= n / 2; {          int num;          i = num;        }        )        {          num = 2 * i;          if (num < n && comparer.Compare(keys[lo + num - 1], keys[lo + num]) < 0)            ++num;          if (comparer.Compare(x, keys[lo + num - 1]) < 0)            keys[lo + i - 1] = keys[lo + num - 1];          else            break;        }        keys[lo + i - 1] = x;      }  

最後,附上swap函數的實現:

private static void SwapIfGreater(T[] keys, IComparer<T> comparer, int a, int b)      {        if (a == b || comparer.Compare(keys[a], keys[b]) <= 0)          return;        T obj = keys[a];        keys[a] = keys[b];        keys[b] = obj;      }        private static void Swap(T[] a, int i, int j)      {        if (i == j)          return;        T obj = a[i];        a[i] = a[j];        a[j] = obj;      }  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.