-
Notifications
You must be signed in to change notification settings - Fork 88
/
shellSort.py
37 lines (33 loc) · 934 Bytes
/
shellSort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
''' mbinary
#########################################################################
# File : shellSort.py
# Author: mbinary
# Mail: [email protected]
# Blog: https://mbinary.xyz
# Github: https://github.com/mbinary
# Created Time: 2018-07-06 16:30
# Description:
#########################################################################
'''
def shellSort(s, gaps=None):
if gaps is None:
gaps = [127, 63, 31, 15, 7, 3, 1]
n = len(s)
for gap in gaps:
for j in range(gap, n):
cur = j
num = s[j]
while cur >= gap and num < s[cur-gap]:
s[cur] = s[cur-gap]
cur -= gap
s[cur] = num
return s
if __name__ == '__main__':
from random import randint
import sys
n = 20
if len(sys.argv) > 1:
n = int(sys.argv[1])
nums = [randint(1, 100) for i in range(n)]
print(nums)
print(shellSort(nums))