Direct Insert Sort 1

Source: Internet
Author: User

The basic idea of a direct insert sort (straight insertion sort) is:

Look at the N-sorted elements as an ordered table and an unordered table. The ordered table contains only 1 elements at the beginning, and the unordered table contains n-1 elements, each time the first element is taken out of the unordered table, it is inserted into the appropriate position in the ordered table, the order is made into a new ordered table, and the sorting process is repeated n-1 times.

Insert Sort Text description

The following is an intermediate procedure for directly inserting a sort to illustrate it. Suppose the first 3 numbers in {20,30,40,10,60,50} are already arranged, are ordered, and then 10 are arranged. As follows:

In the graph, the numbers are divided into ordered and unordered areas. There are only two things we need to do: (1) Take out the 1th number in the unordered area and find out where it corresponds in the ordered area. (2) Inserting the data of the unordered area into the ordered area and, if necessary, shifting the relevant data in the ordered area. Look, isn't it simple.

voidInsert_sort (intA[],intN) {    intI, j, K;  for(i =1; I < n; i++)    {        //find a suitable position for a[i] in the orderly interval of the front a[0..i-1]         for(j = i-1; J >=0; j--)            if(A[j] <A[i]) Break; //if we find a suitable location ,        if(J! = i-1)        {            //move data that is larger than a[i] back            inttemp =A[i];  for(k = i-1; K > J; k--) A[k+1] =A[k]; //put A[i] in the right placeA[k +1] =temp; }    }}

Time complexity and stability

The time complexity of the direct insertion sort is O (N2): Assuming that there are N numbers in the ordered sequence, how many times is the traversal time complexity O (N)? N-1 times, therefore, its time complexity is O (N2).

Direct insertion sequencing is a stable algorithm that satisfies the definition of a stable algorithm: Suppose that there is a a[i]=a[j in the sequence, if A[i] precedes a[j before ordering, and A[i] is still in front of A[j]. Then this sort algorithm is stable!

Direct Insert Sort 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.