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
| Algorithm | Best | Average | Worst | Extra space | Stable | In place |
|---|---|---|---|---|---|---|
| Bubble sort (early exit) | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Selection sort | O(n²) | O(n²) | O(n²) | O(1) | No (swap version) | Yes |
| Insertion sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Merge sort (top-down, arrays) | O(n log n) | O(n log n) | O(n log n) | O(n) buffer + O(log n) stack | Yes | No |
| Quicksort (random or median pivot) | O(n log n) | O(n log n) | O(n²) | O(log n) stack if the smaller side recurses first | No | Yes |
| Heap sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Yes |
| Timsort | O(n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Introsort | O(n log n) | O(n log n) | O(n log n) | O(log n) | No | Yes |
Non-comparison sorts
| Algorithm | Time | Extra space | Stable | Works when |
|---|---|---|---|---|
| Counting sort | O(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) | Yes | Fixed-width keys of d digits in base b; each pass must be stable |
| Bucket sort | O(n + k) average, O(n²) worst | O(n + k) | If the per-bucket sort is stable | Keys spread roughly uniformly over a known range |
What the built-in sort actually is
| Call | Algorithm | Stable | Guarantee |
|---|---|---|---|
C# Array.Sort, List<T>.Sort | Introsort: insertion sort for small partitions, heapsort past a depth limit, quicksort otherwise | No | O(n log n) worst |
C# LINQ OrderBy / ThenBy | Sorts a buffered copy; ties keep their original order | Yes, documented | Stability is documented; a complexity bound is not |
Python list.sort, sorted | Timsort, an adaptive merge sort (CPython 3.11+ uses the Powersort merge policy) | Yes, guaranteed by the language | O(n log n) worst, O(n) on sorted input |
Java Arrays.sort(Object[]), List.sort, Collections.sort | Timsort (iterative, adaptive merge sort) | Yes, guaranteed | O(n log n) worst |
Java Arrays.sort(int[]) and other primitives | Dual-pivot quicksort (Yaroslavskiy, Bentley, Bloch) | No (equal primitives are indistinguishable) | O(n log n) on all inputs per the Javadoc |
C++ std::sort | Not specified; libstdc++, libc++ and MSVC use introsort variants | No | O(n log n) comparisons (required since C++11) |
C++ std::stable_sort | Merge sort with a temporary buffer | Yes | O(n log n) with enough memory, O(n log² n) without |
- Need stability in C#? Use
OrderBy, or sort(key, originalIndex)pairs withArray.Sort. - C++ comparators must be a strict weak ordering. Writing
<=instead of<is undefined behaviour and can crashstd::sort. - Java comparators that subtract (
a - b) overflow on large values; useInteger.compareorComparator.comparingInt. - Python sorts by key:
sorted(items, key=len). For a two-argument comparison usefunctools.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, LINQOrderBy, 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
- Array.Sort Method (Remarks: introspective sort, unstable) — Microsoft Learn
- Enumerable.OrderBy Method (stable sort) — Microsoft Learn
- Sorting Techniques (stability guarantee) — Python documentation
- listsort.txt (CPython list sort design notes)
- Arrays (sort implementation notes) — Java SE 21 API
- std::sort — cppreference.com
- std::stable_sort — cppreference.com