TOPICS
Search

Sorting


Sorting is the rearrangement of numbers (or other orderable objects) in a list into their correct lexicographic order. Alphabetization is therefore a form of sorting. Because of the extreme importance of sorting in almost all computer algorithms and database applications, a great deal of effort has been expended in the creation and analysis of efficient sorting algorithms. A number of common sorting algorithms include heapsort, merge sort, quicksort, selection sort, and shellsort.

The following table summarizes the number of comparisons or elementary operations used by several comparison sorts, which determine order by comparing pairs of items, on n items. The bounds suppress constant factors and lower-order terms. The base of the logarithm is immaterial in these big-O bounds because changing the base multiplies the logarithm by a constant. Quicksort's quadratic worst case can be avoided with suitable randomization or pivot safeguards, while shellsort bounds depend on the chosen gap sequence.

algorithmbest caseaverage caseworst case
heapsortO(nlogn)O(nlogn)O(nlogn)
merge sortO(nlogn)O(nlogn)O(nlogn)
quicksortO(nlogn)O(nlogn)O(n^2)
selection sortO(n^2)O(n^2)O(n^2)

See also

Heapsort, Merge Sort, Ordering, Quicksort, Selection Sort, Shellsort, Sort-Then-Add Sequence, Weighing

Explore with Wolfram|Alpha

References

Knuth, D. E. The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. Reading, MA: Addison-Wesley, 1998.National Institute of Standards and Technology. "Sort." https://xlinux.nist.gov/dads/HTML/sort.html.Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T. "Sorting." Ch. 8 in Numerical Recipes in FORTRAN: The Art of Scientific Computing, 2nd ed. Cambridge, England: Cambridge University Press, pp. 320-339, 1992.Skiena, S. "Sorting and Searching." §1.1.6 in Implementing Discrete Mathematics: Combinatorics and Graph Theory with Mathematica. Reading, MA: Addison-Wesley, pp. 14-16, 1990.

Referenced on Wolfram|Alpha

Sorting

Cite this as:

Weisstein, Eric W. "Sorting." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/Sorting.html

Subject classifications