开发者

C# Sorting Question

  • I wonder what is the best C# data structure I should use to sort efficiently?
  • Is it List or Array or what?
  • And why the开发者_如何学运维 standard array [] does not implement sort method in it?

Thanks


Both Lists and Arrays can be sorted, as can other data structures.

The efficiency of the sort depends on what data you are sorting, and what sorting algorithm you are using.

This site gives a good demonstration of different sorting algorithms (click on the green arrrows to start the demonstration).


Sorting efficiency depends on the amount of data and the sort rules. There is no one definite answer to your question.

The array[] class does implement sort. See here. It is a static method to be called as Array.Sort.


Use a generic list, it has a built in Sort

If you are looking for an answer on which is better in your situation then my suggestion would be to run some benchmark testing against sample data you would be expecting to sort....then make a decision.


The default implementation is quick sort for containers like List<>. Quick sort is about O(n * Ln(n)). For most cases it's a good choice. It is a little bit slower on small amounts of data then O(n * n) algorithms and sometimes not so good on special types of data where you'd better use special sort algorithms (suppose you can implement them by yourself or use 3-d party framework)


Actually, If list changes a lot, perhaps you should keep it ordered with a SortedList.

If sorting happens only once. And then, the reallity: if you are sorting in-memory, any O(n * Ln(n)) will suffice. You will notice no difference between List or Array or whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜