C # Sorting question

  • I wonder what is the best C # data structure I should use to sort efficiently?
  • Is it a list or an array or what?
  • And why doesn't the standard array [] use the sort method on it?

thanks

+2


a source to share


5 answers


Both lists and arrays can be sorted like other data structures.

Sorting efficiency depends on what kind of data you are sorting and what sorting algorithm you are using.



This site gives a good demo of the various sorting algorithms (click on the green initials to start the demo).

+3


a source


Sorting efficiency depends on the amount of data and sorting rules. There is no single answer to your question.



The array [] class implements sorting. See here . This is a static method that is called like Array.Sort

.

+3


a source


Use a general list, it has built-in Sort

If you are looking for an answer that is better in your situation, then my suggestion would be to run some benchmark test against the sampled data that you expect to sort ... then make a decision.

+2


a source


The default implementation is quicksort for containers such as List <>. Quicksort is O (n * Ln (n)). In most cases, this is a good choice. It's a bit slower on small amounts of data and then on O (n * n) algorithms, and sometimes not so good on special data types where you'd be better off using special sorting algorithms (suppose you can implement them yourself or use 3- d party framework)

+1


a source


In fact, if the list changes a lot, perhaps you should keep it ordered with a SortedList.

If sorting happens only once. And then, repeatability: if you sort in memory, then any O (n * Ln (n)) will suffice. You won't notice the difference between a list or an array or whatever.

+1


a source







All Articles