#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