HOME

TheInfoList



OR:

A
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. ...
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 Disorder may refer to randomness, non-order, or no intelligible pattern. Disorder may also refer to: Healthcare * Disorder (medicine), a functional abnormality or disturbance * Mental disorder or psychological disorder, a psychological pattern a ...
for various definitions of measures of disorder – and sorts faster. Adaptive sorting is usually performed by modifying existing sorting algorithms.


Motivation

Comparison-based sorting algorithms have traditionally dealt with achieving an optimal bound of '' O''(''n'' log ''n'') when dealing with
time complexity 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 ...
. Adaptive sort takes advantage of the existing order of the input to try to achieve better times, so that the time taken by the algorithm to sort is a smoothly growing function of the size of the sequence ''and'' the disorder in the sequence. In other words, the more presorted the input is, the faster it should be sorted. This is an attractive feature for a sorting algorithm because nearly sorted sequences are common in practice. Thus, the performance of existing sort algorithms can be improved by taking into account the existing order in the input. Note that most worst-case sorting algorithms that do optimally well in the worst-case, notably
heap sort 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 ...
and
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 i ...
, do not take existing order within their input into account, although this deficiency is easily rectified in the case of
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 i ...
by checking if the last element of the lefthand group is less than (or equal) to the first element of the righthand group, in which case a merge operation may be replaced by simple concatenation – a modification that is well within the scope of making an algorithm adaptive.


Examples

A classic example of an adaptive sorting algorithm is ''Straight Insertion Sort''. In this sorting algorithm, we scan the input from left to right, repeatedly finding the position of the current item, and insert it into an array of previously sorted items. In
pseudo-code In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
form, the ''Straight Insertion Sort'' algorithm could look something like this (array X is zero-based): procedure Straight Insertion Sort (X): for j := 1 to length(X) - 1 do t := X i := j while i > 0 and X - 1> t do X := X - 1 i := i - 1 end X := t end The performance of this algorithm can be described in terms of the number of inversions in the input, and then will be roughly equal to , where is the number of Inversions. Using this measure of presortedness – being relative to the number of inversions – ''Straight Insertion Sort'' takes less time to sort the closer it is to being sorted. Other examples of adaptive sorting algorithms are
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 ...
, adaptive merge sort,
patience sort In computer science, patience sorting is a sorting algorithm inspired by, and named after, the card game patience. A variant of the algorithm efficiently computes the length of a longest increasing subsequence in a given array. Overview The algor ...
,
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 ...
,
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 ...
,
splaysort In computer science, splaysort is an adaptive comparison sorting algorithm based on the splay tree data structure. Algorithm The steps of the algorithm are: # Initialize an empty splay tree # For each data item in the input order, insert it int ...
,
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 algorit ...
, and Cartesian tree sorting.


See also

*
Sorting algorithms 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 ...
*
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 ...


References

* * * {{sorting Sorting algorithms