Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which sorting algorithm is used by .NET's Array.Sort() method?

Which sorting algorithm is used by .NET's Array.Sort() method?

like image 733
Manoj Talreja Avatar asked Dec 06 '09 07:12

Manoj Talreja


People also ask

Which sorting algorithm is used in array sort?

As mentioned in the official JavaDoc, Arrays. sort uses dual-pivot Quicksort on primitives. It offers O(n log(n)) performance and is typically faster than traditional (one-pivot) Quicksort implementations. However, it uses a stable, adaptive, iterative implementation of mergesort algorithm for Array of Objects.

Which sorting algorithm is used in arrays sort Python?

The Quicksort Algorithm in Python. Just like merge sort, the Quicksort algorithm applies the divide-and-conquer principle to divide the input array into two lists, the first with small items and the second with large items. The algorithm then sorts both lists recursively until the resultant list is completely sorted.

What algorithm does Ruby sort use?

The Array#sort method in Ruby uses the venerable Quicksort algorithm. In its best case, Quicksort has time complexity O(n log n), but in cases where the data to be sorted is already ordered, the complexity can grow to O(n2).


3 Answers

Array.Sort() chooses one of three sorting algorithm, depending on the size of the input:

  1. If the size is fewer than 16 elements, it uses an insertion sort algorithm.
  2. If the size exceeds 2 * log^N, where N is the range of the input array, it uses a Heap Sort algorithm.
  3. Otherwise, it uses a Quicksort algorithm

Source: Array.Sort(Array) Method on MSDN.

like image 169
Badgujar Bhushankumar Avatar answered Sep 27 '22 16:09

Badgujar Bhushankumar


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. enter image description here

like image 27
Stanimir Yakimov Avatar answered Sep 27 '22 18:09

Stanimir Yakimov


It uses the QuickSort algorithm.

Source:

  • Array.Sort Method (MSDN, Remarks section)
like image 32
Christian C. Salvadó Avatar answered Sep 27 '22 17:09

Christian C. Salvadó