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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With