25 lines
683 B
Python
25 lines
683 B
Python
# 基本情况:列表长度小于等于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] |