首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请教SHELL算法中这一步有何用啊

2012-07-31 
请问SHELL算法中这一步有何用啊?/* shellsort:sort v[0]...v[n-1] into increasing order */void shellsor

请问SHELL算法中这一步有何用啊?
/* shellsort: sort v[0]...v[n-1] into increasing order */
  void shellsort(int v[], int n)
  {
  int gap, i, j, temp;

  for (gap = n/2; gap > 0; gap /= 2)
  for (i = gap; i < n; i++)
  for (j=i-gap; j>=0 && v[j]>v[j+gap]; j-=gap) {
  temp = v[j];
  v[j] = v[j+gap];
  v[j+gap] = temp;
  }
  }


如上为SHELL算法 最内层for语句参数中的j-=gap 有何作用 我目测是为了切换成功一次后跳出最内层for循环 
因为j自减之后,要么j<0要么v[j] < v[j+gap],不符合继续循环条件。

我的想法是否正确 请大家指点!

[解决办法]
呵呵,是的。

热点排行