What is time complexity of C#\'s List
I guess it\'s o(N)
But after I searched a lot, I didn\'t get any accurate result.
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.