Two-point insertion sort

Source: Internet
Author: User

Two-point insertion sorting algorithm

  first, the basic idea: derived from the binary search, assuming that the table elements are in ascending order, the keyword to be inserted and the middle position of the table record keywords and comparison. If it is less than the keyword, the element is inserted into the subsequent child table, and conversely, if it is greater than the keyword, the element is inserted in the preceding child table. Iterate until all the keywords are inserted into the table.

  

  

  Second, C language code:

  

1#include <stdio.h>2#include <stdlib.h>3 4 //Direct Insert ordering for r[0...n-1] ascending order5 voidBinaryinsertsort (RecType r[],intN)6 {7     inti;8     intJ;9     intLow ;Ten     intHigh ; One     intmid; A RecType tmp; -      -      for(i =1; I < n; i++) { theTMP =R[i]; -Low =0;  -High = i-1;  -          +         //find the position of an orderly insert in R[low...high] -          while(Low <=High ) { +Mid = (low + high)/2; A             if(Tmp.key <R[mid].key) { atHigh = mid-1;//left half area -}Else { -Low = mid +1;//Right Half area -             } -         } -          in         //record backward, high means that each time it is compared from right to left in an ordered interval -          for(j = i-1; J >= High +1; j--) { tor[j+1] =R[j]; +         }  -r[high+1] = tmp;//Inserting Records the     } *} 

Three, algorithm analysis

    Time complexity: By the algorithm code is known, the binary insertion sort consists of two loops, for n sort records, the outer layer must undergo n-1 cycles, in each order to find the position of the insertion point <= high when the number of cycles of the tree Logi, wherein the 0≦i≦n-1, while the record of the movement of the second The number aspect is the same as the direct insertion sort, so for the binary insertion sort:

The minimum number of times the keyword comparison and record movement is (n-1) Σlogi, where 0≦i≦n-1 the time complexity of the algorithm is O (n).

The maximum number of keyword comparisons and records moved is (n-1) Σ (I+logi), where 0≦i≦n-1, the algorithm's time complexity is O (n²).

The average comparison of keywords and the number of records moved is [(n-1) Σ (I+logi)]/2, where 0≦i≦n-1, the time complexity of the algorithm is O (n²).

Spatial complexity: The algorithm code shows that the extra space required is only one TMP variable, so the spatial complexity of the direct insertion sorting algorithm is O (1).

Iv. thinking

Binary insertion sort does the number of records move the same as the direct insert sort after a traversal of the insert sort?

Two-point insertion sort

Related Article

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.