Sorting
Reorder elements according to a key or comparator.
- Problem: Sorting
- Reorder elements so that their keys are in non-decreasing order under a given comparator.
- Input: A sequence a[0..n-1] of items. Each item has a key. Keys are compared by a total preorder, so equal keys are allowed.
- Output: A permutation of the input in which key(a[i]) <= key(a[i+1]) for every i.
- Constraints
- Model: comparisons only, or access to key digits. Comparison sorts need Ω(n log n) comparisons in the worst case.
- Stability: items with equal keys keep their input order.
- Space: in place, O(log n) stack, or an O(n) buffer.
- Input shape: random, nearly sorted, reversed, many duplicates, or a small integer range.
- Memory model: everything fits in RAM, or data lives on disk or on many machines.
- Cost: comparisons, writes or cache misses.
- Guarantee: a worst-case bound, or a good average case.
Comparison Sorts8 items
Sorts that only compare pairs of elements, such as insertion sort and quicksort.
Non-comparison Sorts3 items
Sorts that use the digits or range of the keys instead of comparing them.
Hybrid Sorts2 items
Production sorts that combine simple and fast algorithms, such as Timsort and introsort.