Reputation: 37
I want to sort a list using Shell sort, but in the reverse mode. What do I need to change in my code to make the sorting descending? This is my working code:
from timeit import default_timer as timer
import resource
start = timer()
def shellSort(array):
gap = len(array) // 2
# loop over the gaps
while gap > 0:
# do the insertion sort
for i in range(gap, len(array)):
val = array[i]
j = i
while j >= gap and array[j - gap] > val:
array[j] = array[j - gap]
j -= gap
array[j] = val
gap //= 2
with open('lista.txt', 'r') as f:
long_string = f.readline()
alist = long_string.split(',')
shellSort(alist)
f = open("shell.txt", "w")
print >>f,(alist)
print resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000
end = timer()
print(end - start)
f.close()
print 'Shell\n'
Thank you :D
Upvotes: 2
Views: 670
Reputation: 363
array[j-gap] < gap
Or just reverse the final list using reversed()
method like:
reversed(array)
Upvotes: 1