HOME

TheInfoList



OR:

A comparison sort is a type of
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. ...
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 In computer science, a three-way comparison takes two values A and B belonging to a type with a total order and determines whether A < B, A = B, or A > B in a single operation, in accordance with the mathematical law of trichotomy. Machine ...
) 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 In mathematics, especially order theory, a weak ordering is a mathematical formalization of the intuitive notion of a ranking of a set, some of whose members may be tied with each other. Weak orders are a generalization of totally ordered set ...
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 In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is important ...
, the input order determines the sorted order in this case. A metaphor for thinking about comparison sorts is that someone has a set of unlabelled weights and a
balance scale A scale or balance is a device used to measure weight or mass. These are also known as mass scales, weight scales, mass balances, and weight balances. The traditional scale consists of two plates or bowls suspended at equal distances from a ...
. 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: *
Quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
*
Heapsort In computer science, heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the ...
*
Shellsort Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange ( bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs o ...
*
Merge sort In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same ...
*
Introsort Introsort or introspective sort is a hybrid sorting algorithm that provides both fast average performance and (asymptotically) optimal worst-case performance. It begins with quicksort, it switches to heapsort when the recursion depth exceeds a l ...
*
Insertion sort 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 on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Ho ...
*
Selection sort In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(''n''2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is no ...
* Bubble sort *
Odd–even sort In computing, an odd–even sort or odd–even transposition sort (also known as brick sort or parity sort) is a relatively simple sorting algorithm, developed originally for use on parallel processors with local interconnections. It is a compar ...
*
Cocktail shaker sort Cocktail 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 sort, or shuttle sort, is an extension of bubble sort. The algorithm extends bub ...
*
Cycle sort Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the pe ...
* Merge-insertion sort *
Smoothsort In computer science, smoothsort is a comparison-based sorting algorithm. A variant of heapsort, it was invented and published by Edsger Dijkstra in 1981. Like heapsort, smoothsort is an in-place algorithm with an upper bound of , but it is not a ...
*
Timsort Timsort is a hybrid, stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algor ...
*
Block sort Block sort, or block merge sort, is a sorting algorithm combining at least two merge operations with an insertion sort to arrive at in-place stable sorting. It gets its name from the observation that merging two sorted lists, and , is equivale ...


Performance limits and advantages of different sorting techniques

There are fundamental limits on the performance of comparison sorts. A comparison sort must have an average-case lower bound of Ω(''n'' log ''n'') comparison operations, which is known as
linearithmic In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
time. This is a consequence of the limited information available through comparisons alone — or, to put it differently, of the vague algebraic structure of totally ordered sets. In this sense, mergesort, heapsort, and introsort are
asymptotically optimal In computer science, an algorithm is said to be asymptotically optimal if, roughly speaking, for large inputs it performs at worst a constant factor (independent of the input size) worse than the best possible algorithm. It is a term commonly en ...
in terms of the number of comparisons they must perform, although this metric neglects other operations. Non-comparison sorts (such as the examples discussed below) can achieve O(''n'') performance by using operations other than comparisons, allowing them to sidestep this lower bound (assuming elements are constant-sized). Comparison sorts may run faster on some lists; many
adaptive sort A sorting algorithm falls into the adaptive sort family if it takes advantage of existing order in its input. It benefits from the presortedness in the input sequence – or a limited amount of disorder for various definitions of measures of disord ...
s such as
insertion sort 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 on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Ho ...
run in O(''n'') time on an already-sorted or nearly-sorted list. The Ω(''n'' log ''n'') lower bound applies only to the case in which the input list can be in any possible order. Real-world measures of sorting speed may need to take into account the ability of some algorithms to optimally use relatively fast cached
computer memory In computing, memory is a device or system that is used to store information for immediate use in a computer or related computer hardware and digital electronic devices. The term ''memory'' is often synonymous with the term ''primary storage ...
, or the application may benefit from sorting methods where sorted data begins to appear to the user quickly (and then user's speed of reading will be the limiting factor) as opposed to sorting methods where no output is available until the whole list is sorted. Despite these limitations, comparison sorts offer the notable practical advantage that control over the comparison function allows sorting of many different datatypes and fine control over how the list is sorted. For example, reversing the result of the comparison function allows the list to be sorted in reverse; and one can sort a list of
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
s in
lexicographic order In mathematics, the lexicographic or lexicographical order (also known as lexical order, or dictionary order) is a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a ...
by just creating a comparison function that compares each part in sequence: function tupleCompare((lefta, leftb, leftc), (righta, rightb, rightc)) if lefta ≠ righta return compare(lefta, righta) else if leftb ≠ rightb return compare(leftb, rightb) else return compare(leftc, rightc) Comparison sorts generally adapt more easily to complex orders such as the order of
floating-point number In computing, floating-point arithmetic (FP) is arithmetic that represents real numbers approximately, using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. For example, 12.345 can be r ...
s. Additionally, once a comparison function is written, any comparison sort can be used without modification; non-comparison sorts typically require specialized versions for each datatype. This flexibility, together with the efficiency of the above comparison sorting algorithms on modern computers, has led to widespread preference for comparison sorts in most practical work.


Alternatives

Some sorting problems admit a strictly faster solution than the bound for comparison sorting by using non-comparison sorts; an example is
integer sorting In computer science, integer sorting is the algorithmic problem of sorting a collection of data values by integer keys. Algorithms designed for integer sorting may also often be applied to sorting problems in which the keys are floating point numb ...
, where all keys are integers. When the keys form a small (compared to ) range,
counting sort In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small positive integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that possess dis ...
is an example algorithm that runs in linear time. Other integer sorting algorithms, such as
radix sort In computer science, radix sort is a non-comparative sorting algorithm. It avoids comparison by creating and distributing elements into buckets according to their radix. For elements with more than one significant digit, this bucketing process i ...
, are not asymptotically faster than comparison sorting, but can be faster in practice. The problem of sorting pairs of numbers by their sum is not subject to the bound either (the square resulting from the pairing up); the best known algorithm still takes time, but only comparisons.


Number of comparisons required to sort a list

Above: A comparison of the lower bound \left\lceil\log_2(n!)\right\rceil to the actual minimum number of comparisons (from ) required to sort a list of ''n'' items (for the worst case). Below: Using
Stirling's approximation In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though a related but less p ...
, this lower bound is well-approximated by n \log_2 n - \frac.
The number of comparisons that a comparison sort algorithm requires increases in proportion to n\log(n), where n is the number of elements to sort. This bound is asymptotically tight. Given a list of distinct numbers (we can assume this because this is a worst-case analysis), there are n
factorial In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial: \begin n! &= n \times (n-1) \times (n-2) \t ...
permutations exactly one of which is the list in sorted order. The sort algorithm must gain enough information from the comparisons to identify the correct permutation. If the algorithm always completes after at most f(n) steps, it cannot distinguish more than 2^ cases because the keys are distinct and each comparison has only two possible outcomes. Therefore, :2^\geq n!, or equivalently f(n)\geq\log_2(n!). By looking at the first n/2 factors of n! = n (n-1) \cdots 1, we obtain :\log_2(n!) \geq \log_2\left(\left(\frac\right)^\frac\right) = \frac \frac - \frac = \Theta(n \log n). :\log_2(n!) = \Omega(n \log n). This provides the lower-bound part of the claim. A more precise bound can be given via
Stirling's approximation In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though a related but less p ...
. An upper bound of the same form, with the same leading term as the bound obtained from Stirling's approximation, follows from the existence of the algorithms that attain this bound in the worst case, like
merge sort In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same ...
. The above argument provides an ''absolute'', rather than only asymptotic lower bound on the number of comparisons, namely \left\lceil\log_2(n!)\right\rceil comparisons. This lower bound is fairly good (it can be approached within a linear tolerance by a simple merge sort), but it is known to be inexact. For example, \left\lceil\log_2(13!)\right\rceil = 33, but the minimal number of comparisons to sort 13 elements has been proved to be 34. Determining the ''exact'' number of comparisons needed to sort a given number of entries is a computationally hard problem even for small ''n'', and no simple formula for the solution is known. For some of the few concrete values that have been computed, see .


Lower bound for the average number of comparisons

A similar bound applies to the average number of comparisons. Assuming that * all keys are distinct, i.e. every comparison will give either ''a''>''b'' or ''a''<''b'', and * the input is a random permutation, chosen uniformly from the set of all possible permutations of ''n'' elements, it is impossible to determine which order the input is in with fewer than comparisons on average. This can be most easily seen using concepts from
information theory Information theory is the scientific study of the quantification (science), quantification, computer data storage, storage, and telecommunication, communication of information. The field was originally established by the works of Harry Nyquist a ...
. The
Shannon entropy Shannon may refer to: People * Shannon (given name) * Shannon (surname) * Shannon (American singer), stage name of singer Shannon Brenda Greene (born 1958) * Shannon (South Korean singer), British-South Korean singer and actress Shannon Arrum W ...
of such a random permutation is bits. Since a comparison can give only two results, the maximum amount of information it provides is 1 bit. Therefore, after ''k'' comparisons the remaining entropy of the permutation, given the results of those comparisons, is at least bits on average. To perform the sort, complete information is needed, so the remaining entropy must be 0. It follows that ''k'' must be at least on average. The lower bound derived via information theory is phrased as 'information-theoretic lower bound'. Information-theoretic lower bound is correct but is not necessarily the strongest lower bound. And in some cases, the information-theoretic lower bound of a problem may even be far from the true lower bound. For example, the information-theoretic lower bound of selection is \left\lceil\log_2(n)\right\rceil whereas n-1 comparisons are needed by an adversarial argument. The interplay between information-theoretic lower bound and the true lower bound are much like a real-valued function lower-bounding an integer function. However, this is not exactly correct when the average case is considered. To unearth what happens while analyzing the average case, the key is that what does 'average' refer to? Averaging over what? With some knowledge of information theory, the information-theoretic lower bound averages over the set of all permutations as a whole. But any computer algorithms (under what are believed currently) must treat each permutation as an individual instance of the problem. Hence, the average lower bound we're searching for is averaged over all individual cases. To search for the lower bound relating to the non-achievability of computers, we adopt the
Decision tree model In computational complexity the decision tree model is the model of computation in which an algorithm is considered to be basically a decision tree, i.e., a sequence of ''queries'' or ''tests'' that are done adaptively, so the outcome of the pre ...
. Let's rephrase a bit of what our objective is. In the
Decision tree model In computational complexity the decision tree model is the model of computation in which an algorithm is considered to be basically a decision tree, i.e., a sequence of ''queries'' or ''tests'' that are done adaptively, so the outcome of the pre ...
, the lower bound to be shown is the lower bound of the average length of root-to-leaf paths of an n!-leaf binary tree (in which each leaf corresponds to a permutation). It would be convinced to say that a balanced full binary tree achieves the minimum of the average length. With some careful calculations, for a balanced full binary tree with n! leaves, the average length of root-to-leaf paths is given by :\frac For example, for , the information-theoretic lower bound for the average case is approximately 2.58, while the average lower bound derived via
Decision tree model In computational complexity the decision tree model is the model of computation in which an algorithm is considered to be basically a decision tree, i.e., a sequence of ''queries'' or ''tests'' that are done adaptively, so the outcome of the pre ...
is 8/3, approximately 2.67. In the case that multiple items may have the same key, there is no obvious statistical interpretation for the term "average case", so an argument like the above cannot be applied without making specific assumptions about the distribution of keys.


n log n maximum number of comparisons for array-size in format 2^k

Can easy compute for real algorithm sorted-list-merging (array are sorted n-blocks with size 1, merge to 1-1 to 2, merge 2-2 to 4...).
(1) = = = = = = = =

(2) =   =   =   =     // max 1 compares (size1+size2-1), 4x repeats to concat 8 arrays with size 1 and 1
   

(3) = = // max 7 compares, 2x repeats to concat 4 arrays with size 2 and 2

=

=

(4) // max 15 compares, 1x repeats to concat 2 arrays with size 4 and 4 Formula extraction: n = 256 = 2^8 (array size in format 2^k, for simplify) On = (n-1) + 2(n/2-1) + 4(n/4-1) + 8(n/8-1) + 16(n/16-1) + 32(n/32-1) + 64(n/64-1) + 128(n/128-1) On = (n-1) + (n-2) + (n-4) + (n-8) + (n-16) + (n-32) + (n-64) + (n-128) On = n+n+n+n+n+n+n+n - (1+2+4+8+16+32+64+128) , 1+2+4... = formula for geometric sequence Sn = a1 * (q^i - 1) / (n - 1), n is number of items, a1 is first item On = 8*n - 1 * (2^8 - 1) / (2 - 1) On = 8*n - (2^8 - 1) , 2^8 = n On = 8*n - (n - 1) On = (8-1)*n + 1 , 8 = ln(n)/ln(2) = ln(256)/ln(2) On = (ln(n)/ln(2) - 1) * n + 1 Example: n = 2^4 = 16, On ~= 3*n n = 2^8 = 256, On ~= 7*n n = 2^10 = 1.024, On ~= 9*n n = 2^20 = 1.048.576, On ~= 19*n


Sorting a pre-sorted list

If a list is already close to sorted, according to some measure of sortedness, the number of comparisons required to sort it can be smaller. An
adaptive sort A sorting algorithm falls into the adaptive sort family if it takes advantage of existing order in its input. It benefits from the presortedness in the input sequence – or a limited amount of disorder for various definitions of measures of disord ...
takes advantage of this "presortedness" and runs more quickly on nearly-sorted inputs, often while still maintaining an O(n\log n) worst case time bound. An example is
adaptive heap sort In computer science, adaptive heap sort is a comparison-based sorting algorithm of the adaptive sort family. It is a variant of heap sort that performs better when the data contains existing order. Published by Christos Levcopoulos and Ola Pete ...
, a sorting algorithm based on
Cartesian tree In computer science, a Cartesian tree is a binary tree derived from a sequence of numbers; it can be uniquely defined from the properties that it is heap-ordered and that a symmetric (in-order) traversal of the tree returns the original sequenc ...
s. It takes time O(n\log k), where k is the average, over all values x in the sequence, of the number of times the sequence jumps from below x to above x or vice versa..


Notes


References

*
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sc ...
. ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. Volumes 1–5 are intended to represent the central core of compu ...
'', Volume 3: ''Sorting and Searching'', Second Edition. Addison-Wesley, 1997. . Section 5.3.1: Minimum-Comparison Sorting, pp. 180–197. {{DEFAULTSORT:Comparison Sort Comparison_sorts Sorting algorithms