有没有办法计算for循环的起点和对它的调整。原始循环具有这些条件
for( int gap = a.length / 2; gap > 0; gap /= 2 )
我调整它来设置Hibbard的Shell Sort的条件并得到了这个
for( int gap = (int) Math.pow(2, a.length); gap > 0; gap /= 2 )
它的工作稍微好一些,甚至可能是正确的,但我想从这里使用更高级的shell排序。
http://en.wikipedia.org/wiki/Shellsort#Gap_sequences
如何将(n ^ k - 1)/ 2不大于n / 3的上限转换为for循环条件?
答案 0 :(得分:3)
“k”值是序列的元素。所以你的for循环可能看起来像:
for (int k = 0; (Math.pow(3, k) - 1) / 2 <= Math.ceil(n / 3); k++) {
int gap = (int) ((Math.pow(3, k) - 1) / 2);
...
}
答案 1 :(得分:0)
for( int gap = 1; gap < ((a.length + 2)/3); gap = (((((gap *2)+1)*3)-1)/2))