Sorting algorithms compared
Each name opens that algorithm's own visualizer page, with how it works step by step. The last column is the article the C# code comes from.
| Algorithm | Best case | Average case | Worst case | Extra space | Stable | C# article |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n²) | O(n²) | O(n²) | O(1) | Yes | Bubble Sort In C# |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | Selection Sort in C# |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Insertion Sort in C# |
| Shell Sort | O(n log n) | Depends on the intervals | O(n²) | O(1) | No | Shell Sort in C# |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Merge Sort in C# |
| Quicksort | O(n log n) | O(n log n) | O(n²) | O(log n) on average | No | Quicksort Algorithm in C# |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Heap Sort in C# |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | In its stable form | Counting Sort in C# |
| Radix Sort | O(d × n) | O(d × n) | O(d × n) | O(n) | Yes | Radix Sort in C# |
| Bucket Sort | O(n) | O(n) | O(n²) | O(n) | Yes | Bucket Sort in C# |
n is the number of values. For counting sort k is the largest value, and for radix sort d is the number of digits in the largest value.
How to use the visualizer
- Play runs the sort. Next and Back move one step, and the slider jumps to any step.
- Size and Start order make a new array. Reversed and already sorted arrays are the inputs where the algorithms differ most.
- Your own numbers takes 2 to 30 whole numbers, split by commas or spaces.
- The counters show the comparisons and array writes up to the current step. Run two algorithms on the same array to compare them.
FAQ
Which sorting algorithm is the fastest?
There is no single winner. On large shuffled arrays quicksort, merge sort and heap sort all run in O(n log n). Insertion sort wins on small or nearly sorted arrays, and counting sort or radix sort can beat all of them when the values are whole numbers in a known range.
What does Array.Sort use in .NET?
Introsort: quicksort with a median-of-three pivot, insertion sort for partitions of 16 or fewer elements, and heap sort when the recursion gets too deep. It is not stable.
What is a stable sorting algorithm?
One that keeps equal values in the order they had before the sort. It matters when you sort records by one field and want ties to keep an earlier order. In C#, OrderBy in LINQ is stable and Array.Sort is not.