When simple sorters
Simple algorithms are worthwhile for small n, for teaching purposes, and as a base case in hybrids (e.g. insertion in quicksort leaves).
For large n: $O(n \log n)$ algorithms.
| Method | Extra memory (classic) | Stable |
|---|---|---|
| Selection | in-place | no |
| Insertion | in-place | yes |
| Merge | extra array | yes |
| Quicksort | in-place partition | no |
| ## Where used |
n very small, nearly sorted, or as a base case in hybrids. Elsewhere: Arrays.sort / List.sort.
Depth
Simple quadratic sorters are often competitive for small arrays because they have low overhead and work locally. Insertion Sort is also suitable for nearly sorted data and as a finishing step for small subarrays in hybrid methods.
Selection Sort offers a predictably small number of element movements. This can be relevant for media with expensive write operations. However, for large, unordered data, the many comparisons dominate, so asymptotically better algorithms typically win.
Difficulty levels
- Recognize input size and presorting as selection criteria.
- Distinguish comparison costs from write costs.
- Justify the use as a base case of a hybrid sorter.
Pitfalls
The statement "small input" has no universal limit. Data type, runtime environment, cache behavior, and comparison function influence the actual crossover point.
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Beginner
- Completed: 0 users