Lably
Skip to content

Comparison sort

Type of sorting algorithm that works by comparing pairs of elements

  • 9 min read
  • 4 chapters

Related on Lably

Through Quicksort, Insertion sort and Bubble sort

  1. if a ≤ b and b ≤ c then a ≤ c (transitivity)
  2. for all a and b, a ≤ b or b ≤ a (connexity).

It is possible that both a ≤ b and b ≤ a with a ≠ b; in this case either may come first in the sorted list. In a stable sort, the input order determines the sorted order in this case.

Comparison sorts studied in the literature are "comparison-based". Elements a and b can be swapped or otherwise re-arranged by the algorithm only when the order between these elements has been established based on the outcomes of prior comparisons. This is the case when the order between a and b can be derived via the transitive closure of these prior comparison outcomes.

For comparison-based sorts, the decision to execute basic operations other than comparisons is based on the outcome of comparisons. Hence in a time analysis, the number of executed comparisons is used to determine upper bound estimates for the number of executed basic operations such as swaps or assignments.

A metaphor for thinking about comparison sorts is that someone has a set of unlabelled weights and a balance scale. Their goal is to line up the weights in order by their weight without any information except that obtained by placing two weights on the scale and seeing which one is heavier (or if they weigh the same).

Examples

Some of the most well-known comparison sorts include:

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication