Direct insertion sort
Ordering process
Fundamental
Of length n of the sequence A sort, assuming the presence of two sections sequences - Ordered interval [0, i], random interval [i + 1, n-1]
(1) an ordered traversal of the interval [0, i], to find a suitable position of the insertion section disordered [i + 1, n-1] of the elements A [i + 1];
(2) Expansion of sorted range [0, i + 1], the disorder is reduced to the interval [i + 2, n-1];
(3) The process is repeated until the orderly expansion of the interval [0, n-1], i.e., the sort is complete.
optimization
Optimization of direct insertion sort algorithm breakthrough lies: how to quickly find the right insert position?
1, set bit sentinel
A traverse inserted before recording element A [x] and the insertion position x. If to be inserted in the traversal of the element A [i + 1] is smaller than A [x], all the elements directly to the interval [x, i] Mobile a rearward position, then the interval [0, x-1] to find the right the inserted position; if to be inserted in the traversal of the element a [i + 1] is equal to a [x], directly to the interval [x + 1, i] for all the elements move back one position and then a [i + 1] inserted into the lower position marked x + 1; if this ratio to be inserted to traverse element a [i + 1] is greater than a [x], is according to the old method of finding the right position of insertion interval [0, i].
2, binary search
Find an ordered sequence of a given element, the fastest way is undoubtedly the binary search method, but there is a drawback that the stability of the algorithm will be destroyed. Now we must destroy the stability of the algorithm, you can directly use the more excellent shell sort.