我必须用Python编写一个shell sort程序,但同时我必须有一个程序,它使用一些特殊的间隙序列来创建文本文件,这就是我的shell sort将得到它的间隙编号的地方。在
在Wikipedia(http://en.wikipedia.org/wiki/Shellsort)上,Pratt序列的方程是:“形式为2^p*3^q的连续数”,它产生1,2,3,4,6,8,9,12,。。。在
我不知道如何实现,基本上p和Q是什么?在
最坏情况下的时间复杂度为O(Nlog^2N)
序列生成器文件的当前代码:
def Hibbard(big):
H = open("Hibbard.txt","w")
i = 1
math = (2**i)-1
while math <= big:
H.write(str(math))
H.write("\n")
i+=1
math = (2**i)-1
def Pratt(big):
pass
def SedA(big):
SA = open("SedgewickA.txt","w")
SA.write("1\n")
i = 1
math = (4**i)+3*2**(i-1)+1
while math <= big:
SA.write(str(math))
SA.write("\n")
i+=1
math = (4**i)+3*2**(i-1)+1
def SedB(big):
pass
def main():
big = int(input("Enter the largest gap: "))
Hibbard(big)
main()
http://oeis.org/A003586
罗伯特·威尔逊的观察结果可用于筛选数字
但速度太慢了O(n^2)
Mathematica代码看起来不错,可以生成
在不到O(n)时间内对Pratt序列进行排序
为了进行比较,我将介绍Damian的解决方案
^{pr2}$我们可以使用堆栈来反转元素的顺序
不需要为堆栈创建新节点
我们可以尝试从现有队列节点构建堆栈
在Pratt序列的定义中,p和q分别是2和3的指数。你需要找到所有幂为2和3的乘积不大于你同类的最大间隙大小。要做到这一点,做一张表格,上面的幂是2,下面是3的幂,然后用它们的乘积填充每个单元格,直到它们超过最大间隙大小。例如,最大间隙尺寸为500时,表格如下所示:
现在用Python模拟这个表的生成。在
^{pr2}$相关问题 更多 >
编程相关推荐