What is the formula to get the number of passes in a shell sort?

我怕爱的太早我们不能终老 提交于 2020-07-10 08:58:05

问题


I'm referring to the original (Donald Shell's) algorithm. I'm trying to make a subjective sort based on shell sort. I already made all the logic, where it is exactly the same as the shell sort, but instead of the computer calculate what is greater, the user determines subjectively what is greater. But I would like to display a percentage or something to the user know how far in the sorting it is already. That's why I want to find a way to know it.

What is the formula to get the number of passes in a shell sort? I noticed it the number is not fixed, so what would be the minimum and maximum? N and N^2? Or maybe if you have an idea of how it is the best way to display the progress of the sorting, I will really appreciate it.

PS: It is not about the number of comparisons! Also not about time complexity. My question is about the number of passes in the array.

I did this formula displaying it by color. But it doesn't work with the right range.

List<Color> colors = [
    Color(0xFFFF0000),//red
    Color(0xFFFF5500),
    Color(0xFFFFAA00),
    Color(0xFFFFFF00),//yellow
    Color(0xFFAAFF00),
    Color(0xFF00FF00),
    Color(0xFF00FF00),//green
  ];
[...]
style: TextStyle(
                          color: colors[(((pass - 1) * (colors.length - 1)) /
                                  sqrt(a.length).ceil())
                              .floor()]),
[...]

回答1:


Because one pass is the application of one element of the gap sequence, the number of passes depends on the used gap sequence.

If you use Shell's original gap sequence of powers of two, the number of passes is approximately the binary logarithms of the input size.

You can read more about other proposed gap sequences in the wikipedia article on Shell Sort: https://en.wikipedia.org/wiki/Shellsort



来源:https://stackoverflow.com/questions/62452818/what-is-the-formula-to-get-the-number-of-passes-in-a-shell-sort

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