laide_teacher_python/20240710.py

25 lines
683 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 基本情况列表长度小于等于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]