laide_teacher_python/20240710.py

25 lines
683 B
Python
Raw Normal View History

2024-07-12 18:54:25 +08:00
# 基本情况列表长度小于等于1
# 递归过程:找中间值,与中间值进行比较,返回左,中,右连接后的结果
l = [5,3,7,6,4,1]
def fun(lst):
if len(lst) <= 1:
return lst
else:
mid = lst[0]
print("当前中间值:",mid)
left = []
right = []
for i in range(1,len(lst)):
if lst[i] < mid:
left.append(lst[i])
else:
right.append(lst[i])
print("左侧列表:",left)
print("右侧列表:", right)
return fun(left)+[mid]+fun(right)
fun(l)
# 使用快速排序实现对以下列表的排序,[98,89,78,69,53,78,99]