因此,我的程序目前按升序排列shellSorts,但我需要按降序排序。我想要一些帮助,我应该改变什么,使它按降序排序。注意,内置的排序函数是不允许的,例如排序(数组,reverse=True)
def shellSort(array):
n = len(array)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = array[i]
j = i
while j >= gap and array[j - gap] < temp:
array[j] = array[j - gap]
j -= gap
array[j] = temp
gap //= 2
array = []
while True:
try:
user_input = input().split()
array.append(user_input)
except EOFError:
break
for i in array:
shellSort(i)
print(' '.join(i))发布于 2019-09-15 06:58:56
更改:
while j >= gap and array[j - gap] > temp:至:
while j >= gap and array[j - gap] < temp:因此:
array = [2, 5, 4, 8, 1]
shellSort(array)
print(array)将产出:
[8, 5, 4, 2, 1]https://stackoverflow.com/questions/57941753
复制相似问题