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

Asked

Viewed 269 times

11

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

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

  • 1

    Which Sort() Classy Array?

  • I’m using a list, I don’t know if it’s the same, it would be: mylist.Sort() and me passing my Comparer as parameter.

1 answer

10


It uses an algorithm of classification.

The Sort() class List<T> uses the algorithm of array, since internally the list is only one array. According to the documentation it uses an introspective algorithm and adapts as to what will bring the best result. So it can choose to use:

  • Insertion - if it has less than 16 items
  • Heapsort - if the partitions of the array exceed 2 * Logn, where N is the size of the track that will be ordered
  • Quicksort - too many cases

You can access the sources and see the algorithm used. If you want to follow how you got there source of the public calling method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.