Problemintro
Sorting
Also called sort, the sorting problem.
Put the items of a sequence in non-decreasing order by a key.
- Time
- Ω(n log n) worst
- Where
- n = the number of items
- Model
- Comparison model
In plain words
You have a list of things, such as numbers, names or dates. You want them in order, from the smallest to the largest. Sorting is the problem of finding this order. It is one of the most frequent steps in programs. Many other problems become easy when the data is sorted.
Definition
Input. A sequence of items, and a total preorder on the items.
Output. A permutation of the input with .
A sort is stable when items that are equal under keep their order from the input. A sort is in place when it uses or memory in addition to the input.
Variants
- Comparison sorting. The algorithm can only compare two items. This is the model of most library sorts.
- Integer sorting. The items are integers or strings of bits, and the algorithm can use their digits. Counting sort and radix sort are in this group.
- Partial sorting. Find only the smallest items in order.
- External sorting. The items do not fit in memory.
Examples
| input | output |
|---|---|
| 5 2 4 6 1 3 | 1 2 3 4 5 6 |
| "pear" "fig" "apple" | "apple" "fig" "pear" |
| (2, a) (1, b) (2, c) | (1, b) (2, a) (2, c) for a stable sort by the number |
Known algorithms
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Insertion sort | Θ(n²) worst · average | Θ(1) extra | adaptivecomparison baseddeterministicin placeonlineiterativestable | 10 languages |
| Merge sort | Θ(n log n) worst · average · best | Θ(n) extra | not adaptivecomparison baseddeterministicnot in placeofflinerecursivestable | 10 languages |
Bounds
In the comparison model, each sort needs at least comparisons in the worst case. By Stirling's formula, . So the bound is .
The proof uses a decision tree. Each comparison has two outcomes, so a tree of depth has at most leaves. The tree must have a different leaf for each of the orders of the input. So , and . The same bound holds for the average number of comparisons on a random order.
Merge sort reaches this bound up to a lower-order term. Algorithms that use the digits of the items are not limited by this bound. For example, radix sort sorts integers of bits in time.
Reductions
- Element distinctness reduces to sorting. Sort the items. Then two equal items are next to each other.
- Sorting reduces to the convex hull in the plane. Map each number to the point . The hull gives the points in sorted order. So a convex hull also needs time in the comparison model.
Applications
- Search a sorted array with binary search.
- Find duplicates, the median or the most frequent item.
- Merge and join tables in databases.
- Sweep-line algorithms in geometry process events in sorted order.
References
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof |
| |
| Lean |
| |
| Visual |
| |
| Writing |
|
Tag 5SRTPB Edit this page History Markdown JSON