Sorting by insertion

The tri by insertion is the most effective sorting on lists of small size. This is why it is used by other methods like the Tri rapid (or quicksort ). It is all the more fast as the data are already sorted partly in the good order.

The principle of this sorting is very simple: it is the sorting which any person uses naturally when it has of the files (or anything of other) to classify. One takes a file and one puts it at his place among the already sorted files. Then one starts again with the following file.

To proceed to a sorting by insertion, it is enough to traverse a list: one takes the elements in the order. Then, one compares them with the preceding elements until finding the place of the element which one considers. It any more but does not remain to shift the elements of the table to insert the element considered into its place in the already sorted part. One can also make a dichotomizing search on the tables.

A possible improvement of this sorting, significant for the lists of more than 15 elements, is the Tri from Shell.

Properties

In the case of the sorting by insertion on a table as implemented low (the most current implementation), the following properties are checked:
  1. the number of comparisons necessary is about NR ² /2.
  2. the median number of exchanges is about NR ² /4.

To count in term of exchanges is not inevitably adequate for this sorting. Indeed, one can make successive copies of the elements shifted rather than to exchange them what divides by two the number of access to the elements.

If a chained Liste is used, there are no more exchanges to make (insertions are made in constant time), but the number of comparisons to find the site where to insert remainder about NR ² /4 and it is difficle to optimize this research.

A contrario, with a table it is possible to make a number of comparisons about N.ln (NR) by using a research by Dichotomie to find the site where to insert the element. Nevertheless the median number of exchanges will not be modified, and the algorithm remains not very effective on the large tables compared to the Tri rapid.

In the case of an almost sorted list, where only some comparisons and exchanges are necessary by element, the most current implementation of the sorting by insertion is often the best method of sorting. On the other hand, if the elements are in the inverse order at the beginning, it is about worst of the cases and one needs N* (N-1) /2 exchanges.

The sorting by insertion is a stable Tri (preserving the order of appearance of the equal elements) and a Tri on the spot (the elements are directly sorted in the structure).

Possible optimizations

One can optimize this sorting while starting with an element in the middle of the list then by alternatively sorting the elements after and front. One can then insert the new element either into the end, or at the beginning of the sorted elements, which divides by two the median number of shifted elements.

Contrary to the optimization of Shell, this optimization makes it possible to keep the stability of the sorting.

Implementations

Implementation a simple of the sorting by insertion on a table of real numbers in C:

#define MAX 100   void insertion (int T) { /* external Spécifications: Sorting of table T per sequential insertion * int' I, p, J; double X;   for ( I = 1; I < MAX; i++ ) { /* storage of the value out of I * X = T;   /* research of the smallest index p lower than I such as T >= T * for ( p = 0; T < X; p++ ) ; /* p points a value of T higher than that out of I *   /* shift before values of T between p and I * for ( J = i-1; J >= p; J-- ) { T = T; }   /* insertion of the value stored in the vacancy * T = X; } }

Sorting by insertion in Pascal in ascending order.

const' MAX = 100; standard = array off real ; Procedure TriInsertion (N: integer; VAr T: ); VAr I, J: integer; VAr Z: real; begin for I: =2 to N C begin Z: = T; (* Z is the value to be inserted *) (* in the suitable place of the table *) (* One shifts all the values of the table < Z *) (* to empty a place for Z on the right *) J: = I - 1; while (J >= 1) and (T > Z) C begin T + 1: = T; J: = J - 1; end ; (* finally value Z is inserted into its adequate site *) T + 1: = Z; end ; end ;

Recursive sorting by insertion by using lists in OCaml. let rec inserts elem = function - > | head:: tail - > yew elem < head then elem:: head:: tail (* one found the place of the element *) else head:: (elem tail inserts) (* one continues to seek in the tail of the list *) let rec tri_insertion = function - > | head:: tail - > inserts head (tri_insertion tail) It is noticed that the lists are structures of data simpler to sort by insertion than the tables, because there does not need " to shift the éléments".

Random links:Michel Pullet | Rue Neuve | Island of Walpole | Percy Radcliffe | Hellborn (film)