Shell-sort algorithm variations in Java

好久不见. 提交于 2019-12-20 01:41:41

问题


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 )

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 )

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

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


回答1:


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);
        ...
    }



回答2:


for( int gap = 1; gap < ((a.length + 2)/3); gap = (((((gap *2)+1)*3)-1)/2))


来源:https://stackoverflow.com/questions/13828461/shell-sort-algorithm-variations-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!