Shell排序多个空白

时间:2017-12-14 20:21:54

标签: c++ shellsort

我必须创建具有间隙O(N ^ 3/2)和O(N ^ 4/3)的Shell排序代码,而不是将其减少为1,程序现在如何实现。这就是我实际得到的:

using namespace std;

int main()
{
  int N;
  cout << "Amount of numbers: ";
  cin >> N;
  int d[N],h,i,j,x;
  srand((unsigned)time(NULL));

  for(i = 0; i < N; i++) d[i] = rand() % 100;
  for(i = 0; i < N; i++) cout << setw(4) << d[i];
  cout << endl;
  for(h = 1; h < N; h =  2^h - 1 );
  //h = N/2;
  //if(!h) h++; 
  while(h)
  {
    for(j = N - h - 1; j >= 0; j--)
    {
      x = d[j];
      i = j + h;
      while((i < N) && (x > d[i]))
      {
        d[i - h] = d[i];
        i += h;
      }
      d[i - h] = x;
    }
    h /= 3;
  }
  cout << "After sort :\n\n";
  for(i = 0; i < N; i++) cout << setw(4) << d[i];
  cout << endl;
  return 0;
}

0 个答案:

没有答案