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會動,但我不知道他為甚麼會動