2
  • 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

5 Answers 5

3

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).

Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

sorting 32bit integers for exapmle that can fit in memory
How many? 10? 1000? 100000? On what hardware? Using parallel extensions? You need to test things yourself to find out.
2

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.

3 Comments

I know, but I am asking what is more efficient List sorting or Array
@betamoo: as already suggested it is based on the data you are sorting. If you want to test the effeciency of both then you are best doing some benchmarking with both approaches
I think that List expandable manner should decrease its sort efficiency.. of course it is based on its implementation in c#
1

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)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.