Which sorting algorithm is used by .NET's Array.Sort() method?
Array.Sort()
chooses one of three sorting algorithm, depending on the size of the input:
- If the size is fewer than 16 elements, it uses an insertion sort algorithm.
- If the size exceeds
2 * log^N
, whereN
is the range of the input array, it uses a Heap Sort algorithm. - Otherwise, it uses a Quicksort algorithm
Source: Array.Sort(Array) Method on MSDN.
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. I used to decompile Array.Sort()
from CLR and it seems that they are using both Heap, Insertion and Quicksort.
It uses the QuickSort algorithm.
Source:
- Array.Sort Method (MSDN, Remarks section)