Sorting algorithms are the most useful place to start with algorithms in C#. Each one is small enough to hold in your head, and you can see what separates them in the code itself.

In real work you call Array.Sort or a LINQ OrderBy and the framework handles it. So why implement them by hand? Because the comparisons underneath are the same ones you will make about every piece of code you write: how does this behave as the input grows, how much extra memory does it need, and does it still work if the data is already nearly sorted.

Quicksort is the usual default and averages O(n log n). Merge sort matches it in the worst case but needs extra memory. Bubble, selection and insertion sort are O(n squared) and are here to be understood rather than used, though insertion sort genuinely wins on very small or nearly sorted inputs. Counting, radix and bucket sort sidestep comparison entirely and can beat all of them when the data suits.

Each article below includes the full C# implementation and walks through what happens on each pass.

Comparison Sorting Algorithms

These decide the order by comparing pairs of elements. The first four are the ones worth using.

Non-Comparison Sorting Algorithms

These use the shape of the data instead of comparing elements, and can beat O(n log n) when the values fit a known range.

Recursion and Other Algorithms

Recursion is how several of the sorts above are built, and permutations are the classic exercise for it.

Where to Go Next

Where these algorithms meet the rest of C#:

One warning if you benchmark these. On a few hundred items every sort here finishes too fast to measure honestly, and the results will tell you nothing. Use enough data that the numbers mean something.