Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何实现普拉特间隙序列?(Python,Shell排序)_Python_Sorting_Shellsort - Fatal编程技术网

如何实现普拉特间隙序列?(Python,Shell排序)

如何实现普拉特间隙序列?(Python,Shell排序),python,sorting,shellsort,Python,Sorting,Shellsort,我必须用Python编写一个shell排序程序,但除了它之外,我还必须有一个使用一些特殊间隙序列创建文本文件的程序,这就是我的shell排序将获得间隙编号的地方 在维基百科()上,普拉特序列的方程式是:“形式为2^p*3^q的连续数”,它产生1,2,3,4,6,8,9,12 我不知道如何实现这个,基本上什么是p和Q 最坏情况下的时间复杂度为O(Nlog^2N) 序列生成器文件的当前代码: def Hibbard(big): H = open("Hibbard.txt","

我必须用Python编写一个shell排序程序,但除了它之外,我还必须有一个使用一些特殊间隙序列创建文本文件的程序,这就是我的shell排序将获得间隙编号的地方

在维基百科()上,普拉特序列的方程式是:“形式为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)
SedB(大型)
在普拉特序列的定义中,p和q分别是2和3的指数。您需要找到所有2和3次幂的乘积,它们都不大于您的排序的最大间隙大小。为此,制作一张顶部为2倍、底部为3倍的表格,并用其产品填充每个单元格,直到它们超过最大间隙大小。例如,最大间隙尺寸为500时,表格如下所示:

   1   2   4   8  16  32  64 128 256
   3   6  12  24  48  96 192 384
   9  18  36  72 144 288
  27  54 108 216 432
  81 162 324
 243 486
现在用Python模拟此表的生成

def generate_pratt(max_size):
    """Generate a sorted list of products of powers of 2 and 3 below max_size"""
    # for https://stackoverflow.com/q/25964453/2738262
    products = []
    pow3 = 1  # start with q = 0
    while pow3 <= max_size:
        # At this point, pow3 = 3**q, so set p = 0
        pow2 = pow3
        while pow2 <= max_size:
            # At this point, pow2 = 2**p * 3**q
            products.append(pow2)
            pow2 = pow2 * 2  # this is like adding 1 to p
        # now that p overflowed the maximum size, add 1 to q and start over
        pow3 = pow3 * 3

    # the Pratt sequence is the result of this process up to the given size
    return sorted(products)

print(generate_pratt(12))
def生成pratt(最大大小):
“”“生成小于最大值2和3次幂的产品的排序列表”“”
#为了https://stackoverflow.com/q/25964453/2738262
产品=[]
pow3=1#从q=0开始

而在普拉特序列的定义中,pow3,p和q分别是2和3的指数。您需要找到所有2和3次幂的乘积,它们都不大于您的排序的最大间隙大小。为此,制作一张顶部为2倍、底部为3倍的表格,并用其产品填充每个单元格,直到它们超过最大间隙大小。例如,最大间隙尺寸为500时,表格如下所示:

   1   2   4   8  16  32  64 128 256
   3   6  12  24  48  96 192 384
   9  18  36  72 144 288
  27  54 108 216 432
  81 162 324
 243 486
现在用Python模拟此表的生成

def generate_pratt(max_size):
    """Generate a sorted list of products of powers of 2 and 3 below max_size"""
    # for https://stackoverflow.com/q/25964453/2738262
    products = []
    pow3 = 1  # start with q = 0
    while pow3 <= max_size:
        # At this point, pow3 = 3**q, so set p = 0
        pow2 = pow3
        while pow2 <= max_size:
            # At this point, pow2 = 2**p * 3**q
            products.append(pow2)
            pow2 = pow2 * 2  # this is like adding 1 to p
        # now that p overflowed the maximum size, add 1 to q and start over
        pow3 = pow3 * 3

    # the Pratt sequence is the result of this process up to the given size
    return sorted(products)

print(generate_pratt(12))
def生成pratt(最大大小):
“”“生成小于最大值2和3次幂的产品的排序列表”“”
#为了https://stackoverflow.com/q/25964453/2738262
产品=[]
pow3=1#从q=0开始

而在普拉特序列的定义中,pow3,p和q分别是2和3的指数。您需要找到所有2和3次幂的乘积,它们都不大于您的排序的最大间隙大小。为此,制作一张顶部为2倍、底部为3倍的表格,并用其产品填充每个单元格,直到它们超过最大间隙大小。例如,最大间隙尺寸为500时,表格如下所示:

   1   2   4   8  16  32  64 128 256
   3   6  12  24  48  96 192 384
   9  18  36  72 144 288
  27  54 108 216 432
  81 162 324
 243 486
现在用Python模拟此表的生成

def generate_pratt(max_size):
    """Generate a sorted list of products of powers of 2 and 3 below max_size"""
    # for https://stackoverflow.com/q/25964453/2738262
    products = []
    pow3 = 1  # start with q = 0
    while pow3 <= max_size:
        # At this point, pow3 = 3**q, so set p = 0
        pow2 = pow3
        while pow2 <= max_size:
            # At this point, pow2 = 2**p * 3**q
            products.append(pow2)
            pow2 = pow2 * 2  # this is like adding 1 to p
        # now that p overflowed the maximum size, add 1 to q and start over
        pow3 = pow3 * 3

    # the Pratt sequence is the result of this process up to the given size
    return sorted(products)

print(generate_pratt(12))
def生成pratt(最大大小):
“”“生成小于最大值2和3次幂的产品的排序列表”“”
#为了https://stackoverflow.com/q/25964453/2738262
产品=[]
pow3=1#从q=0开始

而在普拉特序列的定义中,pow3,p和q分别是2和3的指数。您需要找到所有2和3次幂的乘积,它们都不大于您的排序的最大间隙大小。为此,制作一张顶部为2倍、底部为3倍的表格,并用其产品填充每个单元格,直到它们超过最大间隙大小。例如,最大间隙尺寸为500时,表格如下所示:

   1   2   4   8  16  32  64 128 256
   3   6  12  24  48  96 192 384
   9  18  36  72 144 288
  27  54 108 216 432
  81 162 324
 243 486
现在用Python模拟此表的生成

def generate_pratt(max_size):
    """Generate a sorted list of products of powers of 2 and 3 below max_size"""
    # for https://stackoverflow.com/q/25964453/2738262
    products = []
    pow3 = 1  # start with q = 0
    while pow3 <= max_size:
        # At this point, pow3 = 3**q, so set p = 0
        pow2 = pow3
        while pow2 <= max_size:
            # At this point, pow2 = 2**p * 3**q
            products.append(pow2)
            pow2 = pow2 * 2  # this is like adding 1 to p
        # now that p overflowed the maximum size, add 1 to q and start over
        pow3 = pow3 * 3

    # the Pratt sequence is the result of this process up to the given size
    return sorted(products)

print(generate_pratt(12))
def生成pratt(最大大小):
“”“生成小于最大值2和3次幂的产品的排序列表”“”
#为了https://stackoverflow.com/q/25964453/2738262
产品=[]
pow3=1#从q=0开始
而pow3

罗伯特·威尔逊的观察结果可用于筛选数字
但是它会太慢O(n^2)
Mathematica代码看起来不错,可以生成
在少于O(n)的时间内排序的普拉特序列

单元队列;
接口
键入pfifo_节点=^fifo_节点;
fifo_节点=记录
数据:longint;
下一步:pfifo_节点;
结束;
fifo_指针=记录
头部、尾部:pfifo_节点;
结束;
过程队列_init(变量fifo:fifo_指针);
函数队列为空(fifo:fifo指针):布尔值;
过程排队(var-fifo:fifo_指针;d:longint);
过程出列(变量fifo:fifo_指针);
程序打印队列(fifo:fifo指针);
实施
过程队列_init(变量fifo:fifo_指针);
开始
先进先出水头:=零;
fifo.tail:=零;
结束;
函数队列为空(fifo:fifo指针):布尔值;
开始
队列为空:=((fifo.head=NIL)和(fifo.tail=NIL));
结束;
过程排队(var-fifo:fifo_指针;d:longint);
var新节点:pfifo节点;
开始
新建(新建_节点);
新节点^。数据:=d;
新节点^.next:=NIL;
如果(fifo.head=NIL),则
开始
fifo.head:=新的_节点;
fifo.tail:=新的_节点;
结束
其他的
开始
fifo.tail^.next:=新节点;
fifo.tail:=新的_节点;
结束;
结束;
过程出列(变量fifo:fifo_指针);
var-tmp:pfifo_节点;
开始
如果(fifo.head NIL),则
开始
tmp:=fifo.head^.next;
处置(先进先出器头);
fifo.head:=tmp;
如果(tmp=NIL),则
fifo.tail:=零;
结束;
结束;
程序打印队列(fifo:fifo指针);
var-tmp:pfifo_节点;
f:文本;
开始
分配(f,);
重写(f);
tmp:=先进先出(fifo.head);
而(tmp-NIL)do
开始
写入(f,tmp^.数据');
tmp:=tmp^.next;
结束;
书面(f);
关闭(f);
结束;
开始
结束。
程序PrattGenerator;
使用crt、队列;
var fifo:fifo_指针;
n、 pow3:长型;
err:整数;
aux:pfifo_节点;
开始
val(ParamStr(1),n,err);
队列初始化(fifo);
排队(先进先出,1);
pow3:=3;
aux:=先进先出磁头;
而(fifo.tail^.数据

罗伯特·威尔逊的观察结果可用于筛选数字
但是它会太慢O(n^2)
Mathematica代码看起来不错,可以生成
在少于O(n)的时间内排序的普拉特序列

单元队列;
接口
键入pfifo_节点=^fifo_节点;
fifo_节点=记录
数据