方法1:
def kp(ls):
if len(ls) <=1:return ls
mins = []
maxs = []
for i in ls[1:]:
if i < ls[0]:
mins.append(i)
elif i >= ls[0]:
maxs.append(i)
return kp(mins) + ls[0:1] + kp(maxs)
res = kp([9,8,7,6,5,4,3,2,1,10])
print(res)
方法2:
def kp2(ll):if len(ll) <= 1:
return ll
return kp2([i for i in ll[1:] if i < ll[0]]) + ll[0:1] + kp2([i for i in ll[1:] if i >= ll[0]])
print(kp2([9,7,3,1,65,2,4,65,8]))
Python学习交流、资源共享群:563626388 QQ