Programmer
Programmer

Reputation: 503

Which sorting algorithm is used by .net in IComparer

Do any one know which sorting algorithm is used by .net when we implement IComparer in our class?

Upvotes: 8

Views: 2304

Answers (3)

Razvan
Razvan

Reputation: 326

The current documentation says it uses a type of Introsort, a hybrid sorting algoritm:

Is works like this:

  1. If the partition size is fewer than 16 elements, it uses an insertion sort algorithm

  2. If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.

  3. Otherwise, it uses a Quicksort algorithm.

    source here

Upvotes: 8

bdukes
bdukes

Reputation: 155925

QuickSort seems to be it.

The documentation on IComparer says

This interface is used in conjunction with the Array.Sort and Array.BinarySearch methods.

The Array.Sort documentation says

This method uses the QuickSort algorithm. 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.

Upvotes: 10

Konrad Rudolph
Konrad Rudolph

Reputation: 545578

According to the MSDN, .NET uses 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?

Upvotes: 3

Related Questions