2017-05-14 67 views
1

我想使用Shell排序對列表進行排序,但處於反向模式。 我需要更改我的代碼以使排序降序? 這是我的工作代碼:在Python中反向排序Shell排序

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' 

謝謝:d

回答

0

array[j-gap] < gap

或者只是使用反向方法reversed()最終名單,如: reversed(array)