Sort algorithm--insert Sort

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Insert Sort, in short, that is, for the number of the first i-1 number is sorted, the number of I is to start from the number of i-1 to traverse forward, the comparison size, the larger than its backward one, smaller than it is the position of the insertion, then insert it here. After inserting, I will find that the number of previous I has been sorted (it should be noted here). So the number of i+1 is inserted. The worst time complexity of this method will also reach O (n^2). </span>

For example, the initial sequence:

8 21

First Pass insert: 8 (default the first number is ordered, then the second number 8 is inserted, obviously 8 is less than the first number, So insert in front of 34)

Second trip insertion: 8 (at this time the first two numbers are ordered, then the third number with the insertion is 64, obviously 34:64 is small, No insertion is required)

Third Pass insertion: 8 (at this point the first three numbers are ordered, then the fourth number 51 is inserted, and the 51:64 is obviously small, Larger than 34, then inserted between 34 and 64)

Four-time insertion: 8 (the first four numbers are ordered, then the fifth number 32 is inserted, Obviously 32:34 is smaller than 8, then inserted between 34 and 8)

Five times inserted: 8 (at this time the first five numbers are ordered, then the sixth number 21 is inserted, obviously 21:32 is smaller than 8, is inserted between 32 and 8)

Five times after inserting, the whole sequence is ordered. Therefore, the number of N, at least n-1 to insert.


Algorithm implementation:

/* Direct insertion of the sorting method */#include <stdio.h>void insertsort (int *data,int N); int main () {    int i=0;    int data[6]={34,8,64,51,32,21};    printf ("Before sort:\n");    for (i=0;i<6;i++)    {        printf ("%d\t", Data[i]);    }    Insertsort (data,6);    printf ("\nafter sort:\n");    for (i=0;i<6;i++)    {        printf ("%d\t", Data[i]);    }    return 0;} void Insertsort (int *data,int N) {    int i=0,j=0;    int tmp=0;    for (i=1;i<n;i++)    {        tmp=data[i];//the value to be inserted for        (j=i;j>0 && data[j-1]>tmp;j--)// Traverse the sorted array from backward to forward until the insertion position is found J            Data[j]=data[j-1];        data[j]=tmp;}    }


Sort algorithm--insert Sort

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.