Last active
December 30, 2016 23:09
-
-
Save robbie01/543e85fc886d9117599f9d756da49161 to your computer and use it in GitHub Desktop.
Shellsort implemented in Python 3 using various gap sequences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import floor | |
def shellsort(list): | |
gaps = gengaps(len(list)) | |
for gap in gaps: | |
for i in range(gap, len(list)): | |
temp = list[i] | |
j = i | |
while j >= gap and list[j-gap] > temp: | |
list[j] = list[j - gap] | |
j -= gap | |
list[j] = temp | |
def gengaps(n): | |
unfinished = True | |
gaps = [] | |
exponent = 1 | |
while unfinished: | |
gap = floor(n/2**exponent) | |
exponent += 1 | |
if gap >= 1: | |
gaps.append(gap) | |
if gap == 1: | |
unfinished = False | |
return gaps | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
shellsort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shellsort(list): | |
gaps = [i for i in [701, 301, 132, 57, 23, 10, 4, 1] if i < len(list)] | |
for gap in gaps: | |
for i in range(gap, len(list)): | |
temp = list[i] | |
j = i | |
while j >= gap and list[j-gap] > temp: | |
list[j] = list[j - gap] | |
j -= gap | |
list[j] = temp | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
shellsort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shellsort(list): | |
gaps = gengaps(len(list)) | |
for gap in gaps: | |
for i in range(gap, len(list)): | |
temp = list[i] | |
j = i | |
while j >= gap and list[j-gap] > temp: | |
list[j] = list[j - gap] | |
j -= gap | |
list[j] = temp | |
def gengaps(n): | |
unfinished = True | |
gaps = [1] | |
k = 1 | |
while unfinished: | |
gap = 4**k+3*2**(k-1)+1 | |
k += 1 | |
if gap < n: | |
gaps.append(gap) | |
else: | |
unfinished = False | |
gaps.reverse() | |
return gaps | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
shellsort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
def shellsort(list): | |
gaps = gengaps(len(list)) | |
for gap in gaps: | |
for i in range(gap, len(list)): | |
temp = list[i] | |
j = i | |
while j >= gap and list[j-gap] > temp: | |
list[j] = list[j - gap] | |
j -= gap | |
list[j] = temp | |
def gengaps(n): | |
unfinished = True | |
gaps = [] | |
k = 1 | |
while unfinished: | |
gap = ceil((9**k-4**k)/(5*4**(k-1))) | |
k += 1 | |
if gap < n: | |
gaps.append(gap) | |
else: | |
unfinished = False | |
gaps.reverse() | |
return gaps | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
shellsort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment