Creating shell sort with an interval of Pratt's sequence in Python

70 views Asked by At
def shellsort_pratt(list):

    lenoflist = len(list) # is it maximum interval
    

    products = [] # create a list of pratt sequence
    pow2 = 1
    pow3 = 1
    while pow3 <= lenoflist:
        while pow2 <= lenoflist:
            products.append(pow2)
            pow2 = pow2 * 2
        pow3 = pow3 * 3

     
    i = -1
    while min(products) > 0:
        for i in products[i]:
            temp = list[i]
            j = -i
            while j >= products[i] and list[ j-products[i] ] > temp:
                list[j] = list[ j-products[i] ]
                j -= products[i]

            list[j] = temp
        i -= 1


    return list

I am trying to sort a given random integer list using shell sort. The gap of the shell sort I am aiming for is Pratt's sequence (1, 2, 3, 4, 6, 9, 8, 12... backward), however, my code does not work. What should I fix to get the correct interval and result?

0

There are 0 answers