def quicksort(mas):
if mas:
mid = mas[0]
menshe = [i for i in mas[1:] if i < mid]
bolshe = [i for i in mas[1:] if i >= mid]
return quicksort(menshe) + [mid] + quicksort(bolshe)
else:
return mas
n = int(input())
mas = input().split()
print(*quicksort(mas))
它未能對一些測試,例如快速排序的Python排序麻煩
input:
3
8 21 22
output:
21 22 8
如何提高代碼?
你輸入'mas'包含字符串,而不是整數。因此你有一個詞典編排:'2'在'8'之前。 – MrT
注意:您的'n'變量未被使用。也許'mas = input()。split()[:n]'?或者'mas = [int(item)for input in()。split()[:n]]'還包含解決方案嗎? – CristiFati