. NET source of internal sorting implementation

Source: Internet
Author: User

The JetBrains Dotpeek tool makes it easy to see part of the source code for. Net. So I looked at it a bit. The internal of net is how to implement the sort algorithm.

The implementation of arraysorthelper<t> can be seen under the System.Collections.Generic namespace .

public void Sort (t[] keys, int index, int length, icomparer<t> comparer)      {        try        {          if (comparer = = nul L)            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, +);        }        catch (IndexOutOfRangeException ex)        {          introspectivesortutilities.throworignorebadcomparer ((object) comparer);        }        catch (Exception ex)        {          throw new InvalidOperationException (Environment.getresourcestring (" Invalidoperation_icomparerfailed "), ex);        }      


Found in. NET4.5 the above version, start using a sorting method called introspective sort.

 internal static void Introspectivesort (t[] keys, int left, int length, icomparer<t> comparer) {if (len        Gth < 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 <=) {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;   }


and. NET4.5 The following is a different sort of scenario.

When the number of sorts is less than 16, the insertion sort is used directly.

private static void Insertionsort (t[] keys, int lo, int hi, icomparer<t> comparer)      {for        (int index1 = lo; i Ndex1 < 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;        }      }  

If it is greater than 16, and if the recursion depth is within 32 times (that is, if the number is less than 4GB), use a quick sort.

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[ind                EX1];                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);   }

And if the quantity is greater than 4GB, the heap is used for sorting.

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&LT;T&GT;.        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;  }   

Finally, the implementation of the SWAP function is attached:

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;      }  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.