From Wikipedia, the free encyclopedia
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 disorder – and sorts faster. Adaptive sorting is usually performed by modifying existing sorting algorithms.
Comparison-based sorting algorithms have traditionally dealt with achieving an optimal bound of O(n logn) when dealing with time complexity. Adaptive sort takes advantage of the existing order of the input to try and 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 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 the most worst-case sorting algorithms that do optimally well in the worst-case, notably heap sort and merge sort, do not take existing order within their input into account, although this deficiency is easily rectified in the case of merge sort by checking if left.last_item ≤ right.first_item, 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.
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 form, the Straight Insertion Sort algorithm could look something like this:
procedure Straight Insertion Sort (X, n): X := − ∞; for j := 2 to n do begin i := j − 1; t := X[j]; while t < X[i] do begin X[i + 1] := X[i]; i := i + 1 end; X[i + 1] := t; end;
The performance of this algorithm can be described in terms of the number of inversions in the input. 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 and Adaptive Merge-Sort. Dijkstra’s Smoothsort algorithm is a variation on heap-sort that is also considered an adaptive sorting algorithm.
- Hagerup, Torben; Jyrki Katjainen (2004). Algorithm Theory – SWAT 2004. Berlin Heidelberg: Springer-Verlag. pp. 221–222. ISBN 3-540-22339-8. http://books.google.ca/books?id=2E-KNhKl3gEC&pg=PA221&lpg=PA221&dq=adaptive+sort&source=bl&ots=oJ2rk0F-Lu&sig=129YllOwgiclREZjPVG8jZzXxhw&hl=en&ei=LvuhSZSaOaCiMsXtvNML&sa=X&oi=book_result&resnum=3&ct=result#PPA221,M1.
- Mehta, Dinesh P.; Sartaj Sahni (2005). Data Structures and Applications. USA: Chapman & Hall/CRC. pp. 11‑8 - 11‑9. ISBN 1-58488-435-5. http://books.google.ca/books?id=fQVZy1zcpJkC&pg=PT230&lpg=PT230&dq=adaptive+sort&source=bl&ots=aDqmUlKrpM&sig=hdt8UDoT_DSezCOASDq2w0lD2Kk&hl=en&ei=LvuhSZSaOaCiMsXtvNML&sa=X&oi=book_result&resnum=6&ct=result#PPT230,M1.
- Estivill-Castro, Vladmir; Derick Wood (December 1992). "A survey of adaptive sorting algorithms". ACM (New York, NY, USA: ACM) 24 (4): 441–476. ISSN 0360-0300. http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=D302DA5A589E5D23ED76279C88DCCCEB?doi=10.1.1.45.8017&rep=rep1&type=pdf. Retrieved February 22, 2009.
- Petersson, Ola; Alistair Moffat (1992). "A framework for adaptive sorting". Lecture Notes in Computer Science (Berlin: Springer Berlin / Heidelberg) 621: 422–433. doi:10.1007/3-540-55706-7_38. ISSN 1611-3349. http://www.springerlink.com/content/yv85w0u75777j021/. Retrieved February 23, 2009.
- ^ Peters, Tim, listsort.txt, http://svn.python.org/projects/python/trunk/Objects/listsort.txt, retrieved 2009-03-31