简体   繁体   English

IComparer中.net使用哪种排序算法

[英]Which sorting algorithm is used by .net in IComparer

当我们在类中实现IComparer时,有没有人知道.net使用了哪种排序算法?

QuickSort seems to be it. QuickSort似乎就是这样。

The documentation on IComparer says IComparer的文档说

This interface is used in conjunction with the Array.Sort and Array.BinarySearch methods. 此接口与Array.SortArray.BinarySearch方法结合使用。

The Array.Sort documentation says Array.Sort文档说

This method uses the QuickSort algorithm. 此方法使用QuickSort算法。 This implementation performs an unstable sort; 此实现执行不稳定的排序; that is, if two elements are equal, their order might not be preserved. 也就是说,如果两个元素相等,则可能不会保留它们的顺序。 In contrast, a stable sort preserves the order of elements that are equal. 相反,稳定的排序保留了相等元素的顺序。

According to the MSDN , .NET uses QuickSort. 根据MSDN ,.NET使用QuickSort。 By the way, the method absolutely doesn't depend on the comparer (as long as it's comparison-based), why should .NET therefore use a different method depending on whether you provide a custom comparer or not? 顺便说一句,该方法绝对不依赖于比较器(只要它是基于比较的),为什么.NET因此使用不同的方法取决于您是否提供自定义比较器?

The current documentation says it uses a type of Introsort , a hybrid sorting algoritm: 目前的文档说它使用了一种类型的Introsort ,一种混合​​排序算法:

Is works like this: 是这样的:

  1. If the partition size is fewer than 16 elements, it uses an insertion sort algorithm 如果分区大小少于16个元素,则使用插入排序算法

  2. If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm. 如果分区数超过2 * LogN,其中N是输入数组的范围,则它使用Heapsort算法。

  3. Otherwise, it uses a Quicksort algorithm. 否则,它使用Quicksort算法。

    source here 来源于此

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

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