LOGBOOK

HELP

← again tap: answer good → ↑ swipe up at the end: next

1 / 9 Part 1 of 3
Other keys: show • Space: good • 1-4: rate • 0: skip • 5: flag

Question

What makes a sorting algorithm comparison-based, and which of the familiar sorts are comparison-based?

Answer

? ? ? ? x₁ x₂ x₃ x₄ x₁ < x₃ ?x₁ < x₃ ?yesx₂ < x₄ ?no only questions: is x < y? no digits no arithmetic no array index the answers are all it knows bubble selection insertion heap merge quick all of them: comparison sorts comparatorx < y ? 7 < 9 "Ana" < "Ben" p.age < q.age the same code for any order worst case of every comparison sort: ≥ log₂(n!) comparisons even for sorts not yet invented the bound is about this class 0123456789B key7 comparison sorts only ask: which comes first?

Explanation in 6 steps

It learns about the order of its input only by asking yes/no questions of the form "is $x_i < x_j$?" and rearranging the elements based on the answers. Bubble-, selection-, insertion-, heap-, merge- and quick-sort all work this way.

A comparison-based sort may compare two elements and move elements around, but it never looks inside a key: it does not read the key's digits, does no arithmetic with it, and does not use it as an array index. Everything it knows about the order of the input comes from the answers to its comparisons.

That restriction is what makes these algorithms generic. The only question they ever ask is "which of these two comes first?", so the same code sorts numbers, strings or whole records, ascending or descending, as long as someone supplies the comparison: a Comparator in Java, __lt__ or a key function in Python.

The restriction is also what the lower bound for sorting is about. It is proved for every algorithm that works this way, including ones nobody has invented yet. Bucket-sort and radix-sort are not in this class: they use the key itself as an array index, and that is how they get past the bound.

Go deeper:

  • doc Comparison sort — the model, the classic members, and the limits that come with it.
A balance scale only tells which of two weights is heavier, so sorting weights with it is a comparison sort.
A balance scale only tells which of two weights is heavier, so sorting weights with it is a comparison sort.
Photographie personnelle User:Poussin jean · CC BY-SA 3.0 · Wikimedia Commons
or press any other key

Question

How can every comparison sort be drawn as a decision tree, and what do its paths and its height mean?

Answer

a < b ? yesnob < c ?a < c ? nonoa < c ?b < c ? 5 comparison nodes yesyesyesnoyesnoa b cb a ca c bc a bb c ac b a 3! = 6 leaves a = 2 · b = 9 · c = 4 a c b = 2 4 9 2 < 9: yes 9 < 4: no 2 < 4: yes 0123 a b cb a c a c bc a bb c ac b a height 3: the worst case one path per run, one tree for all runs

Explanation in 6 steps

For a fixed input size $n$, each inner node is one comparison "$x_i < x_j$?", its two branches are the two answers, and each leaf is the order the algorithm outputs. A run on one input follows one root-to-leaf path, so the path length is that run's number of comparisons, and the height of the tree is the worst case.

Decision tree for sorting three elements a, b, c: five comparison nodes, six leaves, height 3

* The tree insertion-sort follows for three elements. The run on a = 2, b = 9, c = 4 asks three questions and outputs a c b, that is 2 4 9. *

Why such a tree exists at all: a deterministic comparison sort chooses its next comparison based only on the answers it has received so far. Two inputs that produce the same answers are therefore handled identically, step for step. So all possible runs for a given $n$ fit into one binary tree: start at the first comparison, branch on the answer, continue with whatever the algorithm asks next, and stop at a leaf when it knows enough to output the elements in a fixed order.

The algorithm never builds this tree. It is a way to picture all its possible runs at once. Every comparison sort has such a tree for every $n$, and different algorithms have differently shaped trees. That is what makes the model useful: a statement about all binary trees with enough leaves is a statement about all comparison sorts.

In the tree for three elements, two leaves sit at depth 2 (inputs where two answers already settle the order, such as a < b followed by b < c) and four at depth 3. The height, 3, is the number of comparisons for the unluckiest inputs.

Go deeper:

or press any other key