# Sort Algorithm: insert sort method (direct insert method and Hill sort method)

Source: Internet
Author: User

1. Direct insertion: the first element is extracted from the unordered table every time and inserted into the proper position of the ordered table to make the ordered table still orderly. Because one element is equal to the inserted element, the inserted element is placed after the element to be inserted. Therefore, the order of equal elements is not changed, and the order from the original unordered sequence is the order after sorting,Therefore, insertion sorting is stable.

Implementation ideas:

1. Regard the first number as an ordered sequence, and traverse backward from the second array, that is, I = 1. The outer loop identifies and determines the value to be compared, the inner loop determines its final position for the value to be compared;

2, when from the number of I forward time, save a [I] In temp, then make J = I, first temp and the number of (J-1) Comparison

(1) If temp> A [J-1], it indicates that the sequence has been ordered, I ++ into the next cycle outside;

(2) If temp <A [J-1], move a [J-1] Behind, j -- enter the next inner loop, and finally save temp to a proper location;

(3) perform (1) (2) until the sorting is completed;

Raw data:

 0 1 2 6 10 4

1, I = 1, temp = A [I] = 10; since J = I, temp> A [J-1] = 6, now (I ++) --> 2

 0 1 2 6 10 4

2, initial I = 2, temp = A [I] = 4;

(1) Since J = I = 2, temp <A [J-1] = 10, at this time a [J-1] Move back, a [J] = A [J-1], (j --) --> 1

 0 1 2 6 10 10

(2) j = 1, temp <A [J-1] = 6, at this time a [J-1] Move back, a [J] = A [J-1], (j --) --> 0, a [J] = temp, exit cyclically

 0 1 2 4 6 10

Code implementation:

`void simple_insertSort(int array[], int n){    int i, j, temp;    for(i=1; i<n; i++)    {        temp = array[i];        for(j=i; j>0; j--)        {             if(temp < array[j-1])                 array[j] = array[j-1];             else                 break;        }        array[j] = temp;    }}`

2. Hill sorting method: splits unordered arrays into several sub-sequences, which are divided into sub-sequences according to a certain interval (d), and inserts and sorts the sub-sequences; then select a smaller interval (D = D/2), divide the array into multiple sub-sequences for sorting, and finally select increment as 1, in this case, you can directly use "insert sort directly" to obtain the ordered sequence of the final array.

Hill sorting process:

5 10 8 60 3 1 90 7

First group: the interval is 8/2 = 4

5------------------------3

----- 10-----------------------1

------------ 8-------------------------90

----------------- 60-------------------------7

After sorting:

3 1 8 7 5 10 90 60

Group 2: interval 4/2 = 2

3----------8----------5-----------90

----- 1----------7----------10------------60

After sorting:

3 1 5 7 8 10 90 60

Group 3: with an interval of 2/2 = 1, directly use the direct insertion method"

3----1----5----7----8----10----90----60

After sorting:

1 3 5 7 8 10 60 90

Implementation Code:

`/************************************************************************************** *  Description: *   Input Args: *  Output Args: * Return Value: *************************************************************************************/int shell_sort (int* s, int len){    int d;    int i,temp,j;    d = len / 2;    while(d > 0)    {        i = d;        while(i < len)        {            j = i;            while(j > 0)            {                if(s[j-d] > s[j])                                {                    temp = s[j-d];                    s[j-d] = s[j];                    s[j] = temp;                    j = j-d;                }                else                    break;            }            i++;        }        d = d / 2;    }    return 0;} /* ----- End of shell_sort()  ----- */`

Http://blog.csdn.net/wswifth/article/details/5829156

Http://blog.csdn.net/cjf_iceking/article/details/7951481

Sort Algorithm: insert sort method (direct insert method and Hill sort method)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.