Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best sorting algorithms for C# / .NET in different scenarios

What are the best algorithms for sorting data in C#?

Is there one sorting algorithm that can handle 80% of sorts well?

Please give code examples if applicable.

like image 283
Dan Esparza Avatar asked Nov 04 '08 16:11

Dan Esparza


People also ask

Which is the fastest sorting algorithm in C?

Which is the best sorting algorithm? If you've observed, the time complexity of Quicksort is O(n logn) in the best and average case scenarios and O(n^2) in the worst case. But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

What is sorting algorithm in C programming?

C language provides five sorting techniques, which are as follows − Bubble sort (or) Exchange Sort. Selection sort. Insertion sort (or) Linear sort. Quick sort (or) Partition exchange sort.

Which sorting gives best performance?

The bubble sort is at its best if the input data is sorted. i.e. If the input data is sorted in the same order as expected output.


1 Answers

Check out this site: Sorting Comparisons with Animations

Short answer: Quick Sort

Longer answer: The above site will show you the strengths and weaknesses of each algorithm with some nifty animations.

The short answer is there is no best all around sort (but you knew that since you said 80% of the time :) ) but Quick Sort (or 3 Way Quick Sort) will probably be the best general algorithm you could use.

It is the algorithm used by default for Lists in .Net, so you can just call .Sort if what you have is already in a list.

There is pseudo-code on the website I pointed you to above if you want to see how to implement this.

like image 148
CubanX Avatar answered Sep 29 '22 06:09

CubanX