Which sort algorithm does .NET use by default in a list?

10

I have a problem that I have to perform memory sorting on a large number of items and would like to know which algorithm .NET uses to sort when we call the Sort() method of a list, for example.

I need to know because I realized that my data comes with a certain ordering, so it needs few changes, and depending on the case I can get better results by applying different algorithms.

    
asked by anonymous 11.10.2017 / 17:00

1 answer

9

Sort() of class List<T> usa the array algorithm, since internally the list is just an array . According to the documentation it uses an introspective algorithm and adapts according to what will bring the best result. Then you can use:

  • Insertion - if you have less than 16 elements
  • Heapsort - if the array partitions exceed 2 * LogN, where N is the size of the range which will be sorted
  • Quicksort - other cases

You can access the sources and view the algorithm used . If you want to keep track of how you got here, you have the public method source .

    
11.10.2017 / 17:12