Tuesday, March 18, 2014

Stable Sort


A sorting algorithm is called stable if it keeps elements with equal keys in the same relative order in the output as they were in the input. Bubble sort, merge sort, counting sort, insertion sort are stable sorting methods. Most implementations of quicksort are not stable sorts.

.NET Enumerable.OrderBy[Descending] and .ThenBy[Descending]- perform a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.

.NET List<T>.Sort and Array.Sort- perform an unstable sort; that is, if two elements are equal, their order might not be preserved. Depending on size, the algorithm used may be insertion sort, heapsort or quicksort. These methods use the introspective sort (introsort) algorithm as follows:
  • If the partition size is fewer than 16 elements, it uses an insertion sort algorithm.
  • If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.
  • Otherwise, it uses a Quicksort algorithm.

No comments:

Post a Comment