What is time complexity of .NET List.sort()

At the bottom of the documentation for List.sort, you will see that the algorithm used is as follows:

This method uses the Array.Sort method, which applies the introspective sort as follows:

  • If the partition size is less than or equal to 16 elements, it uses an insertion sort algorithm.
  • If the number of partitions exceeds 2 log n, where n is the range of the input array, it uses a Heapsort algorithm.
  • Otherwise, it uses a Quicksort algorithm.

Hence, Quicksort is the default sorting algorithm, but lists of 16 or fewer elements are sorted using an insertion sort and if the Quicksort algorithm requires more than 2 log n pivots, a Heapsort is used instead.

This final case is important as it changes the worst-case asymptotic complexity to be O(n log n) (as opposed to the O(n^2) of a normal Quicksort).

This is due to the fact that the work done during each pivot operation is no more than O(n) and there are at most O(log n) pivot operations so the Quicksort algorithm will run for no more than O(n log n) time before the Heapsort is used.

Therefore the asymptotic time complexity of the sorting algorithm as a whole is limited by O(2n log n + n log n) which is the same as O(n log n).

If you are still not convinced, the time complexity is listed at the bottom of the remarks:

This method is an O(n log n) operation, where n is Count.


http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx

This method uses Array.Sort, which 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.

On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation.


Adding some information from the recent addition to MSDN on this topic, for framework 4.5, List.Sort method uses a different Sort Strategy depending on the number of elements and partitions.

This method uses the Array.Sort method which applies the introspective sort 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.

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.

On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation.


From the documentation:

On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation.

This is because it uses Quicksort. While this is typically O(n log n), as mentioned on Wikipedia, "Quicksort is often faster in practice than other O(n log n) algorithms"