Java shell排序间隔问题

Java shell排序间隔问题,java,algorithm,sorting,intervals,shellsort,Java,Algorithm,Sorting,Intervals,Shellsort,我需要在使用标准间隔大小和非标准大小时测试shellsort的效率。我遇到的问题是当我尝试使用非标准间隔时 这是h等于标准间隔大小时的Shellsort: public void shellSort() { int inner, outer; int temp; int h = 1; while (h <= length / 3) { h = h * 3 + 1; } while (h > 0

我需要在使用标准间隔大小和非标准大小时测试shellsort的效率。我遇到的问题是当我尝试使用非标准间隔时

这是h等于标准间隔大小时的Shellsort:

    public void shellSort() 
    {
    int inner, outer;
    int temp;
    int h = 1;

    while (h <= length / 3)
    {
      h = h * 3 + 1; 
    }

    while (h > 0) 
    {

      for (outer = h; outer < length; outer++) 
      {
        temp = data[outer];
        inner = outer;

        while (inner > h - 1 && data[inner - h] >= temp) 
        {
          data[inner] = data[inner - h];
          inner -= h;
        }
        data[inner] = temp;
      }

      h = (h - 1) / 3; 

    }

  }
public void shell排序()
{
内部,外部;
内部温度;
int h=1;
while(h0)
{
用于(外部=h;外部<长度;外部++)
{
温度=数据[外部];
内部=外部;
while(内部>h-1&&data[internal-h]>=temp)
{
数据[内部]=数据[内部-h];
内-=h;
}
数据[内部]=温度;
}
h=(h-1)/3;
}
}
这是我尝试使用素数区间的结果

      private int[]  primes = {0, 1, 3, 7, 13, 31, 97, 211, 503, 1013, 2503, 5171};
      public void shellSort() 
      {
        int inner, outer;
        int temp;
        int count = this.h.length - 1;
        int h = 1;

        h = primes[primes.length - 1] * 2 > length ? primes[primes.length - 1] : primes[primes.length - 2];

        while (h > 0) 
        {         
          for (outer = h; outer < length; outer++) 
          {
            temp = data[outer];
            inner = outer;

            while (inner > h - 1 && data[inner - h] >= temp) 
            {
              data[inner] = data[inner - h];
              inner -= h;
            }
            data[inner] = temp;
          }

          if(count - 1 > 0)           
              h = primes[count - 1];              

        }

      }
private int[]primes={0,1,3,7,13,31,97,211,503,1013,2503,5171};
公共空shell排序()
{
内部,外部;
内部温度;
int count=此.h.长度-1;
int h=1;
h=素数[primes.length-1]*2>长度?素数[primes.length-1]:素数[primes.length-2];
而(h>0)
{         
用于(外部=h;外部<长度;外部++)
{
温度=数据[外部];
内部=外部;
while(内部>h-1&&data[internal-h]>=temp)
{
数据[内部]=数据[内部-h];
内-=h;
}
数据[内部]=温度;
}
如果(计数-1>0)
h=素数[计数-1];
}
}
我试图根据实时效率比较这两种方法,但我不知道如何让这个基本间隔起作用

我正在尝试测试:

  • 通过适当选择间隔大小,Shellsort的性能优于O(N^2)
  • 选择的一系列间隔大小对于实现优于O(N^2)的运行时非常重要

感谢您的帮助。

您可能希望在外部循环的每次迭代中减小
count
的值。在您的代码中,它仍然是
this.h.length-1
,即11。因此,在外部循环的每次迭代之后,您都满足了
if
条件
count-1>0
,因此您将
h=this.h[count-1]
设置为2503。所以,你重新进入循环


顺便说一下,调用间隔大小列表
h
会严重影响可读性。你至少应该叫它
hs

它怎么了?我认为它排序不正确?或者你不知道如何计时吗?我知道如何计时,正是间隔本身将shellsort方法扔进了一个无限循环,或者它没有正确排序。有人能告诉我素数区间的计算是否正确吗?相信我,我从来没有想过。我只是根据经验给我的数据命名,从来没有机会给它重新命名。