Sorting algorithm one of the insertion sort

Source: Internet
Author: User

Basic ideas

Each time a record is sorted, by its keyword size, is inserted into the appropriate position in the previously ordered subsequence until all records have been inserted.

Classification

According to the search insertion position method is divided into

    • Direct Insert Sort
    • Binary (two minutes) insert sort
    • Hill Insert Sort
Direct Insertion Sort Basic idea

When inserting sectioni(i≥1) Object, the previousv[0],v[1],... ,V[i? 1] Already lined up. At this point, useV[i] The sort code andV[i? 1],V[i? 2],... ,V[0] Sort order to find the insertion position that will be V[i] Insert, and the object in its original position moves backwards.

Insert a sort diagram directly

From top to bottom, it shows all the possible processes of the direct sorting algorithm, including the ordering of the same sort code ( keeping the original order, stating that it is a stable sort ) and the moving of elements in the in-place operation.

Analysis of direct insertion sorting algorithm

Set the number of objects to be sorted n, then the main program of the algorithm executes n? 1 trip Sorting code the number of comparisons and the number of objects moved is related to the initial arrangement of the object sort codes .

  • In the best case, the pre-order objects have been ordered as required. Number of comparisons (KCN):n1  ; Number of moves (RMN): 0 . The corresponding time complexity is O (n ) .
  • In the worst case, the pre-ordering object is reversed in the order required. The first I- time object must be compared with the previous I -object to do a sort code comparison, and every 1 comparisons will be done 1 times the data movement (can be seen from the code given below).
  • Number of comparisons (KCN): ∑N?1i =1i =n (n? 1) 2 ≈n22 ; Number of moves (RMN):∑N?1i =1i =n (n? 1) 2 ≈n22 。 The corresponding time complexity is O(n2).
  • If the sort records are random, then, on average, the number of sort codes compared and the number of objects moved is approximately n24, based on the same probability,so the time complexity of direct insertion sorting is O(n2).
Features of the direct insertion sorting algorithm
    • It is a stable sort and does not change the original order of the same elements.
    • It is a in-place sort, requiring only O(1) of extra memory space.
    • It is ordered online and can be sorted by the side receiving data.
    • It's similar to the way we do cards.
    • is valid for small datasets.
Insert sort pseudo-code directly
j=2 to   a.length    key = A[j]    i = j-1 while     i>0 and a[i]>key                a[i+1] =A[i]        i = i-1        a[i+1] = key   
Direct Insert Sort C code

  

 for (j=1; j<n;j++) {    = a[j];      for (i=j-1; i>=0&&a[i]>key;i--)        A[i+1] = a[i];     = key;}

Sorting algorithm one of the insertion 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.