Simple insertion sort

Principle: traversal sequence, the scan element from the back, one by one comparison, the elements should be inserted in position while shifting by one element after the latter gives way to construct the ordered sequence

Algorithm Description: [1] The first element of the ordered set.

     [2] Remove the next element in series with the forward progressively from a comparison of the sorted sequence.

     [3] if the extracted element is smaller than the ordered elements, collating elements already given way rearward movement until you find small than or equal to the extracted element or elements without the front element, the element will be smaller than the insert element behind.

     [4] [2] repeating step [3]

Dynamic presentation:

Code demonstrates:

int currentE;
        for (int i = 0; i < array.length - 1; i++) {
            currentE = array[i+1];
            int tampIndex = i;
            while (tampIndex >= 0 && currentE < array[tampIndex]) {
                array[tampIndex + 1] = array[tampIndex];
                tampIndex--;
            }
            array[tampIndex + 1] = currentE;
        }

 

 

Guess you like

Origin www.cnblogs.com/Gikin/p/11297730.html