Cheatsheet

Sorting algorithms

Best, average and worst time, extra space, stability and in-place behaviour of common sorts, and what each language’s built-in sort actually runs.

Version 1 · updated 2026-11-24

Comparison sorts

Comparison sorts on an array of n elements
AlgorithmBestAverageWorstExtra spaceStableIn place
Bubble sort (early exit)O(n)O(n²)O(n²)O(1)YesYes
Selection sortO(n²)O(n²)O(n²)O(1)No (swap version)Yes
Insertion sortO(n)O(n²)O(n²)O(1)YesYes
Merge sort (top-down, arrays)O(n log n)O(n log n)O(n log n)O(n) buffer + O(log n) stackYesNo
Quicksort (random or median pivot)O(n log n)O(n log n)O(n²)O(log n) stack if the smaller side recurses firstNoYes
Heap sortO(n log n)O(n log n)O(n log n)O(1)NoYes
TimsortO(n)O(n log n)O(n log n)O(n)YesNo
IntrosortO(n log n)O(n log n)O(n log n)O(log n)NoYes
Any comparison sort needs Ω(n log n) comparisons in the worst case. Heap sort’s best case is O(n) only when all keys are equal.

Non-comparison sorts

Sorts that look at key values instead of comparing pairs
AlgorithmTimeExtra spaceStableWorks when
Counting sortO(n + k)O(n + k)Yes (prefix-sum placement)Integer keys in a small range of size k
Radix sort (LSD)O(d · (n + b))O(n + b)YesFixed-width keys of d digits in base b; each pass must be stable
Bucket sortO(n + k) average, O(n²) worstO(n + k)If the per-bucket sort is stableKeys spread roughly uniformly over a known range

What the built-in sort actually is

Standard library sorts (current documentation)
CallAlgorithmStableGuarantee
C# Array.Sort, List<T>.SortIntrosort: insertion sort for small partitions, heapsort past a depth limit, quicksort otherwiseNoO(n log n) worst
C# LINQ OrderBy / ThenBySorts a buffered copy; ties keep their original orderYes, documentedStability is documented; a complexity bound is not
Python list.sort, sortedTimsort, an adaptive merge sort (CPython 3.11+ uses the Powersort merge policy)Yes, guaranteed by the languageO(n log n) worst, O(n) on sorted input
Java Arrays.sort(Object[]), List.sort, Collections.sortTimsort (iterative, adaptive merge sort)Yes, guaranteedO(n log n) worst
Java Arrays.sort(int[]) and other primitivesDual-pivot quicksort (Yaroslavskiy, Bentley, Bloch)No (equal primitives are indistinguishable)O(n log n) on all inputs per the Javadoc
C++ std::sortNot specified; libstdc++, libc++ and MSVC use introsort variantsNoO(n log n) comparisons (required since C++11)
C++ std::stable_sortMerge sort with a temporary bufferYesO(n log n) with enough memory, O(n log² n) without
  • Need stability in C#? Use OrderBy, or sort (key, originalIndex) pairs with Array.Sort.
  • C++ comparators must be a strict weak ordering. Writing <= instead of < is undefined behaviour and can crash std::sort.
  • Java comparators that subtract (a - b) overflow on large values; use Integer.compare or Comparator.comparingInt.
  • Python sorts by key: sorted(items, key=len). For a two-argument comparison use functools.cmp_to_key.
  • Partial work is cheaper: std::partial_sort, std::nth_element (average O(n)), heapq.nsmallest, or a size-k heap for top k.

Choosing

Almost sorted data
Insertion sort or Timsort: both approach O(n).
Need stability
Merge sort, Timsort, std::stable_sort, LINQ OrderBy, any Python or Java object sort.
Tight memory
Heap sort or introsort: O(1) / O(log n) extra space.
Small integer keys
Counting sort or radix sort beat the n log n bound.
Linked lists
Merge sort: no random access needed and no extra buffer.

References