Sorting Efficiency

What Is the Most Efficient Sorting Algorithm?

There is no single most efficient sorting algorithm. Compare merge sort, heapsort, quicksort, Timsort and radix sort by worst case, stability and memory to choose.

On this page
  1. The short answer
  2. Why n log n is the limit for comparison sorts
  3. Algorithms that reach n log n
  4. When a different kind of sort is faster
  5. Comparing the main choices
  6. How to choose

The short answer

There is no single most efficient sorting algorithm. The best choice depends on what you are sorting, how much extra memory you can spare, whether equal items must keep their original order, and what the language’s built-in sort already does well. For sorts that work by comparing pairs of items, the best possible worst case is O(n log n) comparisons, and several well-known algorithms reach it. For most programs, the built-in sort in your language is a sensible default.

Why n log n is the limit for comparison sorts

A sort that learns about its input only by comparing two items at a time cannot be guaranteed to finish in fewer than about n log₂ n comparisons in the worst case. The reason is that n items can be arranged in n! different orders, and each comparison has only two possible answers. The algorithm therefore needs at least log₂(n!) comparisons to tell the orders apart, and that quantity grows like n log₂ n. This limit applies to the worst case. Some comparison sorts do better on particular inputs, but none can do better on every input.

Algorithms that reach n log n

Merge sort splits the list in half, sorts each half, and merges the two sorted halves. It runs in O(n log n) time in the best, average and worst cases. It is stable, but the standard array version needs O(n) extra memory for the merge step.

Heapsort builds a heap, then repeatedly removes the largest or smallest item. Its worst case is O(n log n), and it sorts in place, which means it needs only a small, fixed amount of extra memory. It is not stable.

Quicksort picks a pivot, partitions the items around it, and sorts each part. Its average time is O(n log n), but a poor choice of pivot can push its worst case to O(n²). Practical libraries add safeguards. In GCC’s C++ standard library, for example, the sort is introsort: it switches to heapsort when recursion goes too deep, which keeps the worst case at O(n log n).

Timsort combines merge sort with insertion sort and is built around runs of items that are already in order. It is stable, runs in O(n log n) time in the worst case, and runs in linear time on input that is already sorted. Python’s sorted() and list.sort() use it.

When a different kind of sort is faster

Non-comparison sorts, such as counting sort and radix sort, do not rely only on comparing pairs of items. They use what the keys look like. When the keys are integers within a known, limited range, radix sort can run in time proportional to the number of items times the number of digits, which can beat n log n. They are not general-purpose, though. They need keys with a known structure, and they usually need extra memory.

Comparing the main choices

Algorithm Worst case Stable Extra memory
Merge sort O(n log n) Yes O(n)
Heapsort O(n log n) No O(1)
Quicksort O(n²) Usually no Recursion stack, usually O(log n)
Timsort O(n log n) Yes O(n)

The worst case matters most when inputs are unpredictable. Stability matters when items with equal keys must keep their order. Extra memory matters when the data nearly fills the available space.

How to choose

  • Start with the built-in sort in your language. These are tested and tuned for typical data.
  • If you need a guaranteed worst case and little extra memory, heapsort fits. If you need stability, merge sort or Timsort is the usual choice.
  • If your keys are small integers in a known range, consider counting or radix sort.
  • For very small lists, insertion sort is often fast in practice because its overhead is low, and many hybrid sorts use it for small pieces.

Notation is easier to follow once you know how growth rates are compared, which is covered in big o for data structures. Whether equal items keep their order is its own question, and it is answered in which sorting algorithms are stable. Heapsort is the usual example of an in-place algorithm, and the heapsort algorithm is covered in more depth elsewhere on this site.

Keep exploring

Guides from other topics

All topics →