Modelintro
Comparison model
Also called decision tree model, comparison-based model.
A model where an algorithm learns about its input only by comparing two items. The cost is the number of comparisons.
The machine
The input is a sequence of items from a set with a total order. The algorithm cannot read the items. It can only ask "is ?" or, with a three-way comparison, "is less than, equal to or larger than ?". It can move and copy items, and it can do any other computation that does not look at the items.
An algorithm in this model is a decision tree. Each inner node is a comparison. Each branch is an outcome. Each leaf is an output. The run on an input follows one path from the root to a leaf.
What it counts
The cost is the number of comparisons. The worst-case cost is the depth of the tree. The average cost is the average depth of the leaves, over a distribution of inputs. The other work of the algorithm is free in the model. For most algorithms it is a constant factor of the number of comparisons, so the model also gives the time.
Relation to other models
The word RAM can do arithmetic on the items and use them as addresses. So it can beat the bounds of the comparison model. Counting sort, radix sort and hash tables do this. The comparison model is still the right model for code that must work for any type with an order, such as the library sort of most languages, which takes a comparison function.
Known results in the model
- Sorting needs comparisons in the worst case and on average. A binary tree with leaves has a depth of at least . See the sorting problem.
- Search in a sorted array needs three-way comparisons. See search in a sorted array.
- The maximum of items needs comparisons. Each item except the maximum must lose one comparison.
- The maximum and the minimum together need comparisons.
- Merging two sorted lists of items each needs comparisons in the worst case.
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 9CMPMD Edit this page History Report an error Markdown JSON