Skip to content

Instantly share code, notes, and snippets.

@zigzackey
Created March 30, 2016 08:04
Show Gist options
  • Save zigzackey/43b26d5057d9d243f5af32ccbd9fe11f to your computer and use it in GitHub Desktop.
Save zigzackey/43b26d5057d9d243f5af32ccbd9fe11f to your computer and use it in GitHub Desktop.
def insertionSort(A, n, g):
global cnt
for i in range(g, n):
v = A[i]
j = i - g
while j >= 0 and A[j] > v:
A[j + g] = A[j]
j = j - g
cnt = cnt + 1
A[j + g] = v
def shellSort(A, n):
global m
global G
h = 1
while (True):
if h > n:
break
G.append(h)
h = 3 * h + 1
m = len(G)
G.reverse()
for i in range(m):
insertionSort(A, n, G[i])
if __name__ == '__main__':
N = int(input())
R = [int(input()) for i in range(N)]
cnt = 0
G = []
m = 0
shellSort(R, N)
print(m)
print(" ".join(map(str, G)))
print(cnt)
for i in range(N):
print(R[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment