开发者

What sorting algorithm does the .NET framework implement [duplicate]

This question already has answers here: Whic开发者_如何学运维h sorting algorithm is used by .net in IComparer (3 answers) Closed 10 years ago.

Could anyone please advise when implementing something like IComparable in .NET what sorting algorithm does .NET use to actually sort the underlying data? Also is the algorithm used customizable or selectable?


There are two biggies.

Array.Sort (which sorts an array in-place) uses an unstable Quicksort.

This is the same implementation used internally by List<T>.Sort, according to the MSDN documentation:

This method uses Array.Sort, which uses the QuickSort algorithm.

The Enumerable.OrderBy<TSource, TKey> method (which sorts a copy of an input sequence) uses a stable Quicksort.

As far as I know, these are the only two sorting implementations in the .NET BCL.


The MSDN Documentation states that the sorting algorithm used is Quicksort (at least for arrays) - This is not selectable or customizable.

Note that its not the IComparable interface that specifies what sorting method to use, its down to the method or class that is doing the sorting (normally an array or list, but it could be any method), for example its completely possible for arrays and Lists to sort using completely different algorithms (although in reality both use Quicksort)

This means that if you really want to you can implement your own sorting method using an alternative algorithm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜