What sorting algorithm does the .NET framework implement

Maxim Gershkovich picture Maxim Gershkovich · May 11, 2011 · Viewed 12.4k times · Source

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?

Answer

Dan Tao picture Dan Tao · May 11, 2011

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.