Java Shellsort区间问题
当我使用标准间隔大小以及使用非标准大小时,我需要测试 shellsort 的效率。我遇到的问题是当我尝试使用非标准间隔时。
这是我的 Shellsort,当 h 等于标准间隔大小时:
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;
}
}
这是我尝试使用质数间隔
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];
}
}
我试图根据实时效率比较两者,但我不知道如何得到这个工作的主要间隔。
我正在尝试测试:
- 在适当选择间隔大小的情况下,Shellsort 的性能优于 O(N^2)
- 选择的一系列间隔大小对于实现优于 O(N^2) 运行时间非常重要
谢谢您的帮助。
I need to test the efficiency of shellsort when I am using the standard interval size and also while using a non-standard size. The problem I am encountering is when I try to use my non-standard interval.
This is my Shellsort when h is equal to the standard interval size:
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;
}
}
And here is my attempt at using a prime number interval
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];
}
}
I am trying to compare the two based off of real time efficiency , and I can't figure out how to get this prim interval to work.
I'm trying to test:
- Shellsort performs better than O(N^2) with appropriately chosen interval sizes
- The series of interval sizes chosen is important to achieving better than O(N^2) runtime
Thank you for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能希望在外循环的每次迭代中减少
count
的值。在您的代码中,它仍然是this.h.length-1
,即 11。因此,在外循环的每次迭代之后,您都会有if
条件count -1> 0
满足,所以你设置了h = this.h[count-1]
,我认为是 2503。所以,你重新进入循环。顺便说一句,调用间隔大小列表
h
会严重影响可读性。您至少应该将其命名为hs
。You probably want to decrement the value of
count
in each iteration of the outer loop. In your code it is stillthis.h.length-1
, which is 11. Therefore, after each iteration of the outer loop you have theif
conditioncount-1 > 0
satisfied, so you seth = this.h[count-1]
, which I believe is 2503. So, you reenter the loop.By the way, calling the list of interval sizes
h
seriously impedes readability. You should call it at leasths
.