Which sorting algorithm should you use?
There is no best sorting algorithm, only the best one for your data. This guide turns the choice into a short series of questions about size, order, memory, stability, key type and worst-case needs, then applies them to four concrete situations you could meet in real work.
Start with what your language gives you
In most projects the answer is the built-in sort, and the useful question is which properties it promises. Python's sorted and list.sort, and Java's sort for arrays of objects, typically use Timsort, a stable hybrid of insertion sort and merge sort. JavaScript's Array.prototype.sort has had to be stable since ES2019. C++'s std::sort is typically an introsort, which is quick sort with a heap sort fallback and insertion sort for tiny ranges. It is not stable, so C++ also offers std::stable_sort, which typically merges using extra memory.
Write your own only when a measurement shows the library is the bottleneck, or when your keys have a structure the library cannot exploit, such as small integers.
Size changes the answer
Below a few dozen elements, constant factors matter more than growth rates. Insertion sort has almost no overhead, which is why library sorts typically hand short ranges to it and why the hybrids on this site, Tim sort and Intro sort, contain one.
Around a thousand elements the quadratic algorithms start to hurt. On the visualiser with 1000 shuffled elements, selection sort makes 499,500 comparisons while merge sort makes roughly 8,700. Past that point the real decision is between n log n algorithms, and the next sections decide it.
Do not pay for cleverness you cannot measure. A simple quadratic sort on 30 elements finishes before a fancier algorithm has set up its buffers, and nobody will notice the difference. Reach for the more complex algorithm only when the data size, or how often you sort, makes the gap visible in a profiler.
Is the data nearly sorted?
Insertion sort does work proportional to n plus the number of out-of-order pairs. On an already sorted array of 1000 it makes 999 comparisons and no writes; on a shuffled one it makes about 250,000 comparisons. So when a table is re-sorted after a few edits, or a log arrives almost in time order, insertion sort or Tim sort, which exploits runs that are already in order, can be close to linear.
Be careful with the opposite case. Quick sort that always takes the first or last element as its pivot treats sorted input as its worst case and becomes quadratic. Bubble sort stops early on sorted data, but one small element near the end moves only one place per pass, so it is rarely the right pick even then.
Memory and worst-case guarantees
Merge sort needs a buffer as large as the array, and library Timsort up to half of it; the merge sort here uses n helper cells, and the Reads and Writes counters include their traffic. Heap sort needs almost no extra memory and keeps n log n in every case, but it jumps around the array, so it is typically slower than quick sort on large inputs. Quick sort needs only about log n of stack and is typically the fastest on random data, yet an unlucky pivot sequence makes it quadratic.
When someone else controls the input, or a deadline must never be missed, take an algorithm with a hard bound: heap sort, merge sort, or introsort, which switches to heap sort when quick sort recurses too deeply.
Integer keys and other shortcuts
If the keys are integers in a known small range, counting sort runs in n plus k steps with a table of k cells. A million exam scores between 0 and 100 is the classic case. For fixed-width integers with many distinct values, radix sort makes one pass per digit, so four passes by byte sort 32-bit numbers. For decimals spread evenly over a range, bucket sort is close to linear.
All three rely on assumptions about the keys. The separate guide on comparison and non-comparison sorting explains what each assumption costs.
A decision walk-through
Ask in this order. One: does a standard library sort exist and is it fast enough? Use it. Two: must equal keys keep their input order? Then pick a stable sort. Three: are the keys small integers? Try counting or radix sort. Four: is the array tiny or almost sorted? Insertion sort. Five: is memory tight? Heap sort. Six: do you need a guaranteed bound? Merge, heap or introsort. Otherwise quick sort or merge sort.
Four examples. A web table of 40 rows sorted by clicking column headers: the built-in stable sort, so a second click keeps the order the first one made. Two hundred sensor readings in firmware with 2 KB of free memory: insertion sort or heap sort, since neither needs a buffer. Five million 32-bit sensor ids: radix sort with byte-sized digits. A public service sorting user-supplied lists within a latency budget: introsort or merge sort, never a plain quick sort.