Java中的Shell排序算法变体 [英] Shell-sort algorithm variations in Java

查看:129
本文介绍了Java中的Shell排序算法变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法计算for循环的起点和对它的调整。原始循环具有以下条件

Is there a way to calculate the starting point of a for loop and the adjustments to it. The original loop has these conditions

for(int gap = a.length / 2; gap> 0; gap / = 2)

我调整它来设置Hibbard的Shell Sort的条件并得到这个

I adjusted it to set the conditions of the Hibbard's Shell Sort and got this

for(int gap =(int)Math.pow(2,a.length); gap> 0; gap / = 2)

它的效果稍微好一点,甚至可能是正确的,但我想从这里开始使用更高级的shell排序。

It works slightly better and might even be right, but I want to work with the more advanced shell sorts from here.

http://en.wikipedia.org/wiki/Shellsort#Gap_sequences

我怎样才能将(3 ^ k-1)/ 2不大于n / 3的上限变成for循环条件?

How could I turn (3^k - 1)/2 not greater than the ceiling of n/3 into a for loop condition?

推荐答案

k值是序列的元素。所以你的for循环可能看起来像:

The "k" value is the element of the sequence. So your for loop would probably look something like:

    for (int k = 0; (Math.pow(3, k) - 1) / 2 <= Math.ceil(n / 3); k++) {
        int gap = (int) ((Math.pow(3, k) - 1) / 2);
        ...
    }

这篇关于Java中的Shell排序算法变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆