Back to stories
<Engineering/>
Premium

Sorting Algorithms Overview: When to Use Which

Share by

Sorting Algorithms Overview: When to Use Which

Sorting is one of the most studied problems in computer science. Different algorithms trade off time, space, and stability. This post gives an overview of the most common ones and when to use them in practice.


Complexity at a glance

AlgorithmAverageWorstSpaceStable
Bubble sortO(n²)O(n²)O(1)Yes
InsertionO(n²)O(n²)O(1)Yes
Merge sortO(n log n)O(n log n)O(n)Yes
QuicksortO(n log n)O(n²)O(log n)No*
HeapsortO(n log n)O(n log n)O(1)No

*Quicksort can be made stable with extra space. In practice, language runtimes use tuned hybrids (e.g. Timsort).