Best sorting algorithms for C# / .NET in different scenarios
Solution 1
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.
Solution 2
What are you trying to sort? Is there any reason not to use:
List<T>.Sort() ?
I'm sure this uses QuickSort and you don't have to worry about making any coding errors. You can implement IComparable to change what you want to sort on.
If all your data doesn't fit in memory... well the you're off to the races with Merge sort or something along those lines.
Solution 3
The Bubblesort and the Insertionsort are O(n^2), the Mergesort and the Quicksort are O(nlogn). You can use Sort() method from List, that implements Quicksort, or also you can try implement it and adapt it to yours needs. Here is a basic implementation: Quicksort
//O(nlogn)
public static void QuickSort(int[] array, int init, int end)
{
if (init < end)
{
int pivot = Partition(array, init, end);
QuickSort(array, init, pivot-1);
QuickSort(array, pivot + 1, end);
}
}
//O(n)
private static int Partition(int[] array, int init, int end)
{
int last = array[end];
int i = init - 1;
for (int j = init; j < end; j++)
{
if (array[j] <= last)
{
i++;
Exchange(array, i, j);
}
}
Exchange(array, i + 1, end);
return i + 1;
}
private static void Exchange(int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
From http://yadiragarnicabonome.com/sorting-arrays/
Dan Esparza
As a Software Developer I really like Go / Docker / C# / ASP.NET / MVC / Bootstrap / ReactJS and Flux / yarn / webpack / AppVeyor, CircleCI, RedGate Ants, and that my favorite comic has its own website. As a budding software entrepreneur I like Hacker News, Trello, Stripe, Github, CircleCI, Balsqmiq mockups, Pingdom, CloudFlare, MailGun, DigitalOcean and Amazon EC2. As a cook, I really like the French & Good Eats. As an American, I like College Football and Baseball. As an iPhone & iPad user, I like Reeder, & 1Password. As a lover of the interwebs, I like Pinboard.in, Pocket, Flickr, Yelp, Keepass, and Dropbox As a network and web security hobbyist, I follow Bruce and was illuminated by the Base Rate fallacy. As a geek, I really enjoy learning about quantum physics and I'm amazed by the double slit experiment. Feynman was a rock star in my book.
Updated on February 22, 2020Comments
-
Dan Esparza about 4 years
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.
-
Rodney S. Foley about 6 yearsQuicksort is Big O(n²) as Big O is worst case scenario you gave its average scenario which is Big Theta aka Θ(n log(n)). Quicksort is one of the worse for Big O for time complexity. Mergesort, Heapsort, or Cubesort (or variations) are common sort alogrithms with Big O(n log(n)) time complexity. You typically have to get into something more specialized or complex to do better in Big O. If you can use it a Heapsort is the best well-balanced algorithm across in my opinion for Time (Big Omega, Theta, and O) and Space (Big O).
-
Timo almost 4 yearsNote that C#'s
List<T>
.Sort` implementation is quite clever. It is a quicksort that detects the degenerate case, falling back to heapsort if things get out of hand. This way, it mitigates one of quicksort's greatest shortcomings. I'm assuming it also cuts off to insertion sort whenever a partition is below a certain size (a very common optimization), although I don't remember for sure. It is, however, unstable.