Bomis
In Data-processing or Mathematical, a algorithm of sorting is a algorithm which makes it possible to organize a collection of objects according to a given order. The objects with sorting thus form part of a Ensemble provided with a Relation with order (in a general way a total order). The most used orders are the numerical order and the collating Sequence (dictionary).
According to the relation of order considered, the same collection of object can give place to various arrangements, however it is possible to define an algorithm of sorting independently of the function of order used. This one will do nothing but use one certain function of order corresponding to a relation of order which must make it possible to compare any couple of elements of the collection.
Classification
The classification of the algorithms of sorting is very important, because it makes it possible to choose the algorithm more adapted to the dealt with problem, while taking account of the constraints imposed by this one.
One distinguishes, first of all, the algorithms of sorting of general application, proceeding by comparisons between pairs of elements, and more specialized algorithms making restrictive assumptions on the structure of the entered data (for example, sorting by counting, applicable only if the data are taken among a small unit known in advance). If anything is not specified, one usually understands by algorithm of sorting a general algorithm of sorting by comparison.
The main features which make it possible to differentiate the algorithms from sorting are: the algorithmic Complexity, essential resources (in particular in term of memory capacity used) and stable character.
Algorithmic complexity
-
the algorithmic Complexité temporal in the worst of the cases makes it possible to fix an upper limit of the number of operations which will be necessary to sort a whole of N elements. One uses to symbolize this complexity the notation of Pram: O .
- temporal algorithmic complexity on average: it is the number of elementary operations carried out on average to sort a collection of elements. It makes it possible to compare the algorithms of sorting and gives an good idea of the execution time which will be necessary to the algorithm; one manages to estimate it with a rather important precision. However, if the whole with sorting has a particular form and are not representative of N ! possible combinations, then the performances could be much lower or much higher than “average” complexity.
- space algorithmic complexity (on average or in the worst of the cases) represents, as for it, the memory use which the algorithm will require. This one can depend, like the execution time, of the number of elements to sorting.
For some of the algorithms of sorting simplest, T ( N ) = O ( N 2), for the more worked out sorting, T ( N ) = O ( N ·log ( N )).
One can show that the temporal complexity on average and in worst of the cases of an algorithm based on a function of comparison cannot be better than N·log (N) . The sorting which require only N ·log ( N ) comparisons on average are then known as optimal.
The problem of the sorting consists, being taken an action pursuant U = ( u1 , u2 ,…, un ) of elements of a completely ordered unit (for example ), to determine a permutation σ of 1,…, N such as: there = ( uσ (1) , uσ (2) ,…, uσ (N) ) is sorted.
An algorithm of sorting by successive comparisons models like a binary Tree, each node of the tree corresponding to a comparison between two elements of the unit. One compares two elements ui and uj, and according to the result, one passes to the one of the two following nodes, where one proceeds to another comparison. Each sheet (final node) of the tree corresponds after completely sorted.
The algorithm must be able to provide all the possibilities of permutation of the terms of the continuation, because it is equivalent to provide the permutation σ that the sorted continuation there . The number of permutations of N elements being N ! (Factorial N) the number of sheets of the tree must be at least N ! .
Let us note H the maximum depth of the tree (we speak well about the number of stages in worst of the cases ). The maximum number of sheets in a binary tree of maximum depth H is of .
It thus comes: ; thus, asymptotically, (by the use of the Formula of Stirling).
The fact that there exist sorting in watch in addition which it is possible to have asymptotically from where the concept of minimum.
For certain types of data (whole, character strings of limited size), there exist however more effective algorithms on the level of the execution time, like the Tri counting or the Tri radix. These algorithms do not use the comparison between elements (the terminal N·log (N) thus does not apply for them) but require assumptions on the objects with sorting. For example, tri counting and the sorting radix apply to entireties which one can belong to the unit '' m '' with as additional assumption for the sorting radix that m is a power of 2 (i.e. form 2k).
Character in place
An algorithm is known as places from there if it uses only one very limited number of variables and that it directly modifies the structure which it is sorting. This requires the use of a structure of adapted data (a table for example). This character can be very important if one does not have a great quantity of memory usable.
Let us notice however that in general, one directly does not sort the data themselves, but only of the references (or pointer) on these last.
Stable character
An algorithm is known as stable if it keeps the relative order of the equal quantities for the relation of order.
Example, if the following continuation of elements is considered: (4, 1) (3, 1) (3, 7) (5, 6) that one sorts compared to their first coordinate (the key), two cases are possible, when the relative order is respected and when it is not it: (3, 1) (3, 7) (4, 1) (5, 6) (maintained relative order) (3, 7) (3, 1) (4, 1) (5, 6) (changed relative order) When two elements are equal for relation of order (i.e. it have the same key), the algorithm of tri preserve the order in which these two elements were before its execution. The unstable algorithms of sorting can be specifically worked again in order to make them stable, however that can be with the costs of the speed and/or can require an additional memory capacity.
Among the algorithms listed low, the sorting being stable are: the Tri with bubbles, the Tri by insertion and the Tri fusion. The other algorithms require O (N) additional memory to store the initial order of the elements.
Examples of algorithms of sorting
Sorting by comparison
Slow algorithms
These algorithms are slow for more than 20 elements because they are out of O (n2).-
Tri with bubbles: Quadratic algorithm, T (N) = the O (n2), on average and in worst of the case, stable and in place; amusing but not effective
- Tri by selection: Quadratic algorithm, T (N) = the O (n2), on average and in worst of the cases, not stable so tri on the spot; rapid when one has less than 7 elements
- Tri by insertion: Quadratic algorithm, T (N) = the O (n2), on average and in worst of the case, stable and in place. It fastest and is used the most for lists of less than 15 elements;
Faster algorithms
-
Tri of Shell ( Shell leaves ): Improvement of the Tri by insertion, but not stable. Complexity: at worst for the series of step and for the series of step . One does not know a series giving .
-
Tri fusion ( merge leaves ): on average and in the worst of the case, stable but not in place;
- Tri rapid ( quick leaves ): on average, but in (quadratic) at worst the case, in place but not stable
- Introsort: Improvement of the Tri rapid, which allows an execution in in all the cases.
-
Tri by heap ( heap leaves ): on average and in the worst of the cases, in place but not stable. Always approximately twice slower than the tri rapid, i.e. in the neighborhoods of , it is thus interesting to use it if one suspects that the data to be sorted will be often quadratic cases for the tri rapid;
- Smoothsort : sorting inspired of the Tri by heap by using a tree not reversed, this sorting is very fast for the units already almost sorted, if not, it is in . Sorting places but not stable
Note: one can easily obtain the stability of a sorting if one associates with each element his initial position. For that, one can create a second of the same table cuts to store the initial order (one gives up then the character in place of the sorting).
| Random links: | Aspin-Aure | Cuccaro Vetere | Euro Ice Hockey Challenge | Growth regulator | Italian federation of Rugby with XV |