[C] 折半插入排序 →→→→→进入此内容的聊天室

来自 , 2021-03-21, 写在 C, 查看 131 次.
URL http://www.code666.cn/view/f15d337c
  1. void InsertSort ( S_TBL *s )
  2. { /* 对顺序表s 作折半插入排序*/
  3.         for ( i=2; i<=s->length; i++ )
  4.         {
  5.                 s->elem[0]=s->elem[i]; /* 保存待插入元素*/
  6.                 low=i;
  7.                 high=i-1; /* 设置初始区间*/
  8.                 while ( low<=high ) /* 该循环语句完成确定插入位置*/
  9.                 {
  10.                         mid= ( low+high ) /2;
  11.                         if ( s->elem[0].key>s->elem[mid].key )
  12.                                 low=mid+1; /* 插入位置在高半区中*/
  13.                         else high=mid-1; /* 插入位置在低半区中*/
  14.                 }/* while */
  15.                 for ( j=i-1; j>=high+1; j-- ) /* high+1 为插入位置*/
  16.                         s->elem[j+1]=s->elem[j]; /* 后移元素,留出插入空位*/
  17.                 s->elem[high+1]=s->elem[0]; /* 将元素插入*/
  18.         }/* for */
  19. }/* InsertSort */
  20.  

回复 "折半插入排序"

这儿你可以回复上面这条便签

captcha