1. #include<stdio.h> 
  2.  
  3. //输出数组 
  4. void printdata(int r[],int n) { 
  5.     int i; 
  6.     for(i=1;i<n;i++) 
  7.         printf("%d ",r[i]); 
  8.  
  9. //直接插入排序 
  10. void InsertSort(int r[],int n) { 
  11.     int i,j; 
  12.     for(i=2;i<n;i++) { 
  13.         if(r[i]<r[i-1]) { 
  14.             r[0] = r[i]; 
  15.             for(j=i-1;r[0]<r[j];j--) 
  16.                 r[j+1]=r[j]; 
  17.             r[j+1]=r[0]; 
  18.         } 
  19.     } 
  20.     printdata(r,n); 
  21.  
  22. //折半插入排序 
  23. void BinSort(int r[],int n) { 
  24.     int i,j,low,high,m; 
  25.     for(i=2;i<n;i++) { 
  26.         r[0]=r[i]; 
  27.         low = 1; 
  28.         high = i-1; 
  29.         while(low<=high) { 
  30.             m = (low+high)/2; 
  31.             if(r[0]<r[m]) 
  32.                 high = m-1; 
  33.             else 
  34.                 low = m+1; 
  35.         } 
  36.         for(j=i-1;j>=high+1;--j)  
  37.             r[j+1] = r[j]; 
  38.         r[high+1] = r[0]; 
  39.     } 
  40.     printdata(r,n); 
  41.  
  42. void main() { 
  43.     int a[9] = {0,1,7,3,2,0,5,6,8};  
  44.     int b[9]; 
  45.     int i, n = 9; 
  46.     for(i=0;i<=n-1;i++) 
  47.         b[i]=a[i]; 
  48.     printf("\nInsertSort output:"); 
  49.     InsertSort(b,n); 
  50.  
  51.     for(i=0;i<=n-1;i++) 
  52.         b[i]=a[i]; 
  53.     printf("\nBinSort output:"); 
  54.     BinSort(b,n); 
  55.