Binary Insert Sort

Source: Internet
Author: User

#include <stdio.h>#defineMAXSIZE 100//assuming file length, that is, the number of records to be sortedtypedefintKeytype;typedefstruct{KeyType key;} Rcdtype;typedefstruct{rcdtype r[maxsize+1];//Store a one-dimensional array of records to be sorted, R or as [0] idle or as "Sentinel use    intlength;} Sortsqlist;//the keyword type is defined for KeyType itselfvoidBininsertsort (Sortsqlist &m) {    intI,j,low,high,mid;  for(i=2; i<=l.length;++i) {l.r[0]=l.r[i];//Copy as Sentinellow=1; high=i-1;  while(Low<=high) {//in R[low...high] binary find the insertion positionMid= (Low+high)/2; if(l.r[0].key<l.r[mid].key) high=mid-1;//insertion point in low half area          ElseLow=mid+1;//insertion point in high half area      }      //For (J=I-1;J>=HIGH+1;--J)       for(j=i-1; j>=low;--j) l.r[j+1]=L.R[J];//record and move backl.r[high+1]=l.r[0];//Insert    }}intMain () {inti; Sortsqlist L; printf ("enter the number of sorts"); scanf ("%d",&l.length);  for(i=1; i<=l.length;++i) scanf ("%d",&L.r[i].key); Bininsertsort (L);  for(i=1; i<=l.length;++i) printf ("%d", L.r[i].key); return 0;}

Binary 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.