列想一种,可以扩展一下其他排序算法!
demo:
def QuickSort(myList, start, end):
# 判断low是否小于high,如果是False,直接返回
if start < end:
i, j = start, end
# 设置基准数
base = myList[i]
while i < j:
# 如果列表后边的数大于等于基数时,前移一位到比基数小的数出现为止
while (i < j) and (myList[j] >= base):
j = j - 1
myList[i] = myList[j]
while (i < j) and (myList[i] <= base):
i = i + 1
myList[j] = myList[i]
# 一轮后,列表分成两个半区,并且i = j,将数设置回base
myList[i] = base
QuickSort(myList, start, i - 1)
QuickSort(myList, j + 1, end)
return myList
myList = [49,38,65,97,76,13,27,49]
print("Quick Sort: ")
QuickSort(myList,0,len(myList)-1)
print(myList)