简体   繁体   English

.NET的Array.Sort()方法使用哪种排序算法?

[英]Which sorting algorithm is used by .NET's Array.Sort() method?

.NET的Array.Sort()方法使用哪种排序算法?

Array.Sort() chooses one of three sorting algorithm, depending on the size of the input: Array.Sort()选择三种排序算法之一,具体取决于输入的大小:

  1. If the size is fewer than 16 elements, it uses an insertion sort algorithm. 如果大小少于16个元素,则使用插入排序算法。
  2. If the size exceeds 2 * log^N , where N is the range of the input array, it uses a Heap Sort algorithm. 如果大小超过2 * log^N ,其中N是输入数组的范围,则它使用堆排序算法。
  3. Otherwise, it uses a Quicksort algorithm 否则,它使用Quicksort算法

Source: Array.Sort(Array) Method on MSDN . 源: MSDN上的Array.Sort(Array)方法

It uses the QuickSort algorithm. 它使用QuickSort算法。

Source: 资源:

Actually, It's not that easy as it's seems. 实际上,它看起来并不那么容易。 It looks like .NET is implementing a set of different sorting algorithms depending on the input and his size. 看起来.NET正在根据输入和他的大小实现一组不同的排序算法。 I used to decompile Array.Sort() from CLR and it seems that they are using both Heap, Insertion and Quicksort. 我曾经从CLR反编译Array.Sort() ,似乎他们正在使用Heap,Insertion和Quicksort。 在此输入图像描述

Quick sort as mentioned. 如上所述快速排序。 But it does not equally well for all data! 但它并不适用于所有数据!

By using reflector: it does sort in a native dll -> for the most common case of 1D arrays, ascending order. 通过使用反射器:它在本地dll中进行排序 - >对于1D数组的最常见情况,升序。 However, other cases are sorted in managed code - with very little optimizations applied. 但是,其他情况在托管代码中排序 - 应用的优化很少。 Hence their speed is usually much slower. 因此他们的速度通常要慢得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM