In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most used orders are numerical order and lexicographical order. Efficient sorting is important to optimizing the use of other algorithms (such as search and merge algorithms) that require sorted lists to work correctly; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions:
Since the dawn of computing, the sorting problem has attracted a great deal of research, perhaps due to the complexity of solving it efficiently despite its simple, familiar statement. For example, bubble sort was analyzed as early as 1956.* Although many consider it a solved problem, useful new sorting algorithms are still being invented to this day (for example, library sort was first published in 2004). Sorting algorithms are prevalent in introductory computer science classes, where the abundance of algorithms for the problem provides a gentle introduction to a variety of core algorithm concepts, such as big O notation, divide-and-conquer algorithms, data structures, randomized algorithms, best, worst and average case analysis, time-space tradeoffs, and lower bounds.
When equal elements are indistinguishable, such as with integers, stability is not an issue. However, assume that the following pairs of numbers are to be sorted by their first coordinate:
(4, 1) (3, 1) (3, 7) (5, 6)
In this case, two different results are possible, one which maintains the relative order of records with equal keys, and one which does not:
(3, 1) (3, 7) (4, 1) (5, 6) (order maintained) (3, 7) (3, 1) (4, 1) (5, 6) (order changed)
Unstable sorting algorithms may change the relative order of records with equal keys, but stable sorting algorithms never do so. Unstable sorting algorithms can be specially implemented to be stable. One way of doing this is to artificially extend the key comparison, so that comparisons between two objects with otherwise equal keys are decided using the order of the entries in the original data order as a tie-breaker. Remembering this order, however, often involves an additional space cost.
In this table, n is the number of records to be sorted and k is the average length of the keys. The columns "Best", "Average", and "Worst" give the time complexity in each case; estimates that do not use k assume k to be constant. "Memory" denotes the amount of auxiliary storage needed beyond that used by the list itself. These are all comparison sorts.
| Name | Best | Average | Worst | Memory | Stable | Method | Other notes |
|---|---|---|---|---|---|---|---|
| Bubble sort | O(n) | — | O(n2) | O(1) | Yes | Exchanging | Times are for best variant |
| Cocktail sort | O(n) | — | O(n2) | O(1) | Yes | Exchanging | |
| Comb sort | O(nlog n) | O(nlog n) | O(nlog n) | O(1) | No | Exchanging | |
| Gnome sort | O(n) | — | O(n2) | O(1) | Yes | Exchanging | |
| Selection sort | O(n2) | O(n2) | O(n2) | O(1) | No | Selection | |
| Insertion sort | O(n) | — | O(n2) | O(1) | Yes | Insertion | |
| Shell sort | — | — | O(n1.5) | O(1) | No | Insertion | |
| Binary tree sort | O(nlog(n)) | O(nlog(n)) | O(nlog(n)) | O(1) | Yes | Insertion | |
| Library sort | O(n) | O(nlog(n)) | O(n2) | O(n) | Yes | Insertion | |
| Merge sort | O(nlog(n)) | O(nlog(n)) | O(nlog(n)) | O(n) | Yes | Merging | |
| In-place merge sort | O(nlog(n)) | O(nlog(n)) | O(nlog(n)) | O(1) | Yes | Merging | Times are for best variant |
| Heapsort | O(nlog(n)) | O(nlog(n)) | O(nlog(n)) | O(1) | No | Selection | |
| Smoothsort | O(n) | — | O(nlog(n)) | O(1) | No | Selection | |
| Quicksort | O(nlog(n)) | O(nlog(n)) | O(n2) | O(log n) | No | Partitioning | Naive variants use O(n) space |
| Introsort | O(nlog(n)) | O(nlog(n)) | O(nlog(n)) | O(logn) | No | Hybrid | |
| Patience sorting | O(n) | — | O(nlog(n)) | O(n) | No | Insertion | Finds all the longest increasing subsequences within O(n log log(n)) |
This table describes sorting algorithms that are not comparison sorts. As such, they are not limited by a O(nlog(n)) lower bound. Complexities below are in terms of n, the number of items to be sorted, and k, the size of each key.
| Name | Best | Average | Worst | Memory | Stable |
|---|---|---|---|---|---|
| Pigeonhole sort | O(n+2k) | O(n+2k) | O(n+2k) | O(2k) | Yes |
| Bucket sort | O(n) | O(n) | O(n2) | O(2k) | Yes |
| Counting sort | O(n+2k) | O(n+2k) | O(n+2k) | O(n+2k) | Yes |
| Radix sort | O(n·2k) | O(n·2k) | O(n·2k) | O(n) | Yes |
This table describes some sorting algorithms that are impractical for real-life use due to extremely poor performance or a requirement for specialized hardware.
| Name | Best | Average | Worst | Memory | Stable | Cmp | Other notes |
|---|---|---|---|---|---|---|---|
| Bogosort | O(n) | O(n × n!) | unbounded | O(1) | No | Yes | Average time using Knuth shuffle |
| Bozo sort | O(n) | O(n × n!) | unbounded | O(1) | No | Yes | Average time is asympotically half that of bogosort |
| Stooge sort | O(n2.71) | O(n2.71) | O(n2.71) | O(1) | No | Yes | |
| Bead sort | O(n) | O(n) | O(n) | — | N/A | No | Requires specialized hardware |
| Pancake sorting | O(n) | O(n) | O(n) | — | No | Yes | Requires specialized hardware |
| Sorting networks | O(log n) | O(log n) | O(log n) | — | Yes | Yes | Requires a custom circuit of size O(nlogn) |
For example, the popular recursive quicksort algorithm provides quite reasonable performance with adequate RAM, but due to the recursive way that it copies portions of the array it becomes much less practical when the array does not fit in RAM, because it may cause a number of slow copy or move operations to and from disk. In that scenario, another algorithm may be preferable even if it requires more total comparisons.
One way to work around this problem, which works well when complex records (such as in a relational database) are being sorted by a relatively small key field, is to create an index into the array and then sort the index, rather than the entire array. (A sorted version of the entire array can then be produced with one pass, reading from the index, but often even that is unnecessary, as having the sorted index is adequate.) Because the index is much smaller than the entire array, it may fit easily in memory where the entire array would not, effectively eliminating the disk-swapping problem.
Another technique for overcoming the memory-size problem is to combine two algorithms in a way that takes advantages of the strength of each to improve overall performance. For instance, the array might be subdivided into chunks of a size that will fit easily in RAM (say, a few thousand elements), the chunks sorted using an efficient algorithm (such as quicksort or heapsort), and the results merged as per mergesort. This is more efficient than just doing mergesort in the first place, but it requires less physical RAM (to be practical) than a full quicksort on the whole array.
Techniques can also be combined. For sorting very large sets of data that vastly exceed system memory, even the index may need to be sorted using an algorithm or combination of algorithms designed to perform reasonably with virtual memory, i.e., to reduce the amount of swapping required.
At the University of British Columbia, Jason Harrison has a page graphically demonstrating the activity of various in-place sorts.
Most languages have built-in support for sorting. These implementations typically use a single algorithm tuned for high-performance in-memory sorting, and for this application it's usually strongly preferred to writing one's own sorting algorithm. Quicksort is a frequent choice, but heapsort and mergesort are also popular. Here are some of the most well-known:
qsort(), a standard library function that can perform an arbitrary comparison sort on an array of objects using a comparison operator passed in as a function pointer. Its implementation is usually, although not required to be, based on quicksort. While flexible, the overhead of invoking the frequently-used comparison operator via a function pointer is often prohibitive as it cannot be inlined. (Ref ISO/IEC 9899:1999(E), section 7.20.5.2, or ANSI/ISO 9899:1990, section 7.10.5.2).
qsort() but adds the templated STL function std::sort which can be specialized for particular types of data. In addition to added type safety, it often outperforms qsort(), particularly when the comparison operation is cheap. C++ also provides stable_sort and partial_sort (Ref sections 25.3 and 25.4 of ISO/IEC 14882:2003(E) and 14882:1998(E)).
java.util.Arrays and java.util.Collections (both available in 1.2 and later) include a variety of sorting functions specialized to sort arrays of primitive data types, and arrays and Lists of Objects which implement the Comparable interface. In addition, it contains versions that allows an arbitrary comparator object to be specified. Implementation notes about the specific implementations used by Sun, including quicksort and mergesort, are available in the API documentation.sort(Object[" target="_blank" >*) does not have to be a mergesort, but it does have to be stable.)"
Array.Sort() which can sort an array of objects using an arbitrary comparator.Although the algorithm is not documented, simple reverse engineering shows that Microsoft's implementation uses quicksort. .NET additionally supplies a Sort() method on the ArrayList class, which also uses quicksort.*
sort function that can take a comparator function which returns negative, zero, or positive integers to indicate less, equal, or greater, respectively.* It used quicksort in version 5.6 and earlier and afterwards used a somewhat slower stable mergesort algorithm.
ArrayQSortFn functor for sorting arrays with quicksort.*
sort, stable_sort, and fast_sort, where the last is just whichever of the first two is generally faster. They all provide a guarantee of constant heap space and logarithmic stack space usage.** In earlier versions it used a separate Sort module.
خوارزميات الترتيب | Algorisme_d'ordenació | Sortierverfahren | Algoritmo de ordenamiento | الگوریتمهای مرتبسازی | Algorithme de tri | 정렬 알고리즘 | Röðunarreiknirit | Algoritmo di ordinamento | מיון (מדעי המחשב) | Rūšiavimo algoritmas | Sorteeralgoritme | ソート | Sortowanie | Algoritmo de ordenação | Алгоритм сортировки | Lajittelualgoritmi | Sorteringsalgoritm | อัลกอริทึมการเรียงลำดับ | 排序算法
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Sorting algorithm".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world