Type of sorting algorithm that works by comparing pairs of elements
A comparison sort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a "less than or equal to" operator or a three-way comparison) that determines which of two elements should occur first in the final sorted list. The only requirement is that the operator forms a total preorder over the data, with:
if a ≤ b and b ≤ c then a ≤ c (transitivity)
for all a and b, a ≤ b or b ≤ a (connexity).
It is possible that both a ≤ b and b ≤ a; 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".[1] 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.[1]
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).
^ abWilkes, M. V. (1974-11-01). "The Art of Computer Programming, Volume 3, Sorting and Searching". The Computer Journal. 17 (4): 324–324. doi:10.1093/comjnl/17.4.324. ISSN 0010-4620.
A comparisonsort is a type of sorting algorithm that only reads the list elements through a single abstract comparison operation (often a "less than...
Whether or not they are a comparisonsort. A comparisonsort examines the data only by comparing two elements with a comparison operator. General method:...
during a pass, meaning that the list has become fully sorted. The algorithm, which is a comparisonsort, is named for the way the larger elements "bubble"...
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient...
computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations...
comparisonsort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. It is a comparison-based...
In computer science, selection sort is an in-place comparisonsorting algorithm. It has an O(n2) time complexity, which makes it inefficient on large lists...
sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison...
subroutine in radix sort, another sorting algorithm, which can handle larger keys more efficiently. Counting sort is not a comparisonsort; it uses key values...
American flag sort are typically used to sort large objects such as strings, for which comparison is not a unit-time operation. American flag sort iterates...
In computer science, radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according...
Shell sort or Shell's method, is an in-place comparisonsort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by...
In computer science, bogosort (also known as permutation sort and stupid sort) is a sorting algorithm based on the generate and test paradigm. The function...
perform sorting on fixed numbers of values, in which case they are called sorting networks. Sorting networks differ from general comparisonsorts in that...
log n) runtime due to the heap sort. Since the three algorithms it uses are comparisonsorts, it is also a comparisonsort. Introsort was invented by David...
shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle...
In computer science, a topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge...
show that a comparisonsort of n {\displaystyle n} items must take n log ( n ) {\displaystyle n\log(n)} comparisons. For comparisonsorts, a query is...
computer science, heapsort is a comparison-based sorting algorithm which can be thought of as "an implementation of selection sort using the right data structure...
MIT Press and McGraw-Hill. pp. 161–162. ISBN 0-262-03293-7. Sorting Algorithms (including Stooge sort) Stooge sort – implementation and comparison v t e...
used to improve the efficiency of sorting a list of items. This idiom is appropriate for comparison-based sorting when the ordering is actually based...
Cycle sort is an in-place, unstable sorting algorithm, a comparisonsort that is theoretically optimal in terms of the total number of writes to the original...
A polyphase merge sort is a variation of a bottom-up merge sort that sorts a list using an initial uneven distribution of sub-lists (runs), primarily used...
worst-case performance, thus being degree-optimal for a comparisonsort. However, tree sort algorithms require separate memory to be allocated for the...
least as many comparisons as insertion sort and has the same asymptotic run time characteristics. Gnome sort works by building a sorted list one element...
{\displaystyle T(n)=o(n^{2})} . For example, simple, comparison-based sorting algorithms are quadratic (e.g. insertion sort), but more advanced algorithms can be found...