我有几个在网上找不到的关于 Shell 排序与 Shell 差距的问题。
public static void shell(int[] a) {
int increment = a.length / 2;
while (increment > 0) {
for (int i = increment; i < a.length; i++) {
int j = i;
int temp = a[i];
while (j >= increment && a[j - increment] > temp) {
a[j] = a[j - increment];
j = j - increment;
}
a[j] = temp;
}
if (increment == 2) {
increment = 1;
} else {
increment *= (5.0 / 11);
}
}
}
这是网上找的代码,但是最后的else语句我不是很懂。 5.0/11代表什么? 我还需要分析算法的复杂性,尽管我收到的结果非常令人困惑:
似乎最好和最坏的情况都是 O(n)。这些结果是否合法?
最佳答案
5.0/11其实就是用来获取增量的一半。任何 <0.5 和 >0.45 的值都可以获得整数值的一半。
关于algorithm - shell排序的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42988004/