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

前端 未结 5 2287
面向向阳花
面向向阳花 2021-02-19 05:37

What is time complexity of C#\'s List.Sort()

I guess it\'s o(N)

But after I searched a lot, I didn\'t get any accurate result.

5条回答
  •  -上瘾入骨i
    2021-02-19 06:30

    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.

提交回复
热议问题