Classical algorithm (2) direct insertion sort of three implementations

Source: Internet
Author: User
Tags sort

The basic idea of direct insertion sort (insertion sort) is to insert a record to be sorted each time by its keyword size into the appropriate position in the previously sorted subsequence until all records are inserted.

Set array to a[0...n-1].

1. At the initial time, the a[0] is a 1 ordered region and the disordered region is a[1..n-1]. Make I=1

2. A[i] is merged into the current ordered area A[0...i-1] to form the ordered interval of a[0...i].

3. i++ and repeat the second step until i==n-1. Sorting completed.

The following is a code that is written strictly by definition (sorted from small to large):

void Insertsort1 (int a[], int n)  
{  
    int i, j, K;  
    for (i = 1; i < n; i++)  
    {  
        //a[i] Find a suitable position in the previous A[0...i-1] ordered interval for  
        (j = i-1; J >= 0; j--)  
            if (a[j) & Lt A[i]) break  
                ;  
      
        If a suitable position is found
        //www.bianceng.cn  
        if (J!= i-1)  
        {  
            //will move the data larger than a[i back  
            int temp = a[i];  
            for (k = i-1 k > J; k--)  
                a[k + 1] = a[k];  
            Place A[i] in the correct position  
            a[k + 1] = temp;  
        }}  

Such a code is too long to be clear enough. Now make a rewrite and merge the two steps of searching and moving the data back. That is, each a[i] is first compared to the previous data a[i-1], if a[i] > a[i-1] Description a[0...i] is also orderly, without adjustment. Otherwise it will j=i-1,temp=a[i]. Then, as you move the data a[j] backward, search forward, and then stop and place temp at A[j + 1] When there is data a[j]<a[i.

void Insertsort2 (int a[], int n)  
{  
    int i, J;  
    for (i = 1; i < n; i++)  
        if (A[i] < a[i-1])  
        {  
            int temp = a[i];  
            for (j = i-1 J >= 0 && a[j] > Temp. j--)  
                A[j + 1] = A[j];  
            A[j + 1] = temp;  
        }  
}

Then the method of inserting the a[j] into the ordered interval of the front a[0...j-1] is rewritten, and the data exchange is substituted for the data post shift. If A[J] Previous data a[j-1] > A[j], Exchange a[j] and a[j-1], then j--until A[j-1 <=]. This can also be achieved by incorporating a new data into an ordered interval.

void Insertsort3 (int a[], int n)  
{  
    int i, J;  
    for (i = 1; i < n; i++)  
        for (j = i-1 J >= 0 && A[j] > a[j + 1]; j--)  
            Swap (a[j), A[j + 1]); 
  }

Contact Us

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.