Write a function called "mySort" that takes an list of integers as input, and
returns the sorted version of the input list. You are not allowed to use the
built-in sorted() function.
mySort([17, 0, -3, 2, 1, 0.5]); # returns [-3, 0, 0.5, 1, 2, 17]
写到一半问朋友
朋友告诉我气泡排序法
def mySort(lst):
n = len(lst)
for i in range(n):
for ele in range(0, n - i - 1):
if lst[ele] > lst[ele + 1]:
lst[ele], lst[ele + 1] = lst[ele + 1], lst[ele]
print(lst)
return lst
mySort([17, 0, -3, 2, 1, 0.5])
# returns [-3, 0, 0.5, 1, 2, 17]
我这样写能过
但我有个问题
for ele in range(0, n - i - 1):
这句我不太懂怎么解释
但后面的东西我懂
我现在是一个我的code会动,但我不知道他为什么会动