array = [2, 3, 5, 7, 1, 4, 6, 15, 5, 2, 7, 9, 10, 15, 9, 17, 12]
l = [1,4,5,3,2,6,7,9,8,8,]
def maopao(num):
for i in range(len(num)-1):
for j in range(len(num)-i-1):
if num[j] > num[j+1]:
num[j], num[j+1] = num[j+1], num[j]
return num
def QuickSort(data):
"""快速排序"""
if len(data) >= 2: # 递归入口及出口
mid = data[len(data) // 2] # 选取基准值,也可以选取第一个或最后一个元素
left, right = [], [] # 定义基准值左右两侧的列表
data.remove(mid) # 从原始数组中移除基准值
for num in data:
if num >= mid:
right.append(num)
else:
left.append(num)
return QuickSort(left) + [mid] + QuickSort(right)
else:
return data
if __name__ == '__main__':
print(maopao(l))
print(QuickSort(array))
result:
[1, 2, 3, 4, 5, 6, 7, 8, 8, 9]
[1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 9, 9, 10, 12, 15, 15, 17]
来源:CSDN
作者:liuskyter
链接:https://blog.csdn.net/liuskyter/article/details/103570567